Class: Serbea::TemplateEngine
- Inherits:
-
Erubi::CaptureEndEngine
- Object
- Erubi::CaptureEndEngine
- Serbea::TemplateEngine
- Defined in:
- lib/serbea/template_engine.rb
Constant Summary collapse
- FRONT_MATTER_REGEXP =
%r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
Class Method Summary collapse
- .has_yaml_header?(template) ⇒ Boolean
- .render_directive ⇒ Object
- .render_directive=(directive) ⇒ Object
Instance Method Summary collapse
- #add_postamble(postamble) ⇒ Object
-
#initialize(input, properties = {}) ⇒ TemplateEngine
constructor
A new instance of TemplateEngine.
- #process_serbea_input(template, properties) ⇒ Object
Constructor Details
#initialize(input, properties = {}) ⇒ TemplateEngine
Returns a new instance of TemplateEngine.
16 17 18 19 20 |
# File 'lib/serbea/template_engine.rb', line 16 def initialize(input, properties={}) properties[:regexp] = /{%(\:?={1,2}|-|\#|%|\:)?(.*?)([-=])?%}([ \t]*\r?\n)?/m properties[:strip_front_matter] = true unless properties.key?(:strip_front_matter) super process_serbea_input(input, properties), properties end |
Class Method Details
.has_yaml_header?(template) ⇒ Boolean
12 13 14 |
# File 'lib/serbea/template_engine.rb', line 12 def self.has_yaml_header?(template) template.lines.first&.match? %r!\A---\s*\r?\n! end |
.render_directive ⇒ Object
8 9 10 |
# File 'lib/serbea/template_engine.rb', line 8 def self.render_directive @render_directive ||= "render" end |
.render_directive=(directive) ⇒ Object
5 6 7 |
# File 'lib/serbea/template_engine.rb', line 5 def self.render_directive=(directive) @render_directive = directive end |
Instance Method Details
#add_postamble(postamble) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/serbea/template_engine.rb', line 22 def add_postamble(postamble) src << postamble src << "@_erbout.html_safe" if postamble.respond_to?(:html_safe) src.gsub!("__RAW_START_PRINT__", "{{") src.gsub!("__RAW_END_PRINT__", "}}") src.gsub!("__RAW_START_EVAL__", "{%") src.gsub!("__RAW_END_EVAL__", "%}") end |
#process_serbea_input(template, properties) ⇒ Object
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/serbea/template_engine.rb', line 32 def process_serbea_input(template, properties) buff = "" string = template.dup if properties[:strip_front_matter] && self.class.has_yaml_header?(string) if string = string.match(FRONT_MATTER_REGEXP) string = string.post_match # yaml_data = SafeYAML.load(template.captures[0]) end end # Ensure the raw "tag" will strip out all ERB-style processing until string.empty? text, code, string = string.partition(/{% raw %}.*?{% endraw %}/m) buff << text if code.length > 0 buff << code. sub("{% raw %}", ""). sub("{% endraw %}", ""). gsub("{{", "__RAW_START_PRINT__"). gsub("}}", "__RAW_END_PRINT__"). gsub("{%", "__RAW_START_EVAL__"). gsub("%}", "__RAW_END_EVAL__") end end # Process the pipeline outputs string = buff buff = "" until string.empty? text, code, string = string.partition(/{{.*?}}/m) buff << text if code.length > 0 processed_filters = false code = code.gsub('\|', "__PIPE_C__") subs = code.gsub(/\s*\|\s+(.*?)\s([^|}]*)/) do args = $2 args = nil if args.strip == "" prefix = processed_filters ? ")" : "))" processed_filters = true "#{prefix}.filter(:#{$1.chomp(":")}" + (args ? ", #{args}" : "") end pipeline_suffix = processed_filters ? ") %}" : ")) %}" buff << subs.sub("{{", "{%= pipeline(self, (").sub("}}", pipeline_suffix).gsub("__PIPE_C__", '\|') end end # Process the render directives string = buff buff = "" until string.empty? text, code, string = string.partition(/{%@.*?%}/m) buff << text if code.length > 0 code.sub! /^\{%@/, "" code.sub! /%}$/, "" unless ["end", ""].include? code.strip pieces = code.split(" ") if pieces[0].start_with?(/[A-Z]/) # Ruby class name pieces[0].prepend " " pieces[0] << ".new(" else # string or something else pieces[0].prepend "(" end if pieces.last == "do" pieces.last.prepend ") " buff << "{%:= #{self.class.render_directive}#{pieces.join(" ")} %}" else pieces.last << ")" buff << "{%= #{self.class.render_directive}#{pieces.join(" ")} %}" end else buff << "{%: end %}" end end end buff end |