Class: HMote

Inherits:
Object
  • Object
show all
Defined in:
lib/hmote.rb,
lib/hmote/version.rb

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

PATTERN =
/
  ^[^\S\n]*(%)[^\S\n]*(.*?)(?:\n|\z) |  # % single-line code
  (<\?)\s+(.*?)\s+\?>                |  # <? multi-line code ?>
  (\{\{!?)(.*?)\}\}                     # {{ escape }} or {{! unescape }}
/mx
VERSION =
"1.4.0"

Class Method Summary collapse

Class Method Details

.compile(context, parts) ⇒ Object



33
34
35
# File 'lib/hmote.rb', line 33

def self.compile(context, parts)
  context.instance_eval(parts)
end

.parse(template, context = self, vars = []) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hmote.rb', line 10

def self.parse(template, context = self, vars = [])
  terms = template.split(PATTERN)
  parts = "Proc.new do |params, __o|\n params ||= {}; __o ||= ''\n"

  vars.each do |var|
    parts << sprintf("%s = params[%p]\n", var, var)
  end

  while (term = terms.shift)
    case term
    when "<?"  then parts << "#{terms.shift}\n"
    when "%"   then parts << "#{terms.shift}\n"
    when "{{"  then parts << "__o << Hache.h((#{terms.shift}).to_s)\n"
    when "{{!" then parts << "__o << (#{terms.shift}).to_s\n"
    else            parts << "__o << #{term.dump}\n"
    end
  end

  parts << "__o; end"

  compile(context, parts)
end