| Class | Syndication::AbstractParser |
| In: |
lib/syndication/common.rb
|
| Parent: | Object |
Shared parts of parser code for Atom and RSS. This is an abstract class; Atom::Parser and RSS::Parser are the concrete classes which actually parse syndication feeds.
You don’t need to know about anything below in order to use the library.
The basic parsing strategy is:
in the parse tree that corresponds to where we are in the XML tree. To use a metaphor, it’s the object where parse tree growth is occurring.
text. The parser sends the events to the current_object, which replies with what the new current_object should be after the event has been dealt with.
objects of the parse tree.
derived from tags in a standard way once namespaces have been standardized.
| KNOWN_NAMESPACES | = | { 'http://purl.org/dc/elements/1.1/' => 'dc', 'http://purl.org/dc/terms/' => 'dcterms', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' => 'rdf', 'http://purl.org/rss/1.0/modules/content/' => 'content', 'http://www.itunes.com/DTDs/Podcast-1.0.dtd' => 'itunes', 'http://www.w3.org/1999/xhtml' => 'xhtml', 'http://schemas.google.com/g/2005' => 'gd', 'http://rssnamespace.org/feedburner/ext/1.0' => 'feedburner' } | A Hash of namespace URLs the module knows about, returning the standard prefix to remap to. |
Create a new AbstractParser. The optional argument consists of text to parse.
# File lib/syndication/common.rb, line 148 def initialize(text = nil) reset # Initialize mapping from tags to classes, which only needs to be done # once and not reset. Concrete classes which do actual parsing will # fill the hash. @tag_to_class = Hash.new parse(text) if text end
Supposed to be called when REXML finds a CDATA-encoded piece of text.
# File lib/syndication/common.rb, line 275 def cdata(s) # For content_encoded we re-encode, because (a) the API for RSS content # module provides both encoded and decoded results to the user, and # (b) REXML doesn't always seem to pass CDATA via this callback method. # For other elements, we keep the text decoded. if @textstack.last if @tagstack.last == 'content:encoded' @textstack.last << "<![CDATA[#{s}]]>" else @textstack.last << s end end end
Process a namespace definition for the given prefix and namespace definition URL.
If we recongnize the URL, we set up a mapping from their prefix to our canonical choice of prefix.
# File lib/syndication/common.rb, line 211 def define_namespace(prefix, url) myprefix = KNOWN_NAMESPACES[url] if myprefix @namespacemap[prefix] = myprefix end end
Catch and ignore closing tags that don’t match anything open.
# File lib/syndication/common.rb, line 163 def end_tag(tag, current) return current end
Handle namespace translation for a raw tag.
# File lib/syndication/common.rb, line 190 def handle_namespace(tag, attrs = nil) if attrs and tag.match(/^(rss|\w+:rdf|\w+:div)$/i) for key in attrs.keys if key.match(/xmlns:(\w+)/i) define_namespace($1, attrs[key]) end end end if tag.match(/(\w+):(\w+)/) if @namespacemap[$1] tag = "#{@namespacemap[$1]}:#{$2}" end end return tag end
Parse the text provided. Returns a Syndication::Atom::Feed or Syndication::RSS::Feed object, according to which concrete Parser class is being used. The second argument is optional and determines the parser engine to use. The default is REXML. To use TagSoup, pass in the value Syndication::TagSoup
# File lib/syndication/common.rb, line 184 def parse(text, classname = REXML::Document) classname.parse_stream(text, self) return @parsetree end
Reset the parser ready to parse a new feed.
# File lib/syndication/common.rb, line 168 def reset @current_object = @parsetree @tagstack = Array.new @textstack = Array.new @xhtml = '' @xhtmlmode = false @namespacemap = Hash.new # @parsetree is set up by the concrete classes end
Catch any stuff that drops right through the parse tree, and simply ignore it.
# File lib/syndication/common.rb, line 159 def store(tag, obj) end
Called when REXML finds the end of an XML element.
# File lib/syndication/common.rb, line 241 def tag_end(endtag) endtag = handle_namespace(endtag, nil) # There are two tasks to perform: 1. store the data from the buffers, # and 2. work out if we need to close out any objects in the parse # tree and move the current object pointer begin # Store the top text buffer that's on the stacks by passing it to the # current object along with its tag. Repeat until we find a stacked # tag which matches the endtag, or run out of buffers. tag = @tagstack.pop text = @textstack.pop if text text.strip! if text.length > 0 and @current_object @current_object.store(tag, text) end end end until tag == endtag or @tagstack.length == 0 # Pass the tag end event to the current object to find out what the # new current object should be. if @current_object @current_object = @current_object.tag_end(endtag, @current_object) end end