Class: RDF::Vocabulary

Inherits:
Object show all
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-vocab-799235274ed7/lib/rdf/vocab/extensions.rb

Defined Under Namespace

Modules: VocabFormatExtensions

Class Method Summary collapse

Class Method Details

._orig_each {|klass| ... } ⇒ Enumerator

Enumerates known RDF vocabulary classes.

Yields:

  • (klass)

Yield Parameters:

  • klass (Class)

Returns:

  • (Enumerator)


17
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-vocab-799235274ed7/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 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-vocab-799235274ed7/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.

Parameters:

  • :graph (RDF::Queryable)

    Optional graph, otherwise uses statements from vocabulary.

  • Prefixes (Hash{#to_sym => String})

    to add to output

  • jsonld (String, Hash)

    If not provided, the to_jsonld method is used to generate it.

  • template (String)

    The path to a Haml or ERB template used to generate the output using the JSON-LD serialization

Returns:



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 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-vocab-799235274ed7/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.expand_path("../../../../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
  expanded = ::JSON::LD::API.expand(json).first
  expanded.delete('@reverse')

  # Re-compact keys
  expanded = expanded.inject({}) do |memo, (k, v)|
    term = RDF::Vocabulary.find_term(k)
    k = term.pname if term
    memo.merge(k => v)
  end

  # Normalize label accessors
  expanded['rdfs:label'] ||= %w(dc:title dc11:title skos:prefLabel).inject(nil) do |memo, key|
    memo || expanded[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]
    expanded_section = ::JSON::LD::API.expand(json['@graph'][section], expandContext: jld_context)
    # Re-compact keys
    expanded[section] = expanded_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: expanded, context: json['@context'], prefixes: prefixes)
  when /.erb$/
    require 'erubis'
    eruby = Erubis::FastEruby.new(File.read(template))
    result = eruby.evaluate(binding: self, ont: expanded, 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 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-vocab-799235274ed7/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