Class: Titi::Matcher::MatchFirstElement

Inherits:
Base
  • Object
show all
Defined in:
lib/titi/ignore/matcher.rb

Overview

Concrete subclass of IMW::Parsers::HtmlMatchers::Matcher for matching against the first element of a document matching a selector.

Direct Known Subclasses

MatchProc

Instance Attribute Summary

Attributes inherited from Base

#matcher, #options, #selector

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Titi::Matcher::Base

Instance Method Details

#match(doc) ⇒ Object

Grab the first element from doc matching the selector this class was initialized with. If initialized with a matcher, then return the matcher‘s match against the first element, else just return the inner HTML of the first element.

m = MatchFirstElement.new('span#bio/a.homepage')
m.match('<span id="bio"><a class="homepage" href="http://foo.bar">My Homepage</a></span>')
# => 'My Homepage'


41
42
43
44
45
46
47
48
49
# File 'lib/titi/ignore/matcher.rb', line 41

def match doc
  doc = Hpricot(doc) if doc.is_a?(String)
  el = doc.at(selector) or return nil
  if matcher
    matcher.match(el)
  else
    options[:html] ? el : el.inner_text.strip
  end
end