class Nydp::Html::HamlToHtml

Public Instance Methods

builtin_invoke(vm, args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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