Class: Epuber::BookspecGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/from_file/bookspec_generator.rb

Defined Under Namespace

Classes: TocItem

Instance Method Summary collapse

Constructor Details

#initialize(opf, nav) ⇒ BookspecGenerator

Returns a new instance of BookspecGenerator.

Parameters:



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

Returns:

  • (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 format_author(author_node)
  name = author_node.text.strip
  id = author_node['id']

  if id
    role = @opf.find_refines(id, 'role')
    file_as = @opf.find_refines(id, 'file-as')
  end

  role ||= author_node['opf:role']
  file_as ||= author_node['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_authorsvoid

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 generate_authors
  authors = @opf..css('creator')
  if authors.empty?
    add_setting_property(:author, nil)
  elsif authors.count == 1
    add_setting_property(:author, format_author(authors.first))
  else
    add_setting_property(:authors, authors.map { |auth| format_author(auth) })
  end
end

#generate_bookspecString

Returns:

  • (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
    generate_authors
    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_covervoid

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_idvoid

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_languagevoid

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_publishedvoid

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_publishervoid

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_titlesvoid

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_tocvoid

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