Class: Prophecy::Book

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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
84
# 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
  @author         = c['author']         || "The Author"
  @creator        = c['creator']        || @author
  @file_as        = c['file_as']        || @author
  @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
  @show_toc_chapter_numbers = c['show_toc_chapter_numbers'] || true
  @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_dirObject (readonly)

Returns the value of attribute assets_dir.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def assets_dir
  @assets_dir
end

#authorObject (readonly)

Returns the value of attribute author.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def author
  @author
end

#book_atagObject (readonly)

Returns the value of attribute book_atag.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def book_atag
  @book_atag
end

#bookidObject (readonly)

Returns the value of attribute bookid.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def bookid
  @bookid
end

#build_dirObject (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_layoutObject (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_formatObject (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

#chaptersObject (readonly)

Returns the value of attribute chapters.



6
7
8
# File 'lib/prophecy/book.rb', line 6

def chapters
  @chapters
end

#compile_nameObject (readonly)

Returns the value of attribute compile_name.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def compile_name
  @compile_name
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/prophecy/book.rb', line 6

def config
  @config
end

#contributorsObject (readonly)

Returns the value of attribute contributors.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def contributors
  @contributors
end

#cover_creditObject (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_imageObject (readonly)

Returns the value of attribute cover_image.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def cover_image
  @cover_image
end

#creatorObject (readonly)

Returns the value of attribute creator.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def creator
  @creator
end

#dateObject (readonly)

Returns the value of attribute date.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def date
  @date
end

#ebook_isbnObject (readonly)

Returns the value of attribute ebook_isbn.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def ebook_isbn
  @ebook_isbn
end

#editionObject (readonly)

Returns the value of attribute edition.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def edition
  @edition
end

#exclude_assetsObject (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_asObject (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_assetsObject (readonly)

Returns the value of attribute include_assets.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def include_assets
  @include_assets
end

#langObject (readonly)

Returns the value of attribute lang.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def lang
  @lang
end

#lang_iso_639_2Object (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_dirObject (readonly)

Returns the value of attribute layouts_dir.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def layouts_dir
  @layouts_dir
end

#manifestObject (readonly)

Returns the value of attribute manifest.



6
7
8
# File 'lib/prophecy/book.rb', line 6

def manifest
  @manifest
end

#markdown_dirObject (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_formatObject (readonly)

Returns the value of attribute output_format.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def output_format
  @output_format
end

Returns the value of attribute print_isbn.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def print_isbn
  @print_isbn
end

#publisherObject (readonly)

Returns the value of attribute publisher.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def publisher
  @publisher
end

#publisher_atagObject (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_logoObject (readonly)

Returns the value of attribute publisher_logo.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def 
  @publisher_logo
end

#rightsObject (readonly)

Returns the value of attribute rights.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def rights
  @rights
end

#show_chapter_nameObject (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

#show_toc_chapter_numbersObject (readonly)

Returns the value of attribute show_toc_chapter_numbers.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def show_toc_chapter_numbers
  @show_toc_chapter_numbers
end

#sourceObject (readonly)

Returns the value of attribute source.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def source
  @source
end

#subjectObject (readonly)

Returns the value of attribute subject.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def subject
  @subject
end

#subtitleObject (readonly)

Returns the value of attribute subtitle.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def subtitle
  @subtitle
end

#template_dirObject (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_dirObject (readonly)

Returns the value of attribute tex_dir.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def tex_dir
  @tex_dir
end

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def title
  @title
end

#tocObject (readonly)

Returns the value of attribute toc.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def toc
  @toc
end

#uuidObject (readonly)

Returns the value of attribute uuid.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def uuid
  @uuid
end

#versionObject (readonly)

Returns the value of attribute version.



8
9
10
# File 'lib/prophecy/book.rb', line 8

def version
  @version
end

#xhtml_dirObject (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_mobiObject



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
171
# File 'lib/prophecy/book.rb', line 130

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.expand_path(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_latexObject



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
128
# File 'lib/prophecy/book.rb', line 100

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

Returns:

  • (Boolean)


211
212
213
# File 'lib/prophecy/book.rb', line 211

def epub?
  self.output_format == 'epub'
end

#format_dir(src) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/prophecy/book.rb', line 173

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_buildObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/prophecy/book.rb', line 86

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

Returns:

  • (Boolean)


219
220
221
# File 'lib/prophecy/book.rb', line 219

def latex?
  self.output_format == 'latex'
end

#mobi?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/prophecy/book.rb', line 215

def mobi?
  self.output_format == 'mobi'
end

#personal_name_first(lastname_comma_name) ⇒ Object



206
207
208
209
# File 'lib/prophecy/book.rb', line 206

def personal_name_first(lastname_comma_name)
  n = lastname_comma_name.split(/, */)
  "#{n[1]} #{n[0]}"
end

#render_navpointsObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/prophecy/book.rb', line 188

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