Class: RDF::Vocabulary
- Inherits:
-
Object
- Object
- RDF::Vocabulary
- Defined in:
- lib/rdf/vocab/extensions.rb
Direct Known Subclasses
RDF::Vocab::DWC, RDF::Vocab::LDP, RDF::Vocab::LRMI, RDF::Vocab::MA, RDF::Vocab::MO, RDF::Vocab::OG, RDF::Vocab::OGC, RDF::Vocab::XHTML, RDF::Vocab::XHV
Defined Under Namespace
Modules: VocabFormatExtensions
Class Method Summary collapse
-
._orig_each {|klass| ... } ⇒ Enumerator
Enumerates known RDF vocabulary classes.
- .each(&block) ⇒ Object
-
.to_html(graph: nil, prefixes: nil, jsonld: nil, template: nil) ⇒ String
Generate HTML+RDFa representation, specific to vocabularies.
-
.value_to_html(property, value, tag) ⇒ Object
Create HTML for values (Helper method, needs to be public).
Class Method Details
._orig_each {|klass| ... } ⇒ Enumerator
Enumerates known RDF vocabulary classes.
17 |
# File 'lib/rdf/vocab/extensions.rb', line 17 alias_method :_orig_each, :each |
.each(&block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rdf/vocab/extensions.rb', line 18 def each(&block) # This is needed since all vocabulary classes are defined using # Ruby's autoloading facility, meaning that `@@subclasses` will be # empty until each subclass has been touched or require'd. RDF::Vocab::VOCABS.each do |n, params| begin require "rdf/vocab/#{n}" rescue LoadError # Transient error warn "Failed to load #{n}" end end _orig_each(&block) end |
.to_html(graph: nil, prefixes: nil, jsonld: nil, template: nil) ⇒ String
Generate HTML+RDFa representation, specific to vocabularies. This uses generated JSON-LD and a Haml template.
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/rdf/vocab/extensions.rb', line 276 def to_html(graph: nil, prefixes: nil, jsonld: nil, template: nil) # Find namespaces used in the vocabulary graph = RDF::Graph.new {|g| each_statement {|s| g << s}} if graph.nil? || graph.empty? # Get JSON as an object json = case jsonld when String then ::JSON.parse(File.read jsonld) when Hash then jsonld else ::JSON.parse(to_jsonld(graph: graph, prefixes: prefixes)) end raise "Expected JSON-LD data within the '@graph' key" unless json.has_key?('@graph') template ||= File.("../../../../etc/template.erb", __FILE__) prefixes = vocab_prefixes(graph).merge(prefixes || {}) prefixes[:owl] = RDF::OWL.to_uri.to_s # Make sure ontology is typed json['@graph']['@type'] ||= ['owl:Ontology'] jld_context = ::JSON::LD::Context.new.parse([json['@context'], json['@graph']['@context']]) # Expand the JSON-LD to normalize accesses = ::JSON::LD::API.(json).first .delete('@reverse') # Re-compact keys = .inject({}) do |memo, (k, v)| term = RDF::Vocabulary.find_term(k) k = term.pname if term memo.merge(k => v) end # Normalize label accessors ['rdfs:label'] ||= %w(dc:title dc11:title skos:prefLabel).inject(nil) do |memo, key| memo || [key] end || [{'@value' => json['@graph']['@id']}] %w(rdfs_classes rdfs_properties rdfs_datatypes rdfs_instances).each do |section| next unless json['@graph'][section] json['@graph'][section].each do |node| node['rdfs:label'] ||= %w(dc:title dc11:title skos:prefLabel).inject do |memo, key| memo || node[key] end || [{'@value' => node['@id']}] end end # Expand each part separately, as well. %w(rdfs_classes rdfs_properties rdfs_datatypes rdfs_instances).each do |section| next unless json['@graph'][section] = ::JSON::LD::API.(json['@graph'][section], expandContext: jld_context) # Re-compact keys [section] = .map do |node| node.inject({}) do |memo, (k, v)| term = RDF::Vocabulary.find_term(k) k = term.pname if term memo.merge(k => v) end end end # Template invoked with expanded JSON-LD with outer object including `rdfs_classes`, `rdfs_properties`, and `rdf_instances` sections. case template when /.haml$/ require 'haml' haml = Haml::Engine.new(File.read(template)) haml.render(self, ont: , context: json['@context'], prefixes: prefixes) when /.erb$/ require 'erubis' eruby = Erubis::FastEruby.new(File.read(template)) result = eruby.evaluate(binding: self, ont: , context: json['@context'], prefixes: prefixes) else raise "Unknown template type #{template}. Should have '.erb' or '.haml' extension" end end |
.value_to_html(property, value, tag) ⇒ Object
Create HTML for values (Helper method, needs to be public)
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/rdf/vocab/extensions.rb', line 354 def value_to_html(property, value, tag) value.map do |v| %(<#{tag} property="#{property}") + if v['@value'] (v['@language'] ? %( lang="#{v['@language']}") : "") + (v['@type'] ? %( datatype="#{RDF::URI(v['@type']).pname}") : "") + %(>#{v['@value']}) elsif v['@id'] %( resource="#{RDF::URI(v['@id']).pname}">#{RDF::URI(v['@id']).pname}) else raise "Unknown value type: #{v.inspect}, #{property}" end + %(</#{tag}>) end.join("\n") end |