class Hermes::Tags

Example

This parses a table and outputs it as a CSV.

t = Tags.compile "<table><tr><td> ... </table>", "iso-8859-15"
t.table.each :tr do |row|
  if row.has? :th then
    l = row.map :th do |h| h.data end.join ";"
  else
    l = row.map :td do |c| c.data end.join ";"
  end
  puts l
end

Attributes

attrs[R]
list[R]
name[R]

Public Class Methods

new(name, attrs = nil, *elems) click to toggle source
# File lib/hermes/tags.rb, line 238
def initialize name, attrs = nil, *elems
  @name = name.to_sym if name
  @attrs = {}.update attrs if attrs
  @list = []
  @list.concat elems.flatten
end

Public Instance Methods

compile(str, parser = nil) click to toggle source
# File lib/hermes/tags.rb, line 196
def compile str, parser = nil
  p = (parser||Parser).new str
  enc = p.find_encoding||str.encoding
  l = lex p, enc
  new nil, nil, l
end
concat(elems) click to toggle source
# File lib/hermes/tags.rb, line 249
def concat elems
  @list.concat elems
end
data() click to toggle source
# File lib/hermes/tags.rb, line 297
def data
  d = ""
  gather_data self, d
  d
end
each(t = nil) { |e| ... } click to toggle source
# File lib/hermes/tags.rb, line 257
def each t = nil
  if t then
    @list.each { |e|
      yield e if Tags === e and e.name == t
    }
  else
    @list.each { |e| yield e }
  end
end
has?(t)
Alias for: has_tag?
has_tag?(t) click to toggle source
# File lib/hermes/tags.rb, line 273
def has_tag? t
  @list.find { |e|
    Tags === e and e.name == t
  } and true
end
Also aliased as: has?
inspect() click to toggle source
# File lib/hermes/tags.rb, line 253
def inspect
  "<##@name [#{@list.length}]>"
end
lex(parser, encoding = nil) click to toggle source
# File lib/hermes/tags.rb, line 203
def lex parser, encoding = nil
  r = []
  while parser.list.any? do
    e = parser.list.shift
    case e.type
      when :tag
        a = {}
        e.attrs.each { |k,v|
          v.force_encoding encoding if encoding
          a[ k.downcase.to_sym] = Entities.new.decode v
        }
        i = new e.tag, a
        if e.data then
          f = lex e.data, encoding
          i.concat f
        end
        r.push i
      when nil
        d = e.data
        d.force_encoding encoding if encoding
        c = Entities.new.decode d
        r.push c
      when :instr then
      when :comm  then
      when :bang  then
      when :cmd   then
    end
  end
  r
end
map(t) { |e| ... } click to toggle source
# File lib/hermes/tags.rb, line 267
def map t
  @list.map { |e|
    yield e if Tags === e and e.name == t
  }.compact
end
method_missing(sym, *args) click to toggle source
Calls superclass method
# File lib/hermes/tags.rb, line 291
def method_missing sym, *args
  (tag sym, *args) or super
rescue
  super
end
push(elem) click to toggle source
# File lib/hermes/tags.rb, line 245
def push elem
  @list.push elem
end
tag(t, n = nil) click to toggle source
# File lib/hermes/tags.rb, line 280
def tag t, n = nil
  n ||= 0
  @list.each { |e|
    if Tags === e and e.name == t then
      return e if n.zero?
      n -= 1
    end
  }
  nil
end

Private Instance Methods

gather_data(t, d) click to toggle source
# File lib/hermes/tags.rb, line 305
def gather_data t, d
  t.list.each { |e|
    case e
      when Tags then gather_data e, d
      else           d << e
    end
  }
end