Sha256: 7b6ea54789327ec77a8a564d95c101b2a4c9aa3642cbb139fe06dba24148518f

Contents?: true

Size: 1.23 KB

Versions: 16

Compression:

Stored size: 1.23 KB

Contents

$:.unshift 'lib'
require 'microformat'

class XOXO < Microformat
  @@parents  = %w[ol ul]
  @@children = %w[li]

  xpath_build =  proc { |element| element.map { |e| "/#{e}" } * ' | '  }
  @@children_xpath = xpath_build[@@children]
  @@parents_xpath  = xpath_build[@@parents]

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

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

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

  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

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

  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

  class Label < String
    attr_accessor :url
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
mofo-0.2.11 ./lib/mofo/xoxo.rb
mofo-0.2.13 ./lib/mofo/xoxo.rb
mofo-0.2.5 ./lib/mofo/xoxo.rb
mofo-0.2.3 ./lib/mofo/xoxo.rb
mofo-0.2.4 ./lib/mofo/xoxo.rb
mofo-0.2.6 ./lib/mofo/xoxo.rb
mofo-0.2.1 lib/mofo/xoxo.rb
mofo-0.2.10 ./lib/mofo/xoxo.rb
mofo-0.2.12 ./lib/mofo/xoxo.rb
mofo-0.2.14 ./lib/mofo/xoxo.rb
mofo-0.2.15 ./lib/mofo/xoxo.rb
mofo-0.2.16 ./lib/mofo/xoxo.rb
mofo-0.2.2 lib/mofo/xoxo.rb
mofo-0.2 lib/mofo/xoxo.rb
mofo-0.2.8 ./lib/mofo/xoxo.rb
mofo-0.2.9 ./lib/mofo/xoxo.rb