class Hermes::Html::Generator

Attributes

assign_attributes[RW]
close_standalone[RW]

Public Class Methods

new(out) click to toggle source
# File lib/hermes/html.rb, line 153
def initialize out
  @out = out||$stdout
  @ent = Entities.new
  @nl, @ind = true, [ ""]
end

Public Instance Methods

comment(str) click to toggle source
# File lib/hermes/html.rb, line 227
def comment str
  brace true do
    nl = str =~ %r(#$/\z)
    @out << "!--"
    brk if nl
    @out << str
    do_ind if nl
    @out << "--"
  end
end
commented_cdata(str) click to toggle source
# File lib/hermes/html.rb, line 237
def commented_cdata str
  @out << $/ << "/* "
  brace false do
    @out << "![CDATA["
    @out << " */" << $/
    @out << str
    @out << $/ << "/* "
    @out << "]]"
  end
  @out << " */" << $/
end
doctype(type, path, link) click to toggle source
# File lib/hermes/html.rb, line 221
def doctype type, path, link
  brace true do
    @out << "!DOCTYPE"
    %W(#{type} PUBLIC "#{path}" "#{link}").each { |x| @out << " " << x }
  end
end
encoding() click to toggle source
# File lib/hermes/html.rb, line 158
def encoding
  case @out
    when IO then @out.external_encoding||ENCODING
    else         @out.encoding
  end
end
path() click to toggle source
# File lib/hermes/html.rb, line 164
def path
  @out.path
rescue NoMethodError
end
pi_tag(tag, attrs = nil) click to toggle source

Processing Instruction

# File lib/hermes/html.rb, line 211
def pi_tag tag, attrs = nil
  brace true do
    begin
      @out << "?" << tag
      mkattrs attrs
    ensure
      @out << " ?"
    end
  end
end
plain(str) click to toggle source
# File lib/hermes/html.rb, line 168
def plain str
  do_ind
  @out << (@ent.encode str)
end
tag(nls, tag, attrs = nil) { || ... } click to toggle source

nls 0 = no newline 1 = newline after 2 = newline after both 3 = and advance indent

# File lib/hermes/html.rb, line 177
def tag nls, tag, attrs = nil
  if String === attrs then
    tag nls, tag, nil do attrs end
    return
  end
  if Symbol === tag then tag = tag.new_string ; tag.gsub! /_/, "-" end
  if block_given? then
    begin
      brk if nls>1
      brace nls>1 do
        @out << tag
        mkattrs attrs
      end
      indent_if nls>2 do
        r = yield
        plain r if String === r
      end
    ensure
      brk if nls>1
      brace nls>0 do
        @out << "/" << tag
      end
    end
  else
    brk if nls>1
    brace nls>0 do
      @out << tag
      mkattrs attrs
      @out << " /" if @close_standalone
    end
  end
  nil
end

Private Instance Methods

brace(nl) { || ... } click to toggle source
# File lib/hermes/html.rb, line 261
def brace nl
  do_ind
  @out << "<"
  yield
  nil
ensure
  @out << ">"
  brk if nl
end
brk() click to toggle source
# File lib/hermes/html.rb, line 249
def brk
  unless @nl then
    @nl = true
    @out << $/
  end
end
do_ind() click to toggle source
# File lib/hermes/html.rb, line 255
def do_ind
  if @nl then
    @out << @ind.last
    @nl = false
  end
end
indent() { || ... } click to toggle source
# File lib/hermes/html.rb, line 277
def indent
  @ind.push @ind.last + "  "
  yield
ensure
  @ind.pop
end
indent_if(flag) { || ... } click to toggle source
# File lib/hermes/html.rb, line 270
def indent_if flag, &block
  if flag then
    indent &block
  else
    yield
  end
end
mkattrs(attrs) click to toggle source
# File lib/hermes/html.rb, line 283
def mkattrs attrs
  attrs or return
  attrs.each { |k,v|
    if Symbol === k then k = k.new_string ; k.gsub! /_/, "-" end
    v or next
    @out << " " << k
    case v
      when true then
        next unless @assign_attributes
        v = k
      when Array then
        v = v.compact.join " "
    end
    @out << "=\"" << (@ent.encode v) << "\""
  }
end