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.5.2"

Class Method Summary collapse

Class Method Details

.compile(context, parts) ⇒ Object



58
59
60
# File 'lib/hmote.rb', line 58

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

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hmote.rb', line 33

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

  vars.each { |var| parts << format("%s = params[%p]\n", var, var) }

  while (term = terms.shift)
    parts << parse_expression(terms, term)
  end

  parts << "__o; end"

  compile(context, parts)
end

.parse_expression(terms, term) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/hmote.rb', line 48

def self.parse_expression(terms, term)
  case term
  when "<?"  then terms.shift + "\n"
  when "%"   then terms.shift + "\n"
  when "{{"  then "__o << Hache.h((" + terms.shift + ").to_s)\n"
  when "{{!" then "__o << (" + terms.shift + ").to_s\n"
  else            "__o << " + term.dump + "\n"
  end
end