Class: Prophecy::Book
- Inherits:
-
Object
- Object
- Prophecy::Book
- Defined in:
- lib/prophecy/book.rb
Instance Attribute Summary collapse
-
#assets_dir ⇒ Object
readonly
Returns the value of attribute assets_dir.
-
#author ⇒ Object
readonly
Returns the value of attribute author.
-
#book_atag ⇒ Object
readonly
Returns the value of attribute book_atag.
-
#bookid ⇒ Object
readonly
Returns the value of attribute bookid.
-
#build_dir ⇒ Object
readonly
Returns the value of attribute build_dir.
-
#chapter_layout ⇒ Object
readonly
Returns the value of attribute chapter_layout.
-
#chapter_number_format ⇒ Object
readonly
Returns the value of attribute chapter_number_format.
-
#chapters ⇒ Object
readonly
Returns the value of attribute chapters.
-
#compile_name ⇒ Object
readonly
Returns the value of attribute compile_name.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#contributors ⇒ Object
readonly
Returns the value of attribute contributors.
-
#cover_credit ⇒ Object
readonly
Returns the value of attribute cover_credit.
-
#cover_image ⇒ Object
readonly
Returns the value of attribute cover_image.
-
#creator ⇒ Object
readonly
Returns the value of attribute creator.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#ebook_isbn ⇒ Object
readonly
Returns the value of attribute ebook_isbn.
-
#edition ⇒ Object
readonly
Returns the value of attribute edition.
-
#exclude_assets ⇒ Object
readonly
Returns the value of attribute exclude_assets.
-
#file_as ⇒ Object
readonly
Returns the value of attribute file_as.
-
#include_assets ⇒ Object
readonly
Returns the value of attribute include_assets.
-
#lang ⇒ Object
readonly
Returns the value of attribute lang.
-
#lang_iso_639_2 ⇒ Object
readonly
Returns the value of attribute lang_iso_639_2.
-
#layouts_dir ⇒ Object
readonly
Returns the value of attribute layouts_dir.
-
#manifest ⇒ Object
readonly
Returns the value of attribute manifest.
-
#markdown_dir ⇒ Object
readonly
Returns the value of attribute markdown_dir.
-
#output_format ⇒ Object
readonly
Returns the value of attribute output_format.
-
#print_isbn ⇒ Object
readonly
Returns the value of attribute print_isbn.
-
#publisher ⇒ Object
readonly
Returns the value of attribute publisher.
-
#publisher_atag ⇒ Object
readonly
Returns the value of attribute publisher_atag.
-
#publisher_logo ⇒ Object
readonly
Returns the value of attribute publisher_logo.
-
#rights ⇒ Object
readonly
Returns the value of attribute rights.
-
#show_chapter_name ⇒ Object
readonly
Returns the value of attribute show_chapter_name.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
-
#subtitle ⇒ Object
readonly
Returns the value of attribute subtitle.
-
#template_dir ⇒ Object
readonly
Returns the value of attribute template_dir.
-
#tex_dir ⇒ Object
readonly
Returns the value of attribute tex_dir.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
-
#toc ⇒ Object
readonly
Returns the value of attribute toc.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
-
#xhtml_dir ⇒ Object
readonly
Returns the value of attribute xhtml_dir.
Instance Method Summary collapse
- #build_epub_mobi ⇒ Object
- #build_latex ⇒ Object
- #epub? ⇒ Boolean
- #format_dir(src) ⇒ Object
- #generate_build ⇒ Object
-
#initialize(config) ⇒ Book
constructor
A new instance of Book.
- #latex? ⇒ Boolean
- #mobi? ⇒ Boolean
- #personal_name_first(lastname_comma_name) ⇒ Object
- #render_navpoints ⇒ Object
Constructor Details
#initialize(config) ⇒ Book
Returns a new instance of Book.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/prophecy/book.rb', line 17 def initialize(config) @config = config.clone c = config @output_format = c['output_format'] || nil @title = c['title'] || "The Title" @subtitle = c['subtitle'] || nil = c['author'] || "The Author" @creator = c['creator'] || @file_as = c['file_as'] || @publisher = c['publisher'] || nil @publisher_atag = c['publisher_atag'] || nil @publisher_logo = c['publisher_logo'] || nil @book_atag = c['book_atag'] || nil @print_isbn = c['print_isbn'] || nil @ebook_isbn = c['ebook_isbn'] || nil @uuid = c['uuid'] || nil @version = c['version'] || 'v0.1' @edition = c['edition'] || nil @lang = c['lang'] || 'en-GB' @lang_iso_639_2 = c['lang_iso_639_2'] || @lang.downcase.sub(/-.*$/, '') @tex_dir = c['tex_dir'] || find_format_dir('tex') @markdown_dir = c['markdown_dir'] || find_format_dir('markdown') @xhtml_dir = c['xhtml_dir'] || find_format_dir('xhtml') @build_dir = c['build_dir'] || File.join('./build', @output_format) @assets_dir = c['assets_dir'] || find_assets_dir @template_dir = c['template_dir'] || find_template_dir @layouts_dir = c['layouts_dir'] || File.join(@assets_dir, 'layouts') @chapter_layout = c['chapter_layout'] || 'page.xhtml.erb' @include_assets = format_include_assets(c['include_assets']) @exclude_assets = format_exclude_assets(c['exclude_assets']) @toc = c['toc'] || nil @bookid = c['bookid'] || nil @rights = c['rights'] || nil @subject = c['subject'] || nil @source = c['source'] || nil @contributors = c['contributors'] || nil @cover_image = c['cover_image'] || nil @cover_credit = c['cover_credit'] || nil @date = c['date'] || Time.now.strftime("%Y-%m-%d") @show_chapter_name = c['show_chapter_name'] || nil @chapter_number_format = c['chapter_number_format'] || nil @compile_name = "#{self.author}-#{self.title}-#{Time.now.strftime("%FT%T")}".gsub(/[^a-zA-Z0-9-]/, '-').gsub(/--*/, '-') @manifest = nil @chapters = [] Chapter.section_name = config['section_names'] if config['section_names'] if @toc @toc.each do |ch| if ch.is_a?(String) ch = { 'src' => ch } end if ch.has_key?('target') if ch['target'].include?(@output_format) || ch['target'] == @output_format @chapters << Chapter.new(self, ch) end else c = Chapter.new(self, ch) @chapters << c unless c.render_path.nil? end end @navpoints = @chapters.flat_map{|c| c.navpoints} end end |
Instance Attribute Details
#assets_dir ⇒ Object (readonly)
Returns the value of attribute assets_dir.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def assets_dir @assets_dir end |
#author ⇒ Object (readonly)
Returns the value of attribute author.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def end |
#book_atag ⇒ Object (readonly)
Returns the value of attribute book_atag.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def book_atag @book_atag end |
#bookid ⇒ Object (readonly)
Returns the value of attribute bookid.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def bookid @bookid end |
#build_dir ⇒ Object (readonly)
Returns the value of attribute build_dir.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def build_dir @build_dir end |
#chapter_layout ⇒ Object (readonly)
Returns the value of attribute chapter_layout.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def chapter_layout @chapter_layout end |
#chapter_number_format ⇒ Object (readonly)
Returns the value of attribute chapter_number_format.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def chapter_number_format @chapter_number_format end |
#chapters ⇒ Object (readonly)
Returns the value of attribute chapters.
6 7 8 |
# File 'lib/prophecy/book.rb', line 6 def chapters @chapters end |
#compile_name ⇒ Object (readonly)
Returns the value of attribute compile_name.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def compile_name @compile_name end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'lib/prophecy/book.rb', line 6 def config @config end |
#contributors ⇒ Object (readonly)
Returns the value of attribute contributors.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def contributors @contributors end |
#cover_credit ⇒ Object (readonly)
Returns the value of attribute cover_credit.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def cover_credit @cover_credit end |
#cover_image ⇒ Object (readonly)
Returns the value of attribute cover_image.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def cover_image @cover_image end |
#creator ⇒ Object (readonly)
Returns the value of attribute creator.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def creator @creator end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def date @date end |
#ebook_isbn ⇒ Object (readonly)
Returns the value of attribute ebook_isbn.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def ebook_isbn @ebook_isbn end |
#edition ⇒ Object (readonly)
Returns the value of attribute edition.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def edition @edition end |
#exclude_assets ⇒ Object (readonly)
Returns the value of attribute exclude_assets.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def exclude_assets @exclude_assets end |
#file_as ⇒ Object (readonly)
Returns the value of attribute file_as.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def file_as @file_as end |
#include_assets ⇒ Object (readonly)
Returns the value of attribute include_assets.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def include_assets @include_assets end |
#lang ⇒ Object (readonly)
Returns the value of attribute lang.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def lang @lang end |
#lang_iso_639_2 ⇒ Object (readonly)
Returns the value of attribute lang_iso_639_2.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def lang_iso_639_2 @lang_iso_639_2 end |
#layouts_dir ⇒ Object (readonly)
Returns the value of attribute layouts_dir.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def layouts_dir @layouts_dir end |
#manifest ⇒ Object (readonly)
Returns the value of attribute manifest.
6 7 8 |
# File 'lib/prophecy/book.rb', line 6 def manifest @manifest end |
#markdown_dir ⇒ Object (readonly)
Returns the value of attribute markdown_dir.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def markdown_dir @markdown_dir end |
#output_format ⇒ Object (readonly)
Returns the value of attribute output_format.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def output_format @output_format end |
#print_isbn ⇒ Object (readonly)
Returns the value of attribute print_isbn.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def print_isbn @print_isbn end |
#publisher ⇒ Object (readonly)
Returns the value of attribute publisher.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def publisher @publisher end |
#publisher_atag ⇒ Object (readonly)
Returns the value of attribute publisher_atag.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def publisher_atag @publisher_atag end |
#publisher_logo ⇒ Object (readonly)
Returns the value of attribute publisher_logo.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def publisher_logo @publisher_logo end |
#rights ⇒ Object (readonly)
Returns the value of attribute rights.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def rights @rights end |
#show_chapter_name ⇒ Object (readonly)
Returns the value of attribute show_chapter_name.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def show_chapter_name @show_chapter_name end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def source @source end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def subject @subject end |
#subtitle ⇒ Object (readonly)
Returns the value of attribute subtitle.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def subtitle @subtitle end |
#template_dir ⇒ Object (readonly)
Returns the value of attribute template_dir.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def template_dir @template_dir end |
#tex_dir ⇒ Object (readonly)
Returns the value of attribute tex_dir.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def tex_dir @tex_dir end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def title @title end |
#toc ⇒ Object (readonly)
Returns the value of attribute toc.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def toc @toc end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def uuid @uuid end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def version @version end |
#xhtml_dir ⇒ Object (readonly)
Returns the value of attribute xhtml_dir.
8 9 10 |
# File 'lib/prophecy/book.rb', line 8 def xhtml_dir @xhtml_dir end |
Instance Method Details
#build_epub_mobi ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/prophecy/book.rb', line 129 def build_epub_mobi FileUtils.cp_r(File.join(@template_dir, '.'), @build_dir) FileUtils.cp_r(@include_assets, File.join(@build_dir, 'OEBPS')) content_opf_path = nil # For ERB binding. book = self Dir.glob(File.join(@build_dir, '**/*'), File::FNM_DOTMATCH).each do |f| if File.extname(f) == '.erb' if File.basename(f) == 'content.opf.erb' content_opf_path = File.(f) next end template = ERB.new(IO.read(f)) text = template.result(binding) File.open(f.sub(/\.erb$/, ''), "w"){|file| file << text } FileUtils.rm(f) end @exclude_assets.each do |ex| if File.fnmatch(ex, f) || File.fnmatch(ex, File.basename(f)) FileUtils.rm(f) end end end @chapters.each do |ch| File.open(ch.render_path, "w"){|f| f << ch.to_html } end @manifest = Manifest.new(self) # Rendering content.opf.erb at the end. unless content_opf_path.nil? template = ERB.new(IO.read(content_opf_path)) text = template.result(binding) File.open(content_opf_path.sub(/\.erb$/, ''), "w"){|file| file << text } FileUtils.rm(content_opf_path) end end |
#build_latex ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/prophecy/book.rb', line 99 def build_latex FileUtils.cp_r(File.join(@template_dir, '.'), @build_dir) FileUtils.cp_r(@include_assets, @build_dir) # For ERB binding. book = self Dir.glob(File.join(@build_dir, '**/*'), File::FNM_DOTMATCH).each do |f| if File.extname(f) == '.erb' template = ERB.new(IO.read(f)) text = template.result(binding) File.open(f.sub(/\.erb$/, ''), "w"){|file| file << text } FileUtils.rm(f) end @exclude_assets.each do |ex| if File.fnmatch(ex, f) || File.fnmatch(ex, File.basename(f)) FileUtils.rm(f) end end end @chapters.each do |ch| next if ch.src.nil? unless File.extname(ch.src) == '.tex' File.open(ch.render_path, "w"){|f| f << ch.to_tex } end end end |
#epub? ⇒ Boolean
210 211 212 |
# File 'lib/prophecy/book.rb', line 210 def epub? self.output_format == 'epub' end |
#format_dir(src) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/prophecy/book.rb', line 172 def format_dir(src) case File.extname(src) when '.md', '.mkd', '.markdown' self.markdown_dir when '.tex' self.tex_dir when '.xhtml' self.xhtml_dir when '.html' self.xhtml_dir when '.erb' self.format_dir(src.sub(/\.erb$/, '')) end end |
#generate_build ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/prophecy/book.rb', line 85 def generate_build case self.output_format when 'epub' self.build_epub_mobi when 'mobi' self.build_epub_mobi when 'latex' self.build_latex else warn "Don't know how to build output format: " + self.output_format raise "Unknown Output Format Error" end end |
#latex? ⇒ Boolean
218 219 220 |
# File 'lib/prophecy/book.rb', line 218 def latex? self.output_format == 'latex' end |
#mobi? ⇒ Boolean
214 215 216 |
# File 'lib/prophecy/book.rb', line 214 def mobi? self.output_format == 'mobi' end |
#personal_name_first(lastname_comma_name) ⇒ Object
205 206 207 208 |
# File 'lib/prophecy/book.rb', line 205 def personal_name_first(lastname_comma_name) n = lastname_comma_name.split(/, */) "#{n[1]} #{n[0]}" end |
#render_navpoints ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/prophecy/book.rb', line 187 def render_navpoints ret = "" @navpoints.each_with_index do |nav, idx| ret += "<navPoint id='nav#{nav['playOrder']}' playOrder='#{nav['playOrder']}'>\n" ret += "<navLabel><text>#{nav['text']}</text></navLabel>\n" ret += "<content src='#{nav['src']}'/>" next_nav = @navpoints[idx+1] if !next_nav.nil? && next_nav['level'] < nav['level'] d = nav['level'] - next_nav['level'] + 1 d.times{ ret += "</navPoint>\n" } elsif next_nav.nil? || next_nav['level'] == nav['level'] ret += "</navPoint>\n" end end ret end |