Class: RDF::Microdata::Reader
- Inherits:
-
Reader
- Object
- Reader
- RDF::Microdata::Reader
- Includes:
- Expansion, Util::Logger
- Defined in:
- lib/rdf/microdata/reader.rb,
lib/rdf/microdata/reader/nokogiri.rb
Overview
An Microdata parser in Ruby
Based on processing rules, amended with the following:
Defined Under Namespace
Modules: Nokogiri
Constant Summary collapse
- URL_PROPERTY_ELEMENTS =
%w(a area audio embed iframe img link object source track video)
Instance Attribute Summary collapse
-
#implementation ⇒ Module
readonly
Returns the HTML implementation module for this reader instance.
-
#memory ⇒ Hash{Object => RDF::Resource}
readonly
Maps RDF elements (items) to resources.
Class Method Summary collapse
-
.options ⇒ Object
Reader options.
Instance Method Summary collapse
-
#base_uri ⇒ Hash{Symbol => RDF::URI}
Returns the base URI determined by this reader.
-
#each_statement {|statement| ... }
Iterates the given block for each RDF statement in the input.
-
#each_triple {|subject, predicate, object| ... }
Iterates the given block for each RDF triple in the input.
-
#initialize(input = $stdin, **options) {|reader| ... } ⇒ reader
constructor
Initializes the Microdata reader instance.
Methods included from Expansion
Constructor Details
#initialize(input = $stdin, **options) {|reader| ... } ⇒ reader
Initializes the Microdata reader instance.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rdf/microdata/reader.rb', line 97 def initialize(input = $stdin, **, &block) super do @library = :nokogiri require "rdf/microdata/reader/#{@library}" @implementation = Nokogiri self.extend(@implementation) input.rewind if input.respond_to?(:rewind) initialize_html(input, **) rescue log_fatal($!., exception: RDF::ReaderError) log_error("Empty document") if root.nil? log_error(doc_errors.map(&:message).uniq.join("\n")) if !doc_errors.empty? log_debug('', "library = #{@library}") # Load registry begin registry_uri = [:registry] || RDF::Microdata::DEFAULT_REGISTRY log_debug('', "registry = #{registry_uri.inspect}") Registry.load_registry(registry_uri) rescue JSON::ParserError => e log_fatal("Failed to parse registry: #{e.}", exception: RDF::ReaderError) if (root.nil? && validate?) end if block_given? case block.arity when 0 then instance_eval(&block) else block.call(self) end end end end |
Instance Attribute Details
#implementation ⇒ Module (readonly)
Returns the HTML implementation module for this reader instance.
23 24 25 |
# File 'lib/rdf/microdata/reader.rb', line 23 def implementation @implementation end |
#memory ⇒ Hash{Object => RDF::Resource} (readonly)
Returns maps RDF elements (items) to resources.
26 27 28 |
# File 'lib/rdf/microdata/reader.rb', line 26 def memory @memory end |
Class Method Details
.options ⇒ Object
Reader options
43 44 45 46 47 48 49 50 51 |
# File 'lib/rdf/microdata/reader.rb', line 43 def self. super + [ RDF::CLI::Option.new( symbol: :rdfa, datatype: TrueClass, on: ["--rdfa"], description: "Transform and parse as RDFa.") {true}, ] end |
Instance Method Details
#base_uri ⇒ Hash{Symbol => RDF::URI}
Returns the base URI determined by this reader.
36 37 38 |
# File 'lib/rdf/microdata/reader.rb', line 36 def base_uri @options[:base_uri] end |
#each_statement {|statement| ... }
This method returns an undefined value.
Iterates the given block for each RDF statement in the input.
Reads to graph and performs expansion if required.
139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/rdf/microdata/reader.rb', line 139 def each_statement(&block) if block_given? @callback = block # parse parse_whole_document(@doc, base_uri) if validate? && log_statistics[:error] raise RDF::ReaderError, "Errors found during processing" end end enum_for(:each_statement) end |
#each_triple {|subject, predicate, object| ... }
This method returns an undefined value.
Iterates the given block for each RDF triple in the input.
161 162 163 164 165 166 167 168 |
# File 'lib/rdf/microdata/reader.rb', line 161 def each_triple(&block) if block_given? each_statement do |statement| block.call(*statement.to_triple) end end enum_for(:each_triple) end |