Class: Epuber::Compiler::FileTypes::BadeFile

Inherits:
XHTMLFile show all
Defined in:
lib/epuber/compiler/file_types/bade_file.rb

Constant Summary collapse

PRECOMPILED_CACHE_NAME =
'bade_precompiled'

Instance Attribute Summary

Attributes inherited from XHTMLFile

#global_ids, #global_links, #toc_item

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XHTMLFile

#common_process, #default_scripts, #default_styles, #initialize, #load_source, #process_global_ids, #process_nokogiri_errors

Methods inherited from SourceFile

#default_file_copy, #destination_file_exist?, #destination_file_up_to_date?, #initialize, resolve_relative_file, #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::XHTMLFile

Class Method Details

.find_imports(content) ⇒ Object



97
98
99
# File 'lib/epuber/compiler/file_types/bade_file.rb', line 97

def self.find_imports(content)
  content.to_enum(:scan, /^\s*import ("|')([^'"]*)("|')/).map { Regexp.last_match[2] }
end

Instance Method Details

#find_dependenciesObject

return [Array<String>]



83
84
85
# File 'lib/epuber/compiler/file_types/bade_file.rb', line 83

def find_dependencies
  (super + self.class.find_imports(File.read(abs_source_path))).uniq
end

#precompiled_pathString

Returns:

  • (String)


89
90
91
# File 'lib/epuber/compiler/file_types/bade_file.rb', line 89

def precompiled_path
  File.join(Config.instance.build_cache_path(PRECOMPILED_CACHE_NAME), "#{source_path}.precompiled.yml")
end

#pretty_precompiled_pathObject



93
94
95
# File 'lib/epuber/compiler/file_types/bade_file.rb', line 93

def pretty_precompiled_path
  Config.instance.pretty_path_from_project(precompiled_path)
end

#process(compilation_context) ⇒ Object

Parameters:



15
16
17
18
19
20
21
22
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
# File 'lib/epuber/compiler/file_types/bade_file.rb', line 15

def process(compilation_context)
  target = compilation_context.target
  book = compilation_context.book
  file_resolver = compilation_context.file_resolver

  up_to_date = source_file_up_to_date?
  precompiled_exists = File.exist?(precompiled_path)

  variables = {
    __book: book,
    __target: target,
    __file_resolver: file_resolver,
    __file: self,
    __toc_item: toc_item,
    __const: Hash.new do |_hash, key|
               UI.warning("Undefined constant with key `#{key}`", location: caller_locations[0])
             end.merge!(target.constants),
  }

  should_load_from_precompiled = up_to_date &&
                                 precompiled_exists &&
                                 compilation_context.incremental_build? &&
                                 !compilation_context.should_write

  precompiled = if should_load_from_precompiled
                  begin
                    Bade::Precompiled.from_yaml_file(precompiled_path)
                  rescue LoadError
                    UI.warning("Empty precompiled file at path #{pretty_precompiled_path}", location: self)
                    nil
                  end
                end

  if precompiled.nil?
    if compilation_context.incremental_build?
      UI.print_processing_debug_info('Parsing new version of source file')
    end

    bade_content = load_source(compilation_context)

    xhtml_content = UI.print_step_processing_time('rendering changed Bade') do
      renderer = Bade::Renderer.from_source(bade_content, source_path)
                               .with_locals(variables)

      # turn on optimizations when can
      renderer.optimize = true if renderer.respond_to?(:optimize=)

      FileUtils.mkdir_p(File.dirname(precompiled_path))
      renderer.precompiled.write_yaml_to_file(precompiled_path)

      renderer.render(new_line: '', indent: '')
    end
  else
    xhtml_content = UI.print_step_processing_time('rendering precompiled Bade') do
      renderer = Bade::Renderer.from_precompiled(precompiled)
                               .with_locals(variables)
      renderer.file_path = source_path

      renderer.render(new_line: '', indent: '')
    end
  end

  write_compiled(common_process(xhtml_content, compilation_context))
  update_metadata!
end