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

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

Instance Method Summary collapse

Methods inherited from SourceFile

#default_file_copy, #initialize, #write_compiled, #write_processed

Methods inherited from AbstractFile

#==, file_copy, file_copy!, 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



15
16
17
# File 'lib/epuber/compiler/file_types/xhtml_file.rb', line 15

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/epuber/compiler/file_types/xhtml_file.rb', line 61

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

  xhtml_doc = XHTMLProcessor.xml_document_from_string(content, source_path)
  XHTMLProcessor.add_missing_root_elements(xhtml_doc, book.title, target.epub_version)

  XHTMLProcessor.add_styles(xhtml_doc, default_styles(target, file_resolver))

  XHTMLProcessor.add_viewport(xhtml_doc, target.default_viewport) unless target.default_viewport.nil?
  self.properties << :scripted if XHTMLProcessor.using_javascript?(xhtml_doc)

  XHTMLProcessor.resolve_links(xhtml_doc, destination_path, file_resolver.dest_finder)
  XHTMLProcessor.resolve_images(xhtml_doc, destination_path, file_resolver)

  xhtml_string = xhtml_doc.to_s

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

  # perform custom validation
  if compilation_context.should_check
    compilation_context.perform_plugin_things(Checker, :result_text_xhtml_string) do |checker|
      checker.call(final_destination_path, xhtml_string, compilation_context)
    end
  end

  xhtml_string
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



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

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:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/epuber/compiler/file_types/xhtml_file.rb', line 34

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



96
97
98
# File 'lib/epuber/compiler/file_types/xhtml_file.rb', line 96

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