Sha256: a16553332bdb2ebf3ad859b231bfc2bfe3007a0f096e130773cf15ff4ea44c87
Contents?: true
Size: 1.12 KB
Versions: 6
Compression:
Stored size: 1.12 KB
Contents
require "nokogiri" require "microformat/collection" require "microformat/selectors" module Microformat class Parser def self.parse(doc, options = {}) # ensure the document is parsed unless doc.respond_to?(:css) doc = Nokogiri::HTML(doc) end # return the collection new(doc, options).collection end attr_reader :doc, :options def initialize(doc, options) @doc = doc @options = options parse(elements) end def parse(elements) i = 0; while i < [limit, elements.size].min do collection << elements[i] i += 1 end end def limit @limit ||= options[:limit] || Float::INFINITY end def selectors @selectors ||= Selectors.filter(options[:filter]) end def collection @collection ||= Collection.new end private def elements @elements ||= [].tap do |elements| while doc.css(selectors.join(", ")).length > 0 node = doc.css(selectors.join(", ")).first node.remove elements.push node end end end end end
Version data entries
6 entries across 6 versions & 2 rubygems