Class: XOXO

Inherits:
Microformat show all
Defined in:
lib/mofo/xoxo.rb

Defined Under Namespace

Classes: Label

Constant Summary collapse

@@parents =
%w[ol ul]
@@children =
%w[li]
@@children_xpath =
xpath_build[@@children]
@@parents_xpath =
xpath_build[@@parents]

Class Method Summary collapse

Methods inherited from Microformat

#method_missing

Methods included from Microformat::Base

#find, #find_in_children, #timeout=

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Microformat

Class Method Details

.build_label(node) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/mofo/xoxo.rb', line 41

def self.build_label(node)
  if node.elem? 
    label = Label.new(node.innerHTML.strip)
    label.url = node['href'] if node.name == 'a'
    label
  elsif node.text? && !node.to_s.strip.empty?
    node.to_s.strip 
  end
end

.build_tree(child) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mofo/xoxo.rb', line 24

def self.build_tree(child)
  tree = []
  child.search(@@children_xpath) do |element|
    label, branch = nil, nil
    element.children.each do |inner|
      label  ||= build_label(inner) unless container?(inner)
      branch ||= build_tree(inner) if container?(inner)
    end
    tree << (branch ? { label => branch } : label)
  end 
  tree
end

.container?(el) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/mofo/xoxo.rb', line 37

def self.container?(el)
  el.elem? && @@parents.include?(el.name)
end

.find_every(doc) ⇒ Object



16
17
18
# File 'lib/mofo/xoxo.rb', line 16

def self.find_every(doc)
  doc.map { |child| build_tree(child) }
end

.find_first(doc) ⇒ Object



12
13
14
# File 'lib/mofo/xoxo.rb', line 12

def self.find_first(doc)
  find_every(doc).first
end

.find_occurences(doc) ⇒ Object



20
21
22
# File 'lib/mofo/xoxo.rb', line 20

def self.find_occurences(doc)
  @options[:class] ? doc/".xoxo" : doc.search(@@parents_xpath)
end