Class: Texas::Template::Runner::Base

Inherits:
Object
  • Object
show all
Includes:
Helper::Base, Helper::Info
Defined in:
lib/texas/template/runner/base.rb

Direct Known Subclasses

Markdown, TeX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::Info

#abstract, #info, #marked_for_rewrite?, #summary

Methods included from Helper::Base

#default_search_paths, #find_template_file, #find_template_file!, #partial, #path_with_templates_basename, #render, #render_as_array, #template_extensions, #templates_by_glob

Constructor Details

#initialize(_filename, _build) ⇒ Base

Returns a new instance of Base.



12
13
14
15
16
17
# File 'lib/texas/template/runner/base.rb', line 12

def initialize(_filename, _build)
  self.filename = _filename
  self.build = _build
  self.content = File.read(filename)
  @output_filename = filename.gsub(/(\.erb)$/, '')
end

Instance Attribute Details

#buildObject

Returns the value of attribute build.



10
11
12
# File 'lib/texas/template/runner/base.rb', line 10

def build
  @build
end

#contentObject

Returns the value of attribute content.



10
11
12
# File 'lib/texas/template/runner/base.rb', line 10

def content
  @content
end

#filenameObject

Returns the value of attribute filename.



10
11
12
# File 'lib/texas/template/runner/base.rb', line 10

def filename
  @filename
end

Instance Method Details

#__path__Object



26
27
28
# File 'lib/texas/template/runner/base.rb', line 26

def __path__
  File.dirname filename
end

#__render__(locals = {}) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/texas/template/runner/base.rb', line 46

def __render__(locals = {})
  @locals = OpenStruct.new(locals)
  ERB.new(@content, nil, nil, "@erbout").result(binding)
rescue TemplateError => ex
  raise ex
rescue Exception => ex
  raise TemplateError.new(self, ex.message, ex)
end

#__run__(locals = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/texas/template/runner/base.rb', line 55

def __run__(locals = {})
  verbose { "[T] #{filename.gsub(build_path, '')}".dark }

  old_current_template = build.current_template
  build.current_template = self
  @output = after_render __render__(locals)
  build.current_template = old_current_template
  @output
end

#after_render(str) ⇒ Object



66
# File 'lib/texas/template/runner/base.rb', line 66

def after_render(str); str; end

#after_writeObject



65
# File 'lib/texas/template/runner/base.rb', line 65

def after_write; end

#append_to_output(str) ⇒ Object



42
43
44
# File 'lib/texas/template/runner/base.rb', line 42

def append_to_output(str)
  @erbout << str
end

#build_pathObject



22
23
24
# File 'lib/texas/template/runner/base.rb', line 22

def build_path
  build.__path__
end

#documentObject



30
31
32
# File 'lib/texas/template/runner/base.rb', line 30

def document
  build.document_struct
end

#oObject



38
39
40
# File 'lib/texas/template/runner/base.rb', line 38

def o
  @locals
end

#rootObject

TODO: Use Forwardable



20
# File 'lib/texas/template/runner/base.rb', line 20

def root; build.root; end

#storeObject



34
35
36
# File 'lib/texas/template/runner/base.rb', line 34

def store
  build.store
end

#writeObject



68
69
70
71
72
# File 'lib/texas/template/runner/base.rb', line 68

def write
  __run__
  File.open(@output_filename, 'w') {|f| f.write(@output) }
  after_write
end