Class: Hermeneutics::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/hermeneutics/tags.rb

Overview

Parse a HTML file or string.

Defined Under Namespace

Classes: Error

Constant Summary collapse

RE_TAG =
%r{\A\s*(#{ren}(?::#{ren})?)\s*(.*?)\s*(/)?>}mx
RE_INSTR =
%r{\A\?\s*(#{ren})\s*(.*)\s*\?>}m
RE_COMMENT =
%r{\A!--(.*?)-->}m
RE_CDATA =
%r{\A!\[CDATA\[(.*?)\]\]>}m
RE_BANG =
%r{\A!\s*([A-Z]+)\s*(.*?)>}m
RE_CMD =
%r{\A!\s*(\[.*?\])\s*>}m
RE_ATTR =
%r{\A(#{ren}(?::#{ren})?)(=)?}
Tok =
Struct[ :type, :tag, :attrs, :data]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, term = nil) ⇒ Parser

Returns a new instance of Parser.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hermeneutics/tags.rb', line 42

def initialize str, term = nil
  @list = []
  s = str
  while s =~ /</ do
  add_data $`
    s = $'
    e = case s
      when %r{\A/\s*#{term}\s*>}im then
        nil
      when RE_TAG     then
        s = $'
        t =                Tok[ :tag,   $1.downcase, (attrs $2),
                                              (sub_parser s, $1, $3)]
        s =~ %r{\A}
        t
      when RE_INSTR   then Tok[ :instr, $1.downcase, (attrs $2), nil]
      when RE_COMMENT then Tok[ :comm,  nil,         nil,        $1 ]
      when RE_CDATA   then Tok[ nil,    nil,         nil,        $1 ]
      when RE_BANG    then Tok[ :bang,  $1,          (attrl $2), nil]
      when RE_CMD     then Tok[ :cmd,   $1,          nil,        nil]
      else
        raise Error, "Unclosed standalone tag <#{term}>."
    end
    s = $'
    e or break
    add_tok e
  end
  if term then
    str.replace s
  else
    add_data s
  end
end

Instance Attribute Details

#listObject (readonly)

Returns the value of attribute list.



40
41
42
# File 'lib/hermeneutics/tags.rb', line 40

def list
  @list
end

Instance Method Details

#find_encodingObject



76
77
78
# File 'lib/hermeneutics/tags.rb', line 76

def find_encoding
  find_enc @list
end

#pretty_printObject



80
81
82
# File 'lib/hermeneutics/tags.rb', line 80

def pretty_print
  puts_tree @list, 0
end