class Dang::Parser

Constants

DOC_TYPES
Pair

Public Class Methods

new(str, debug=false) click to toggle source
# File lib/dang/parser.rb, line 359
def initialize(str, debug=false)
  setup_parser(str, debug)
  @doctype = nil
  @output = ""
end

Public Instance Methods

attrs(at,sel=[]) click to toggle source
# File lib/dang/parser.rb, line 434
def attrs(at,sel=[])
  out = []
  classes = []

  (at+sel).flatten.each do |pair|
    key = pair.key
    val = pair.value

    if key == "class"
      val = val.str if val.kind_of? Literal
      classes.unshift val
    elsif val == true
      out << "#{key}"
      out << " "
    else
      out << "#{key}='"
      out << val
      out << "'"
      out << " "
    end
  end

  unless classes.empty?
    expanded = ["class='"]
    classes.each do |c|
      expanded << c
      expanded << " "
    end

    expanded[-1] = "'"

    classes = expanded

    if out.empty?
      return classes
    end

    out = classes + [" "] + out
  end

  if out.last == " "
    out.pop
  end

  out
end
code(str, print=true) click to toggle source
# File lib/dang/parser.rb, line 530
def code(str, print=true)
  Code.new(str, print)
end
compile() click to toggle source
# File lib/dang/parser.rb, line 397
def compile
  strings = @output.flatten.map do |i|
    case i
    when Literal
      "_out << #{i.str.dump}"
    when Code
      if i.print
        "_out << (#{i.str}).to_s"
      else
        i.str
      end
    when Filter
      "_out << Dang.run_filter('#{i.name}', #{i.str.dump}).to_s"
    end
  end

  "_out = '';\n" + strings.join(";") + ";_out"
end
html_doctype() click to toggle source
# File lib/dang/parser.rb, line 386
def html_doctype
  return "" unless @doctype

  unless DOC_TYPES.key? @doctype
    warn "doctype '#{@doctype}' not understood, using 'html'"
    @doctype = "html"
  end

  DOC_TYPES[@doctype].dup
end
join(f,b) click to toggle source
# File lib/dang/parser.rb, line 519
def join(f,b)
  f = Literal.new(f) if f.kind_of? String
  b = Literal.new(b) if b.kind_of? String

  if b.kind_of? Array
    [f] + b
  else
    [f,b]
  end
end
joinm(*elems) click to toggle source
# File lib/dang/parser.rb, line 507
def joinm(*elems)
  elems.flatten.map do |i|
    if i.kind_of? String
      Literal.new(i)
    else
      i
    end
  end
end
output(env=nil) click to toggle source
# File lib/dang/parser.rb, line 416
def output(env=nil)
  out = eval(compile, env || binding).strip

  doctype = html_doctype

  if doctype.empty?
    str = out
  else
    if out.empty?
      str = doctype
    else
      str = doctype << "\n" << out
    end
  end

  str
end