Class: Nydp::Html::HamlToHtml

Inherits:
Object
  • Object
show all
Includes:
Builtin::Base, Singleton
Defined in:
lib/nydp/html.rb

Instance Method Summary collapse

Instance Method Details

#builtin_invoke(vm, args) ⇒ Object



74
75
76
# File 'lib/nydp/html.rb', line 74

def builtin_invoke vm, args
  vm.push_arg Nydp::StringAtom.new convert_from_haml(args.car.to_s)
end

#convert_from_haml(convertible) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nydp/html.rb', line 60

def convert_from_haml convertible
  Haml::Engine.new(normalise_indentation(convertible), suppress_eval: true).render
rescue Exception => e
  if e.line
    lines = convertible.split(/\n/)
    beginning = e.line - 2
    beginning = 0 if beginning < 0
    selection = lines[beginning...(e.line + 1)].join "\n"
    "#{e.message}<br/>line #{e.line}<br/><br/><pre>#{selection}</pre>"
  else
    e.message
  end
end

#normalise_indentation(txt) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/nydp/html.rb', line 51

def normalise_indentation txt
  lines = txt.split(/\n/).select { |line| line.strip != "" }
  return txt if lines.length == 0
  indentation = /^ +/.match(lines.first)
  return txt unless indentation
  indentation = indentation.to_s
  txt.gsub(/^#{indentation}/, "")
end