Class: Epuber::BookspecGenerator
- Inherits:
-
Object
- Object
- Epuber::BookspecGenerator
- Defined in:
- lib/epuber/from_file/bookspec_generator.rb
Defined Under Namespace
Classes: TocItem
Instance Method Summary collapse
- #format_author(author_node) ⇒ String
- #generate_authors ⇒ void
- #generate_bookspec ⇒ String
- #generate_cover ⇒ void
- #generate_id ⇒ void
- #generate_language ⇒ void
- #generate_published ⇒ void
- #generate_publisher ⇒ void
- #generate_titles ⇒ void
- #generate_toc ⇒ void
-
#initialize(opf, nav) ⇒ BookspecGenerator
constructor
A new instance of BookspecGenerator.
Constructor Details
#initialize(opf, nav) ⇒ BookspecGenerator
Returns a new instance of BookspecGenerator.
57 58 59 60 |
# File 'lib/epuber/from_file/bookspec_generator.rb', line 57 def initialize(opf, nav) @opf = opf @nav = nav end |
Instance Method Details
#format_author(author_node) ⇒ String
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/epuber/from_file/bookspec_generator.rb', line 116 def () name = .text.strip id = ['id'] if id role = @opf.find_refines(id, 'role') file_as = @opf.find_refines(id, 'file-as') end role ||= ['opf:role'] file_as ||= ['opf:file-as'] role_is_default = role.nil? || role == 'aut' file_as_is_default = file_as.nil? || contributor_file_as_eq?(Book::Contributor.from_obj(name).file_as, file_as) if role_is_default && file_as_is_default name.inspect elsif role_is_default && !file_as_is_default %({ pretty_name: #{name.inspect}, file_as: #{file_as.inspect} }) elsif !role_is_default && file_as_is_default %({ name: #{name.inspect}, role: #{role.inspect} }) else %({ pretty_name: #{name.inspect}, file_as: #{file_as.inspect}, role: #{role.inspect} }) end end |
#generate_authors ⇒ void
This method returns an undefined value.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/epuber/from_file/bookspec_generator.rb', line 103 def = @opf..css('creator') if .empty? add_setting_property(:author, nil) elsif .count == 1 add_setting_property(:author, (.first)) else add_setting_property(:authors, .map { |auth| (auth) }) end end |
#generate_bookspec ⇒ String
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/epuber/from_file/bookspec_generator.rb', line 64 def generate_bookspec @indent = 0 @bookspec = [] add_code('Epuber::Book.new do |book|', after: 'end') do generate_titles add_empty_line generate_id generate_language generate_published generate_publisher add_empty_line generate_cover add_empty_line generate_toc end add_empty_line @bookspec.join("\n") end |
#generate_cover ⇒ void
This method returns an undefined value.
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/epuber/from_file/bookspec_generator.rb', line 187 def generate_cover cover_property = Compiler::OPFGenerator::PROPERTIES_MAP[:cover_image] cover_id = @opf.manifest_items.find { |_, item| item.properties&.include?(cover_property) }&.last&.id cover_id ||= @opf..at_css('meta[name="cover"]')&.[]('content') cover = @opf.manifest_file_by_id(cover_id) if cover_id return unless cover href = cover.href.sub(/#{Regexp.escape(File.extname(cover.href))}$/, '') add_setting_property(:cover_image, href.inspect) end |
#generate_id ⇒ void
This method returns an undefined value.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/epuber/from_file/bookspec_generator.rb', line 144 def generate_id nodes = @opf..css('identifier') nodes.each do |id_node| value = id_node.text.strip is_main = @opf.package['unique-identifier'] == id_node['id'] is_isbn = value.start_with?('urn:isbn:') value = value.sub(/^urn:isbn:/, '').strip if is_isbn key = is_isbn ? :isbn : :identifier if is_main add_setting_property(key, value.inspect) else add_comment('alternate identifier found from original EPUB file (Epuber supports only one identifier)') add_setting_property(key, value.inspect, commented: true) end end end |
#generate_language ⇒ void
This method returns an undefined value.
166 167 168 169 |
# File 'lib/epuber/from_file/bookspec_generator.rb', line 166 def generate_language language = @opf..at_css('language') add_setting_property(:language, language.text.strip.inspect) if language end |
#generate_published ⇒ void
This method returns an undefined value.
173 174 175 176 |
# File 'lib/epuber/from_file/bookspec_generator.rb', line 173 def generate_published published = @opf..at_css('date') add_setting_property(:published, published.text.strip.inspect) if published end |
#generate_publisher ⇒ void
This method returns an undefined value.
180 181 182 183 |
# File 'lib/epuber/from_file/bookspec_generator.rb', line 180 def generate_publisher publisher = @opf..at_css('publisher') add_setting_property(:publisher, publisher.text.strip.inspect) if publisher end |
#generate_titles ⇒ void
This method returns an undefined value.
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/epuber/from_file/bookspec_generator.rb', line 88 def generate_titles titles = @opf..css('title') titles.each do |title| is_main = titles.count == 1 id = title['id'] is_main = @opf.find_refines(id, 'title-type') == 'main' if id && !is_main add_comment('alternate title found from original EPUB file (Epuber supports only one title)') unless is_main add_setting_property(:title, title.text.inspect, commented: !is_main) end end |
#generate_toc ⇒ void
This method returns an undefined value.
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/epuber/from_file/bookspec_generator.rb', line 201 def generate_toc items = calculate_toc_items render_toc_item = lambda do |item| if item.children.empty? add_code(%(toc.file #{item.attribs})) else add_code(%(toc.file #{item.attribs} do), after: 'end') do item.children.each do |child| render_toc_item.call(child) end end end end add_code('book.toc do |toc, target|', after: 'end') do items.each do |toc_item| render_toc_item.call(toc_item) end end end |