Class: Kindler::Book
- Inherits:
-
Object
- Object
- Kindler::Book
- 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
-
#author ⇒ Object
Returns the value of attribute author.
-
#cover_image ⇒ Object
Returns the value of attribute cover_image.
-
#local_images ⇒ Object
Returns the value of attribute local_images.
-
#mobi_type ⇒ Object
Returns the value of attribute mobi_type.
-
#pages ⇒ Object
Returns the value of attribute pages.
-
#pages_by_section ⇒ Object
Returns the value of attribute pages_by_section.
-
#style ⇒ Object
Returns the value of attribute style.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #add_article(options = {}) ⇒ Object
- #add_page(options = {}) ⇒ Object
- #book_path ⇒ Object
- #generate ⇒ Object
-
#generated? ⇒ Boolean
check mobi file is generated already.
-
#initialize(options = {}) ⇒ Book
constructor
availabel options.
- #sectionize_pages ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Book
availabel options
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/kindler.rb', line 29 def initialize(={}) @output_dir = [:output_dir] || '' @keep_image = [:keep_image] || true @debug = [:debug] @title = [:title] || '' @author = [:author] || 'unknown' @mobi_type = [:mobi_type] || :simple @cover = [:cover] || "" @silent = [:silent] @pages = [] @local_images = [] @pages_by_section = {} @style = [:style] || '' raise KindlerError.new("must provide the book title ") unless title end |
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
17 18 19 |
# File 'lib/kindler.rb', line 17 def @author end |
#cover_image ⇒ Object
Returns the value of attribute cover_image.
17 18 19 |
# File 'lib/kindler.rb', line 17 def cover_image @cover_image end |
#local_images ⇒ Object
Returns the value of attribute local_images.
17 18 19 |
# File 'lib/kindler.rb', line 17 def local_images @local_images end |
#mobi_type ⇒ Object
Returns the value of attribute mobi_type.
17 18 19 |
# File 'lib/kindler.rb', line 17 def mobi_type @mobi_type end |
#pages ⇒ Object
Returns the value of attribute pages.
17 18 19 |
# File 'lib/kindler.rb', line 17 def pages @pages end |
#pages_by_section ⇒ Object
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 |
#style ⇒ Object
Returns the value of attribute style.
17 18 19 |
# File 'lib/kindler.rb', line 17 def style @style end |
#title ⇒ Object
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(={}) add_page() end |
#add_page(options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kindler.rb', line 45 def add_page(={}) raise KindlerError.new('must provide title when add page') unless [:title] page = .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_path ⇒ Object
97 98 99 |
# File 'lib/kindler.rb', line 97 def book_path "#{tmp_dir}/#{title}.mobi" end |
#generate ⇒ Object
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
93 94 95 |
# File 'lib/kindler.rb', line 93 def generated? File.exist? book_path end |
#sectionize_pages ⇒ Object
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 |