Class: Epuber::Compiler::FileTypes::XHTMLFile

Inherits:
SourceFile show all
Defined in:
lib/epuber/compiler/file_types/xhtml_file.rb

Direct Known Subclasses

BadeFile

Instance Attribute Summary collapse

Attributes inherited from SourceFile

#abs_source_path, #file_request, #source_path

Attributes inherited from AbstractFile

#compilation_context, #destination_path, #final_destination_path, #group, #path_type, #pkg_destination_path, #properties

Instance Method Summary collapse

Methods inherited from SourceFile

#default_file_copy, #destination_file_exist?, #destination_file_up_to_date?, #find_dependencies, #initialize, #source_file_up_to_date?, #update_metadata!, #write_compiled, #write_processed

Methods inherited from AbstractFile

#==, file_copy!, write_to_file, write_to_file!, write_to_file?

Constructor Details

This class inherits a constructor from Epuber::Compiler::FileTypes::SourceFile

Instance Attribute Details

#toc_itemEpuber::Book::TocItem



13
14
15
# File 'lib/epuber/compiler/file_types/xhtml_file.rb', line 13

def toc_item
  @toc_item
end

Instance Method Details

#common_process(content, compilation_context) ⇒ String

Returns new xhtml string.

Parameters:

Returns:

  • (String)

    new xhtml string



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/epuber/compiler/file_types/xhtml_file.rb', line 82

def common_process(content, compilation_context)
  target = compilation_context.target
  book = compilation_context.book
  file_resolver = compilation_context.file_resolver

  xhtml_doc, errors = UI.print_step_processing_time('parsing XHTML file') do
    XHTMLProcessor.xml_doc_from_str_with_errors(content, source_path)
  end

  if compilation_context.release_build && xhtml_doc.errors.count > 0
    process_nokogiri_errors(errors)
  end

  UI.print_step_processing_time('adding missing elements') do
    XHTMLProcessor.add_missing_root_elements(xhtml_doc, book.title, target.epub_version)
  end

  # resolve links to files, add other linked resources and compute correct path
  UI.print_step_processing_time('resolving links') do
    XHTMLProcessor.resolve_links(xhtml_doc, destination_path, file_resolver.dest_finder)
  end
  UI.print_step_processing_time('resolving image resources') do
    XHTMLProcessor.resolve_images(xhtml_doc, destination_path, file_resolver)
  end
  UI.print_step_processing_time('resolving scripts') do
    XHTMLProcessor.resolve_scripts(xhtml_doc, destination_path, file_resolver)
  end
  UI.print_step_processing_time('resolving stylesheets') do
    XHTMLProcessor.resolve_stylesheets(xhtml_doc, destination_path, file_resolver)
  end

  UI.print_step_processing_time('adding default things') do
    XHTMLProcessor.add_styles(xhtml_doc, default_styles(target, file_resolver))
    XHTMLProcessor.add_scripts(xhtml_doc, default_scripts(target, file_resolver))

    XHTMLProcessor.add_viewport(xhtml_doc, target.default_viewport) unless target.default_viewport.nil?
  end

  XHTMLProcessor.resolve_mathml_namespace(xhtml_doc)

  UI.print_step_processing_time('investigating properties') do
    self.properties << :remote_resources if XHTMLProcessor.using_remote_resources?(xhtml_doc)
    self.properties << :scripted if XHTMLProcessor.using_javascript?(xhtml_doc)
    self.properties << :mathml if XHTMLProcessor.using_mathml?(xhtml_doc)
  end

  xhtml_string = UI.print_step_processing_time('converting to XHTML') do
     xhtml_doc.to_s
  end

  # perform transformations
  UI.print_step_processing_time('performing final transformations') do
    compilation_context.perform_plugin_things(Transformer, :result_text_xhtml_string) do |transformer|
      xhtml_string = transformer.call(final_destination_path, xhtml_string, compilation_context)
    end
  end

  # perform custom validation
  if compilation_context.should_check
    UI.print_step_processing_time('performing final validations') do
      compilation_context.perform_plugin_things(Checker, :result_text_xhtml_string) do |checker|
        checker.call(final_destination_path, xhtml_string, compilation_context)
      end
    end
  end

  xhtml_string
end

#default_scripts(target, file_resolver) ⇒ Array<String>

Returns list of paths to styles relative to this file.

Parameters:

Returns:

  • (Array<String>)

    list of paths to styles relative to this file



35
36
37
38
39
40
41
42
43
# File 'lib/epuber/compiler/file_types/xhtml_file.rb', line 35

def default_scripts(target, file_resolver)
  default_scripts = target.default_scripts.map do |default_style_request|
    Array(file_resolver.file_from_request(default_style_request))
  end.flatten

  default_scripts.map do |style|
    Pathname.new(style.final_destination_path).relative_path_from(Pathname.new(File.dirname(final_destination_path))).to_s
  end
end

#default_styles(target, file_resolver) ⇒ Array<String>

Returns list of paths to styles relative to this file.

Parameters:

Returns:

  • (Array<String>)

    list of paths to styles relative to this file



20
21
22
23
24
25
26
27
28
# File 'lib/epuber/compiler/file_types/xhtml_file.rb', line 20

def default_styles(target, file_resolver)
  default_styles = target.default_styles.map do |default_style_request|
    Array(file_resolver.file_from_request(default_style_request))
  end.flatten

  default_styles.map do |style|
    Pathname.new(style.final_destination_path).relative_path_from(Pathname.new(File.dirname(final_destination_path))).to_s
  end
end

#load_source(compilation_context) ⇒ Object

Parameters:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/epuber/compiler/file_types/xhtml_file.rb', line 47

def load_source(compilation_context)
  xhtml_content = File.read(abs_source_path)

  compilation_context.perform_plugin_things(Transformer, :source_text_file) do |transformer|
    xhtml_content = transformer.call(abs_source_path, xhtml_content, compilation_context)
  end

  # perform custom validation
  if compilation_context.should_check
    compilation_context.perform_plugin_things(Checker, :source_text_file) do |checker|
      checker.call(abs_source_path, xhtml_content, compilation_context)
    end
  end


  if compilation_context.should_write
    self.class.write_to_file(xhtml_content, abs_source_path)
  end

  xhtml_content
end

#process(compilation_context) ⇒ Object



153
154
155
# File 'lib/epuber/compiler/file_types/xhtml_file.rb', line 153

def process(compilation_context)
  write_processed(common_process(load_source(compilation_context), compilation_context))
end

#process_nokogiri_errors(errors) ⇒ Object

Parameters:



71
72
73
74
75
# File 'lib/epuber/compiler/file_types/xhtml_file.rb', line 71

def process_nokogiri_errors(errors)
  errors.each do |problem|
    UI.warning(problem, location: self)
  end
end