Class: Reddy::RdfaParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/reddy/rdfaparser.rb

Overview

An RDFa parser in Ruby

Based on processing rules described here: www.w3.org/TR/rdfa-syntax/#s_model

Ben Adida 2008-05-07 Gregg Kellogg 2009-08-04

Defined Under Namespace

Classes: EvaluationContext

Instance Attribute Summary collapse

Attributes inherited from Parser

#debug, #doc, #graph

Instance Method Summary collapse

Methods inherited from Parser

#initialize, n3_parser, parse, rdfa_parser, rdfxml_parser

Constructor Details

This class inherits a constructor from Reddy::Parser

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



12
13
14
# File 'lib/reddy/rdfaparser.rb', line 12

def namespace
  @namespace
end

Instance Method Details

#parse(stream, uri = nil, options = {}, &block) ⇒ Graph

Parse XHTML+RDFa document from a string or input stream to closure or graph.

Optionally, the stream may be a Nokogiri::HTML::Document or Nokogiri::XML::Document With a block, yeilds each statement with URIRef, BNode or Literal elements

options[:debug]

Array to place debug messages

options[:strict]

Raise Error if true, continue with lax parsing, otherwise

Parameters:

  • stream:: (IO)

    the HTML+RDFa IO stream, string, Nokogiri::HTML::Document or Nokogiri::XML::Document

  • uri:: (String)

    the URI of the document

  • options:: (Hash)

    Parser options, one of

Returns:

  • (Graph)

    Returns the graph containing parsed triples

Raises:

  • (Error)

    Raises RdfError if strict

Author:

  • Gregg Kellogg



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/reddy/rdfaparser.rb', line 62

def parse(stream, uri = nil, options = {}, &block) # :yields: triple
  super

  @doc = case stream
  when Nokogiri::HTML::Document then stream
  when Nokogiri::XML::Document then stream
  else   Nokogiri::XML.parse(stream, uri)
  end
  
  raise ParserException, "Empty document" if @doc.nil? && @strict
  @callback = block

  # If the doc has a default, use that as "html"
  ns = @doc.namespaces["xmlns"]
  ns ||= "http://www.w3.org/1999/xhtml" # FIXME: intuite from DOCTYPE, or however
  @namespace = Namespace.new(ns, "html") if ns
  
  # parse
  parse_whole_document(@doc, @uri)

  @graph
end