lib/rdf/microdata/reader.rb in rdf-microdata-0.1.1 vs lib/rdf/microdata/reader.rb in rdf-microdata-0.1.2

- old
+ new

@@ -37,10 +37,12 @@ # whether to canonicalize parsed literals # @option options [Boolean] :intern (true) # whether to intern all parsed URIs # @option options [#to_s] :base_uri (nil) # the base URI to use when resolving relative URIs + # @option options [Boolean] :rdf_terms (false) + # Generate URIs for itemprop terms based on namespace of itemtype # @option options [Array] :debug # Array to place debug messages # @return [reader] # @yield [reader] `self` # @yieldparam [RDF::Reader] reader @@ -236,11 +238,10 @@ object = uri(el.attribute('cite'), el.base || base) add_debug(el, "blockquote: cite=#{object}") add_triple(el, base, RDF::DC.source, object) end - # 5. Let memory be a mapping of items to subjects, initially empty. # 6. For each element that is also a top-level microdata item, run the following steps: # * Generate the triples for the item. Pass a reference to memory as the item/subject list. # Let result be the subject returned. # * Generate the following triple: @@ -285,22 +286,22 @@ # 2. Add a mapping from item to subject in memory, if there isn't one already. memory[item][:subject] ||= subject # 3. If item has an item type and that item type is an absolute URL, let type be that item type. # Otherwise, let type be the empty string. - type = uri(item.attribute('itemtype')) + rdf_type = type = uri(item.attribute('itemtype')) type = '' unless type.absolute? if type != '' add_triple(item, subject, RDF.type, type) # 4.2. If type does not contain a U+0023 NUMBER SIGN character (#), then append a # to type. type += '#' unless type.to_s.include?('#') # 4.3. If type does not have a : after its #, append a : to type. type += ':' unless type.to_s.match(/\#:/) elsif fallback_type add_debug(item, "gentrips(5.2): fallback_type=#{fallback_type}, fallback_name=#{fallback_name}") - type = fallback_type + rdf_type = type = fallback_type # 5.2. If type does not contain a U+0023 NUMBER SIGN character (#), then append a # to type. type += '#' unless type.to_s.include?('#') # 5.3. If type does not have a : after its #, append a : to type. type += ':' unless type.to_s.match(/\#:/) # 5.4. If the last character of type is not a :, %20 to type. @@ -333,9 +334,13 @@ add_debug(element, "gentrips(6.1.3): value=#{value.inspect}") predicate = if name_uri.absolute? name_uri + elsif @options[:rdf_terms] + # Use the URI of the type to create URIs for @itemprop terms + add_debug(element, "gentrips: rdf_type=#{rdf_type}") + predicate = RDF::URI(rdf_type.to_s.sub(/(?<=[\/\#])[^\/\#]*$/, name)) elsif !name.include?(':') s = type.to_s s += '%20' unless s[-1,1] == ':' s += name RDF::MD[s.gsub('#', '%23')] \ No newline at end of file