Class: Epuber::Compiler::XHTMLProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/compiler/xhtml_processor.rb

Defined Under Namespace

Classes: UnparseableLinkError

Class Method Summary collapse

Class Method Details

.add_missing_root_elements(xhtml_doc, title, epub_version) ⇒ Object

Method to add all missing items in XML root

Required items:

- html (with all namespaces and other attributes)
- body
  - head (with title)

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)

    input XML document to work with

  • title (String)

    title of this document, since this is required by EPUB specification

  • epub_version (Epuber::Version)

    version of result EPUB

Returns:

  • nil



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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/epuber/compiler/xhtml_processor.rb', line 103

def self.add_missing_root_elements(xhtml_doc, title, epub_version)
  # add missing body element
  if xhtml_doc.at_css('body').nil?
    if xhtml_doc.root.node_name == 'html'
      xhtml_doc.root << xhtml_doc.create_element('body')
    else
      xhtml_doc.root.surround_with_element('body')
    end
  end

  html = xhtml_doc.at_css('html')

  # add missing root html element
  if html.nil?
    attrs               = {}
    attrs['xmlns']      = 'http://www.w3.org/1999/xhtml'
    attrs['xmlns:epub'] = 'http://www.idpf.org/2007/ops' if epub_version >= 3
    html = xhtml_doc.root.surround_with_element('html', attrs)
  elsif html.namespaces.empty?
    html['xmlns']      = 'http://www.w3.org/1999/xhtml'
    html['xmlns:epub'] = 'http://www.idpf.org/2007/ops' if epub_version >= 3
  end

  # add missing head in html
  if xhtml_doc.at_css('html > head').nil?
    head = xhtml_doc.create_element('head')
    head << xhtml_doc.create_element('title', title)
    head << xhtml_doc.create_element('meta', charset: 'utf-8') if epub_version >= 3.0

    if (first = html.children.first)
      first.before(head)
    else
      html << head
    end
  end

  # https://github.com/IDPF/epubcheck/issues/631
  return unless epub_version < 3.0

  xhtml_doc.internal_subset&.remove
  xhtml_doc.create_internal_subset('html', '-//W3C//DTD XHTML 1.1//EN', 'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd')
end

.add_scripts(xhtml_doc, scripts) ⇒ Object

Method for adding scripts with links, method will not add duplicate items

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)

    input XML document to work with

  • styles (Array<String>)

    links to files

Returns:

  • nil



171
172
173
174
175
176
177
178
179
180
# File 'lib/epuber/compiler/xhtml_processor.rb', line 171

def self.add_scripts(xhtml_doc, scripts)
  head = xhtml_doc.at_css('html > head')
  old_links = head.css('script').map { |node| node['src'] }

  links_to_add = scripts - old_links

  links_to_add.each do |path|
    head << xhtml_doc.create_element('script', src: path, type: 'text/javascript')
  end
end

.add_styles(xhtml_doc, styles) ⇒ Object

Method for adding style sheets with links, method will not add duplicate items

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)

    input XML document to work with

  • styles (Array<String>)

    links to files

Returns:

  • nil



153
154
155
156
157
158
159
160
161
162
# File 'lib/epuber/compiler/xhtml_processor.rb', line 153

def self.add_styles(xhtml_doc, styles)
  head = xhtml_doc.at_css('html > head')
  old_links = head.css('link[rel="stylesheet"]').map { |node| node['href'] }

  links_to_add = styles - old_links

  links_to_add.each do |path|
    head << xhtml_doc.create_element('link', href: path, rel: 'stylesheet', type: 'text/css')
  end
end

.add_viewport(xhtml_doc, viewport_size) ⇒ Object

Adds viewport meta tag to head of some document, but only if there is not some existing tag

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)
  • viewport_size (Epuber::Size)


187
188
189
190
191
192
193
# File 'lib/epuber/compiler/xhtml_processor.rb', line 187

def self.add_viewport(xhtml_doc, viewport_size)
  head = xhtml_doc.at_css('html > head')
  return unless head.at_css("meta[name='viewport']").nil?

  s = viewport_size
  head << xhtml_doc.create_element('meta', name: 'viewport', content: "width=#{s.width},height=#{s.height}")
end

.find_global_ids(xhtml_doc) ⇒ Array<string>

Returns list of global ids (without dollar signs).

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)

    input XML document to work with

Returns:

  • (Array<string>)

    list of global ids (without dollar signs)



371
372
373
374
# File 'lib/epuber/compiler/xhtml_processor.rb', line 371

def self.find_global_ids(xhtml_doc)
  find_global_ids_nodes(xhtml_doc)
    .map { |node| node['id'][1..-1] }
end

.find_global_ids_nodes(xhtml_doc) ⇒ Array<Nokogiri::XML::Node>

Returns list of nodes with global ids.

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)

    input XML document to work with

Returns:



363
364
365
366
# File 'lib/epuber/compiler/xhtml_processor.rb', line 363

def self.find_global_ids_nodes(xhtml_doc)
  xhtml_doc
    .css('[id^="$"]')
end

Returns list of global ids (without dollar signs).

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)

    input XML document to work with

Returns:

  • (Array<string>)

    list of global ids (without dollar signs)



387
388
389
390
# File 'lib/epuber/compiler/xhtml_processor.rb', line 387

def self.find_global_links(xhtml_doc)
  find_global_links_nodes(xhtml_doc)
    .map { |node| node['href'][1..-1] }
end

Returns list of nodes with global links.

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)

    input XML document to work with

Returns:



379
380
381
382
# File 'lib/epuber/compiler/xhtml_processor.rb', line 379

def self.find_global_links_nodes(xhtml_doc)
  xhtml_doc
    .css('[href^="$"]')
end

.resolve_images(xhtml_doc, file_path, file_resolver) ⇒ Object

Returns nil.

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)
  • file_path (String)

    path of referring file

  • file_resolver (FileResolver)

Returns:

  • nil



322
323
324
# File 'lib/epuber/compiler/xhtml_processor.rb', line 322

def self.resolve_images(xhtml_doc, file_path, file_resolver)
  resolve_resources_in('img', 'src', :image, xhtml_doc, file_path, file_resolver)
end

Resolves all links to files in XHTML document and returns the valid and resolved versions

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)

    input XML document to work with

  • file_path (String)

    path to file from which is searching for other file

  • file_finder (Epuber::Compiler::FileFinder)

    finder for searching for files

Returns:

  • (Array<URI>)

    resolved links



278
279
280
281
282
283
# File 'lib/epuber/compiler/xhtml_processor.rb', line 278

def self.resolve_links(xhtml_doc, file_path, file_finder)
  [
    resolve_links_for(xhtml_doc, 'a', 'href', :text, file_path, file_finder),
    resolve_links_for(xhtml_doc, 'map > area', 'href', :text, file_path, file_finder),
  ].flatten
end

Resolves all links to files in XHTML document and returns the valid and resolved versions

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)

    input XML document to work with

  • tag_name (String)

    CSS selector for tag

  • attribute_name (String)

    name of attribute

  • groups (Symbol | Array<Symbol>)

    groups of the searching file, could be for example :image when searching for file from tag <img>

  • file_path (String)

    path to file from which is searching for other file

  • file_finder (Epuber::Compiler::FileFinder)

    finder for searching for files

Returns:

  • (Array<URI>)

    resolved links



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/epuber/compiler/xhtml_processor.rb', line 246

def self.resolve_links_for(xhtml_doc, tag_name, attribute_name, groups, file_path, file_finder)
  founded_links = []

  xhtml_doc.css("#{tag_name}[#{attribute_name}]").each do |node|
    src = node[attribute_name]
    # @type [String] src

    next if src.nil?
    next if src.start_with?('$')

    target_file = resolved_link_to_file(src, groups, file_path, file_finder)
    founded_links << target_file

    node[attribute_name] = target_file.to_s
  rescue UnparseableLinkError, FileFinders::FileNotFoundError, FileFinders::MultipleFilesFoundError => e
    UI.warning(e.to_s, location: node)

    # skip not found files
    next
  end

  founded_links
end

.resolve_mathml_namespace(xhtml_doc) ⇒ Object



310
311
312
313
314
# File 'lib/epuber/compiler/xhtml_processor.rb', line 310

def self.resolve_mathml_namespace(xhtml_doc)
  xhtml_doc.css('math').each do |math_node|
    math_node.add_namespace('xmlns', 'http://www.w3.org/1998/Math/MathML')
  end
end

.resolve_resources_in(node_css_query, attribute_name, resource_group, xhtml_doc, file_path, file_resolver) ⇒ Object



346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/epuber/compiler/xhtml_processor.rb', line 346

def self.resolve_resources_in(node_css_query, attribute_name, resource_group, xhtml_doc, file_path, file_resolver)
  xhtml_doc.css(node_css_query).each do |img|
    path = img[attribute_name]
    next if path.nil?

    new_path = Compiler::FileTypes::SourceFile.resolve_relative_file(file_path,
                                                                     path,
                                                                     file_resolver,
                                                                     group: resource_group,
                                                                     location: img)
    img[attribute_name] = new_path if new_path
  end
end

.resolve_scripts(xhtml_doc, file_path, file_resolver) ⇒ Object

Returns nil.

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)
  • file_path (String)

    path of referring file

  • file_resolver (FileResolver)

Returns:

  • nil



332
333
334
# File 'lib/epuber/compiler/xhtml_processor.rb', line 332

def self.resolve_scripts(xhtml_doc, file_path, file_resolver)
  resolve_resources_in('script', 'src', :script, xhtml_doc, file_path, file_resolver)
end

.resolve_stylesheets(xhtml_doc, file_path, file_resolver) ⇒ Object

Returns nil.

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)
  • file_path (String)

    path of referring file

  • file_resolver (FileResolver)

Returns:

  • nil



342
343
344
# File 'lib/epuber/compiler/xhtml_processor.rb', line 342

def self.resolve_stylesheets(xhtml_doc, file_path, file_resolver)
  resolve_resources_in('link[rel="stylesheet"]', 'href', :style, xhtml_doc, file_path, file_resolver)
end

Method which will resolve path to file from pattern

Parameters:

  • path (String)

    pattern or path of the file

  • groups (Symbol | Array<Symbol>)

    groups of the searching file, could be for example :image when searching for file from tag <img>

  • file_path (String)

    path to file from which is searching for other file

  • file_finder (Epuber::Compiler::FileFinders::Abstract)

    finder for searching for files

Returns:

  • (URI)

    resolved path to file or remote web page

Raises:

  • UnparseableLinkError, FileFinder::FileNotFoundError, FileFinder::MultipleFilesFoundError



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/epuber/compiler/xhtml_processor.rb', line 207

def self.resolved_link_to_file(path, groups, file_path, file_finder)
  raise FileFinders::FileNotFoundError.new(path, file_path) if path.empty?

  begin
    uri = URI(path)
  rescue URI::InvalidURIError
    begin
      uri = URI(Addressable::URI.encode(path))
    rescue URI::InvalidURIError
      # skip not valid uri
      raise UnparseableLinkError, "Unparseable link `#{path}`"
    end
  end

  return uri if path == '#'

  # skip uri with scheme (links to web pages)
  return uri unless uri.scheme.nil?

  # skip empty path
  return uri if uri.path.empty? && !uri.fragment.nil? && !uri.fragment.empty?

  uri.path = file_finder.find_file(uri.path, groups: groups, context_path: file_path)

  uri
end

.using_javascript?(xhtml_doc) ⇒ Bool

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)

    input XML document to work with

Returns:

  • (Bool)


289
290
291
# File 'lib/epuber/compiler/xhtml_processor.rb', line 289

def self.using_javascript?(xhtml_doc)
  !xhtml_doc.at_css('script').nil?
end

.using_mathml?(xhtml_doc) ⇒ Bool

Parameters:

  • xhtml_doc (Nokogiri::XML::Document)

    input XML document to work with

Returns:

  • (Bool)


306
307
308
# File 'lib/epuber/compiler/xhtml_processor.rb', line 306

def self.using_mathml?(xhtml_doc)
  !xhtml_doc.at_css('math|math', 'math' => 'http://www.w3.org/1998/Math/MathML').nil?
end

.using_remote_resources?(xhtml_doc) ⇒ Boolean

Returns:

  • (Boolean)


293
294
295
296
297
298
299
300
# File 'lib/epuber/compiler/xhtml_processor.rb', line 293

def self.using_remote_resources?(xhtml_doc)
  regexp = %r{^[^:/?#]+://.*}

  result = false
  result ||= xhtml_doc.css('[src]').any? { |node| node['src'] =~ regexp }
  result ||= xhtml_doc.css('link[href]').any? { |node| node['href'] =~ regexp }
  result
end

.xml_doc_from_str_with_errors(text, file_path = nil) ⇒ Nokogiri::XML::Document

Method for parsing incomplete XML, supports multiple root elements

‘body`, since it will be used in next steps.

Parameters:

  • text (String)

    input XHTML text

Returns:

  • (Nokogiri::XML::Document)

    parsed document



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/epuber/compiler/xhtml_processor.rb', line 23

def self.xml_doc_from_str_with_errors(text, file_path = nil)
  text = text.dup

  if /\A[\n\r ]+(<\?xml)/ =~ text
    UI.warning('XML header must be at the beginning of document',
               location: Epuber::Location.new(path: file_path, lineno: 1))

    text = text.lstrip
  end

  xml_header = nil
  if /\A\s*(<\?xml[^>]*\?>)/ =~ text
    match = Regexp.last_match
    xml_header = text[match.begin(1)...match.end(1)]
    text[match.begin(1)...match.end(1)] = ''
  end

  doctypes = []
  while /(\n|\?>|\A)?(<!DOCTYPE [^>]*>\n*)/ =~ text
    doctypes << ::Regexp.last_match(2).strip

    match = Regexp.last_match
    text[match.begin(2)...match.end(2)] = ''
  end

  before = ([xml_header] + doctypes).compact.join("\n")
  before += "\n" unless before.empty?

  parse_options = Nokogiri::XML::ParseOptions::DEFAULT_XML |
                  Nokogiri::XML::ParseOptions::NOERROR | # to silence any errors or warnings printing into console
                  Nokogiri::XML::ParseOptions::NOWARNING |
                  Nokogiri::XML::ParseOptions::NOENT

  doc = Nokogiri::XML("#{before}<root>#{text}</root>", file_path, nil, parse_options)
  text_for_errors = before + text
  doc.encoding = 'UTF-8'
  doc.file_path = file_path

  if doc.errors.empty?
    errors = []
  else
    errors = doc.errors.map do |e|
      Problem.new(:error, e.message, text_for_errors, line: e.line, column: e.column, file_path: file_path)
    end
  end

  root = root_node = doc.root
  root_elements = root.children.select { |a| a.element? || a.comment? }

  if root_elements.count == 1
    doc.root = root_elements.first
  elsif root_node.at_css('html')
    doc.root = root_node.at_css('html')
  elsif root_node.at_css('body').nil?
    root_node.node_name = 'body'
  else
    root_node.node_name = 'html'
  end

  [doc, errors]
end

.xml_document_from_string(text, file_path = nil) ⇒ Object



85
86
87
88
# File 'lib/epuber/compiler/xhtml_processor.rb', line 85

def self.xml_document_from_string(text, file_path = nil)
  xml, = xml_doc_from_str_with_errors(text, file_path)
  xml
end