Module: Crawlr::Parser
- Defined in:
- lib/crawlr/parser.rb
Overview
Document parsing and callback execution engine.
The Parser module provides the core document processing functionality for the Crawlr framework. It efficiently parses HTML and XML content using Nokogiri and executes registered callbacks on matching elements. The module optimizes performance by grouping callbacks by document format to minimize parsing overhead.
Class Method Summary collapse
-
.apply_callbacks(content:, callbacks:, context:) ⇒ void
Applies registered callbacks to parsed document content.
Class Method Details
.apply_callbacks(content:, callbacks:, context:) ⇒ void
This method returns an undefined value.
Applies registered callbacks to parsed document content
This method is the main entry point for document processing. It efficiently handles multiple callbacks by grouping them by document format, ensuring that each piece of content is parsed only once per format regardless of how many callbacks are registered for that format.
The method performs the following operations:
- Groups callbacks by document format (:html or :xml)
- Parses content once per format using appropriate Nokogiri parser
- Executes all callbacks for each format on the parsed document
- Extracts matching nodes using CSS or XPath selectors
- Calls callback blocks with matched nodes and context
162 163 164 165 166 167 168 169 170 |
# File 'lib/crawlr/parser.rb', line 162 def self.apply_callbacks(content:, callbacks:, context:) # Group callbacks by format to minimize parsing callbacks_by_format = callbacks.group_by { |cb| cb[:format] || :html } callbacks_by_format.each do |format, format_callbacks| doc = parse_content(format, content) format_callbacks.each { |callback| apply_callback(doc, callback, context) } end end |