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:



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

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.



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

def author
  @author
end

#cover_imageObject

Returns the value of attribute cover_image.



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

def cover_image
  @cover_image
end

#local_imagesObject

Returns the value of attribute local_images.



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

def local_images
  @local_images
end

#mobi_typeObject

Returns the value of attribute mobi_type.



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

def mobi_type
  @mobi_type
end

#pagesObject

Returns the value of attribute pages.



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

def pages
  @pages
end

#pages_by_sectionObject

Returns the value of attribute pages_by_section.



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

def pages_by_section
  @pages_by_section
end

#styleObject

Returns the value of attribute style.



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

def style
  @style
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#add_article(options = {}) ⇒ Object



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

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

#add_page(options = {}) ⇒ Object

Raises:



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

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



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

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

#generateObject



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

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)


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

def generated?
  File.exist? book_path
end

#sectionize_pagesObject



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

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