Class: Kindler::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/kindler.rb

Defined Under Namespace

Classes: KindlerError

Constant Summary collapse

TMP_DIR_PREFIX =
'__km_'
DEFAULT_SECTION =
"All Pages"
PAGE_ATTRIBUTES =
%w(wrap title author content section url)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Book

availabel options

Parameters:

  • options (Hash) (defaults to: {})
  • title (Hash)

    a customizable set of options

  • output_dir (Hash)

    a customizable set of options

  • debug (Hash)

    a customizable set of options

  • keep_image (Hash)

    a customizable set of options

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kindler.rb', line 30

def initialize(options={})
  @output_dir = options[:output_dir] || ''
  @keep_image = options[:keep_image] || true
  @debug = options[:debug]
  @title = options[:title] || ''
  @author = options[:author] || 'unknown'
  @mobi_type = options[:mobi_type] || :simple
  @cover = options[:cover] || ""
  @silent = options[:silent]
  @pages = []
  @local_images = []
  @pages_by_section = {}
  @style = options[:style] || ''
  raise KindlerError.new("must provide the book title ") unless title
end

Instance Attribute Details

#authorObject

Returns the value of attribute author.



18
19
20
# File 'lib/kindler.rb', line 18

def author
  @author
end

#cover_imageObject

Returns the value of attribute cover_image.



18
19
20
# File 'lib/kindler.rb', line 18

def cover_image
  @cover_image
end

#local_imagesObject

Returns the value of attribute local_images.



18
19
20
# File 'lib/kindler.rb', line 18

def local_images
  @local_images
end

#mobi_typeObject

Returns the value of attribute mobi_type.



18
19
20
# File 'lib/kindler.rb', line 18

def mobi_type
  @mobi_type
end

#pagesObject

Returns the value of attribute pages.



18
19
20
# File 'lib/kindler.rb', line 18

def pages
  @pages
end

#pages_by_sectionObject

Returns the value of attribute pages_by_section.



18
19
20
# File 'lib/kindler.rb', line 18

def pages_by_section
  @pages_by_section
end

#styleObject

Returns the value of attribute style.



18
19
20
# File 'lib/kindler.rb', line 18

def style
  @style
end

#titleObject

Returns the value of attribute title.



18
19
20
# File 'lib/kindler.rb', line 18

def title
  @title
end

Instance Method Details

#add_article(options = {}) ⇒ Object



62
63
64
# File 'lib/kindler.rb', line 62

def add_article(options={})
  add_page(options)
end

#add_page(options = {}) ⇒ Object

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kindler.rb', line 46

def add_page(options={})
  raise KindlerError.new('must provide title when add page') unless options[:title]
  page = options.reject{|k,v| PAGE_ATTRIBUTES.include?(k)}
  page[:wrap] ||= true
  page[:section] ||= DEFAULT_SECTION
  page[:count] = pages.count + 1
  page[:file_name] = "#{page[:count].to_s.rjust(3,'0')}.html"
  page[:author] = '' unless page[:author]
  # escape special chars
  page[:title] = CGI::escapeHTML(page[:title])
  page[:title] = title if(page[:title] == "")
  page[:author] = CGI::escapeHTML(page[:author])
  pages << page
  debug pages
end

#book_pathObject



98
99
100
# File 'lib/kindler.rb', line 98

def book_path
  "#{tmp_dir}/#{title}.mobi"
end

#generateObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kindler.rb', line 66

def generate
  make_generated_dirs
  localize_images if @keep_image
  prepare_conver_img
  # reorder count index
  if magzine?
    sectionize_pages
  end
  generate_toc
  generate_opf
  generate_ncx
  write_to_disk
  kindlegen
end

#generated?Boolean

check mobi file is generated already

Returns:

  • (Boolean)


94
95
96
# File 'lib/kindler.rb', line 94

def generated?
  File.exist? book_path
end

#sectionize_pagesObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kindler.rb', line 81

def sectionize_pages
  self.pages.each do |page|
    pages_by_section[page[:section]] ||= []
    pages_by_section[page[:section]] << page
  end
  self.pages = pages_by_section.values.flatten
  self.pages.each_with_index do |page,index|
    page[:count] = index + 1
    page[:file_name] = "#{page[:count].to_s.rjust(3,'0')}.html"
  end
end