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

#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, #load_source, #process_nokogiri_errors

Methods inherited from SourceFile

#default_file_copy, #destination_file_exist?, #destination_file_up_to_date?, #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

Class Method Details

.find_imports(content) ⇒ Object



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

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>]



78
79
80
# File 'lib/epuber/compiler/file_types/bade_file.rb', line 78

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

#precompiled_pathString

Returns:

  • (String)


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

def precompiled_path
  File.join(Config.instance.build_cache_path(PRECOMPILED_CACHE_NAME), source_path + '.precompiled.yml')
end

#pretty_precompiled_pathObject



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

def pretty_precompiled_path
  Config.instance.pretty_path_from_project(precompiled_path)
end

#process(compilation_context) ⇒ Object

Parameters:



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
# File 'lib/epuber/compiler/file_types/bade_file.rb', line 16

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 { |_hash, key| UI.warning("Undefined constant with key `#{key}`", location: caller_locations[0]) }.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?
    xhtml_content = UI.print_step_processing_time('rendering precompiled Bade') do
      renderer = Bade::Renderer.from_precompiled(precompiled)
                               .with_locals(variables)

      renderer.render(new_line: '', indent: '')
    end
  else
    UI.print_processing_debug_info('Parsing new version of source file') if compilation_context.incremental_build?

    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
      if renderer.respond_to?(:optimize=)
        renderer.optimize = true
      end

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

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

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