Class: Temple::Generator

Inherits:
Object show all
Includes:
Mixins::CompiledDispatcher, Mixins::Options, Utils
Defined in:
lib/temple/generator.rb

Overview

Abstract generator base class Generators should inherit this class and compile the Core Abstraction to ruby code.

Constant Summary

Constants included from Utils

Utils::ESCAPE_HTML, Utils::ESCAPE_HTML_PATTERN

Instance Attribute Summary

Attributes included from Mixins::Options

#options

Instance Method Summary collapse

Methods included from Mixins::Options

included, #initialize

Methods included from Mixins::CompiledDispatcher

#compile

Methods included from Utils

#empty_exp?, #escape_html, #escape_html_safe, #indent_dynamic, #unique_name

Instance Method Details

#call(exp) ⇒ Object



18
19
20
# File 'lib/temple/generator.rb', line 18

def call(exp)
  [preamble, compile(exp), postamble].flatten.compact.join('; ')
end

#create_bufferObject



38
39
# File 'lib/temple/generator.rb', line 38

def create_buffer
end

#on(*exp) ⇒ Object

Raises:



45
46
47
# File 'lib/temple/generator.rb', line 45

def on(*exp)
  raise InvalidExpression, "Generator supports only core expressions - found #{exp.inspect}"
end

#on_capture(name, exp) ⇒ Object



57
58
59
60
61
# File 'lib/temple/generator.rb', line 57

def on_capture(name, exp)
  capture_generator.new(capture_generator: options[:capture_generator],
                        freeze_static: options[:freeze_static],
                        buffer: name).call(exp)
end

#on_code(code) ⇒ Object



71
72
73
# File 'lib/temple/generator.rb', line 71

def on_code(code)
  code
end

#on_dynamic(code) ⇒ Object



67
68
69
# File 'lib/temple/generator.rb', line 67

def on_dynamic(code)
  concat(code)
end

#on_multi(*exp) ⇒ Object



49
50
51
# File 'lib/temple/generator.rb', line 49

def on_multi(*exp)
  exp.map {|e| compile(e) }.join('; '.freeze)
end

#on_newlineObject



53
54
55
# File 'lib/temple/generator.rb', line 53

def on_newline
  "\n"
end

#on_static(text) ⇒ Object



63
64
65
# File 'lib/temple/generator.rb', line 63

def on_static(text)
  concat(options[:freeze_static] ? "#{text.inspect}.freeze" : text.inspect)
end

#postambleObject



26
27
28
# File 'lib/temple/generator.rb', line 26

def postamble
  [return_buffer, restore_buffer]
end

#preambleObject



22
23
24
# File 'lib/temple/generator.rb', line 22

def preamble
  [save_buffer, create_buffer]
end

#restore_bufferObject



34
35
36
# File 'lib/temple/generator.rb', line 34

def restore_buffer
  "ensure; #{buffer} = #{@original_buffer}; end" if options[:save_buffer]
end

#return_bufferObject



41
42
43
# File 'lib/temple/generator.rb', line 41

def return_buffer
  'nil'
end

#save_bufferObject



30
31
32
# File 'lib/temple/generator.rb', line 30

def save_buffer
  "begin; #{@original_buffer = unique_name} = #{buffer} if defined?(#{buffer})" if options[:save_buffer]
end