Module: IMW::Parsers::HtmlMatchers

Included in:
HtmlParser
Defined in:
lib/imw/parsers/html_parser/matchers.rb

Defined Under Namespace

Classes: MatchArray, MatchAttribute, MatchFirstElement, MatchHash, MatchProc, MatchRegexp, MatchRegexpRepeatedly, Matcher

Class Method Summary collapse

Class Method Details

.build_match_hash(spec_hash) ⇒ Object

construct the downstream part of a hash matcher



260
261
262
263
264
265
266
# File 'lib/imw/parsers/html_parser/matchers.rb', line 260

def self.build_match_hash spec_hash
  hsh = { }
  spec_hash.each do |attr, spec|
    hsh[attr] = build_parse_tree(spec)
  end
  hsh
end

.build_parse_tree(spec) ⇒ Object

recursively build a tree of matchers



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/imw/parsers/html_parser/matchers.rb', line 271

def self.build_parse_tree spec
  case spec
  when nil            then nil
  when Matcher        then spec
  when Hash           then MatchHash.new(build_match_hash(spec))
  when Array          then
    return nil if spec.empty?
    raise "Array spec must be a single selector or a selector and another match specification" unless (spec.length <= 2)
    MatchArray.new(spec[0].to_s, build_parse_tree(spec[1]))
  when String         then MatchFirstElement.new(spec)
  when Proc           then MatchProc.new(nil, spec)
  when Regexp         then MatchRegexp.new(nil, spec, nil, :capture => 1)
  when Symbol         then MatchAttribute.new(nil, spec, nil)
  else raise "Don't know how to parse #{spec.inspect}"
  end
end