class Hermeneutics::Html::Generator

Constants

INDENT
NBSP
TAGS

Attributes

assign_attributes[RW]
cdata_block[RW]
close_standalone[RW]

Public Class Methods

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

Public Instance Methods

<<(str) click to toggle source
# File lib/hermeneutics/html.rb, line 353
def << str
  @generator.plain str if str
  self
end
_(*strs) { || ... } click to toggle source
# File lib/hermeneutics/html.rb, line 358
def _ *strs
  if strs.notempty? then
    pcdata *strs
  else
    @generator.plain yield
    nil
  end
end
comment(str) click to toggle source
# File lib/hermeneutics/html.rb, line 198
def comment str
  if str =~ /\A.*\z/ then
    brace_comment do
      @out << " " << str << " "
    end
  else
    brace_comment do
      brk
      out_brk str
      do_ind
    end
  end
end
doctype(*args) click to toggle source
# File lib/hermeneutics/html.rb, line 185
def doctype *args
  brace true do
    @out << "!DOCTYPE"
    args.each { |x|
      @out << " "
      if x =~ /\W/ then
        @out << '"' << (@ent.encode x) << '"'
      else
        @out << x
      end
    }
  end
end
encoding() click to toggle source
# File lib/hermeneutics/html.rb, line 114
def encoding
  case @out
    when IO then @out.external_encoding||Encoding.default_external
    else         @out.encoding
  end
end
file_path() click to toggle source
# File lib/hermeneutics/html.rb, line 120
def file_path
  @out.path
rescue NoMethodError
end
form(**attrs, &block) click to toggle source
# File lib/hermeneutics/html.rb, line 392
def form **attrs, &block
  attrs[ :method] ||=
          attrs[ :enctype] == "multipart/form-data" ? "post" : "get"
  method_missing :form, **attrs, &block
end
h(i, **attrs, &block) click to toggle source
# File lib/hermeneutics/html.rb, line 388
def h i, **attrs, &block
  method_missing :"h#{i.to_i}", **attrs, &block
end
head(**attrs) { || ... } click to toggle source
# File lib/hermeneutics/html.rb, line 381
def head **attrs
  method_missing :head, **attrs do
    meta charset: @generator.encoding
    yield
  end
end
html(**attrs) { |end| ... } click to toggle source
# File lib/hermeneutics/html.rb, line 376
  def html **attrs
    attrs[ :"lang"] ||= language
    method_missing :html, **attrs do yield end
  end

  def head **attrs
    method_missing :head, **attrs do
      meta charset: @generator.encoding
      yield
    end
  end

  def h i, **attrs, &block
    method_missing :"h#{i.to_i}", **attrs, &block
  end

  def form **attrs, &block
    attrs[ :method] ||=
            attrs[ :enctype] == "multipart/form-data" ? "post" : "get"
    method_missing :form, **attrs, &block
  end

  def input **attrs, &block
    attrs[ :id] ||= attrs[ :name]
    method_missing :input, **attrs, &block
  end

end
input(**attrs, &block) click to toggle source
# File lib/hermeneutics/html.rb, line 398
def input **attrs, &block
  attrs[ :id] ||= attrs[ :name]
  method_missing :input, **attrs, &block
end
javascript(str = nil, &block) click to toggle source
# File lib/hermeneutics/html.rb, line 371
def javascript str = nil, &block
  mime = { type: "text/javascript" }
  script mime, str, &block
end
merge(str) click to toggle source
# File lib/hermeneutics/html.rb, line 124
def merge str
  do_ind
  @out << str
end
pcdata(*strs) click to toggle source
# File lib/hermeneutics/html.rb, line 346
def pcdata *strs
  strs.each { |s|
    @generator.plain s if s.notempty?
  }
  nil
end
pi_tag(tag, attrs = nil) click to toggle source

Processing Instruction

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

nls 0 = no newline 1 = newline after 2 = newline after both 3 = and advance indent 4 = Block without any indent

# File lib/hermeneutics/html.rb, line 138
def tag tag, type, attrs = nil
  tag = tag.to_s
  nls = type & 0xf
  if (type & 0x10).nonzero? then
    brace nls>0 do
      @out << tag
      mkattrs attrs
      @out << " /" if @close_standalone
    end
  else
    begin
      brk if nls>1
      brace nls>1 do
        @out << tag
        mkattrs attrs
      end
      if nls >3 then
        verbose_block yield
      else
        indent_if nls>2 do
          if block_given? then
            r = yield
            plain r if String === r
          end
        end
      end
    ensure
      brk if nls>1
      brace nls>0 do
        @out << "/" << tag
      end
    end
  end
  nil
end
verbose_block(str) click to toggle source
# File lib/hermeneutics/html.rb, line 211
def verbose_block str
  if @cdata_block then
    @out << "/* "
    brace false do
      @out << "![CDATA["
      @out << " */" << $/
      @out << str
      @out << $/ << "/* "
      @out << "]]"
    end
    @out << " */"
  else
    out_brk str
  end
end

Private Instance Methods

brace(nl) { || ... } click to toggle source
# File lib/hermeneutics/html.rb, line 244
def brace nl
  do_ind
  @out << "<"
  yield
  nil
ensure
  @out << ">"
  brk if nl
end
brace_comment() { || ... } click to toggle source
# File lib/hermeneutics/html.rb, line 253
def brace_comment
  brace true do
    @out << "!--"
    yield
    @out << "--"
  end
end
brk() click to toggle source
# File lib/hermeneutics/html.rb, line 227
def brk
  unless @nl then
    @nl = true
    @out << $/
  end
end
do_ind() click to toggle source
# File lib/hermeneutics/html.rb, line 238
def do_ind
  if @nl then
    @out << @ind.last
    @nl = false
  end
end
indent() { || ... } click to toggle source
# File lib/hermeneutics/html.rb, line 268
def indent
  @ind.push @ind.last + " "*INDENT
  yield
ensure
  @ind.pop
end
indent_if(flag) { |end| ... } click to toggle source
# File lib/hermeneutics/html.rb, line 260
  def indent_if flag
    if flag then
      indent do yield end
    else
      yield
    end
  end
  INDENT = 2
  def indent
    @ind.push @ind.last + " "*INDENT
    yield
  ensure
    @ind.pop
  end
  def mkattrs attrs
    attrs or return
    attrs.each { |k,v|
      if Symbol === k then k = k.to_s ; k.gsub! /_/, "-" end
      v = case v
        when Array      then v.compact.join " "
        when true       then k.to_s if @assign_attributes
        when nil, false then next
        else                 v.to_s
      end
      @out << " " << k
      @out << "=\"" << (@ent.encode v) << "\"" if v.notempty?
    }
  end
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/hermeneutics/html.rb, line 328
def method_missing name, *args, &block
  t = tag? name
  t or super
  if String === args.last then
    b = args.pop
    @generator.tag name, t, *args do b end
  else
    @generator.tag name, t, *args, &block
  end
end
mkattrs(attrs) click to toggle source
# File lib/hermeneutics/html.rb, line 274
def mkattrs attrs
  attrs or return
  attrs.each { |k,v|
    if Symbol === k then k = k.to_s ; k.gsub! /_/, "-" end
    v = case v
      when Array      then v.compact.join " "
      when true       then k.to_s if @assign_attributes
      when nil, false then next
      else                 v.to_s
    end
    @out << " " << k
    @out << "=\"" << (@ent.encode v) << "\"" if v.notempty?
  }
end
out_brk(str) click to toggle source
# File lib/hermeneutics/html.rb, line 233
def out_brk str
  @out << str
  @nl = str !~ /.\z/
  brk
end
tag?(name) click to toggle source
# File lib/hermeneutics/html.rb, line 322
def tag? name
  TAGS[ name]
end