Debian Codegraph @

graph/formats/rdf/testdata/mk_vocab.rb

raw symbols SCIP
1 #!/usr/bin/env ruby
2 # Generate vocab.jsonld and vocab.html from vocab.ttl and vocab_template.
3 #
4 # Generating vocab.jsonld is equivalent to running the following:
5 #
6 # jsonld --compact --context vocab_context.jsonld --input-format ttl vocab.ttl -o vocab.jsonld
7 require 'linkeddata'
8 require 'haml'
9 require 'active_support'
10
11 File.open("vocab.jsonld", "w") do |f|
12 r = RDF::Repository.load("vocab.ttl")
13 JSON::LD::API.fromRDF(r, useNativeTypes: true) do |expanded|
14 # Remove leading/trailing and multiple whitespace from rdf:comments
15 expanded.each do |o|
16 c = o[RDF::RDFS.comment.to_s].first['@value']
17 o[RDF::RDFS.comment.to_s].first['@value'] = c.strip.gsub(/\s+/m, ' ')
18 end
19 JSON::LD::API.compact(expanded, File.open("vocab_context.jsonld")) do |compacted|
20 # Create vocab.jsonld
21 f.write(compacted.to_json(JSON::LD::JSON_STATE))
22
23 # Create vocab.html using vocab_template.haml and compacted vocabulary
24 template = File.read("vocab_template.haml")
25
26 html = Haml::Engine.new(template, :format => :html5).render(self,
27 ontology: compacted['@graph'].detect {|o| o['@id'] == "http://json-ld.github.io/normalization/tests/vocab#"},
28 classes: compacted['@graph'].select {|o| o['@type'] == "rdfs:Class"}.sort_by {|o| o['rdfs:label']},
29 properties: compacted['@graph'].select {|o| o['@type'] == "rdf:Property"}.sort_by {|o| o['rdfs:label']}
30 )
31 File.open("vocab.html", "w") {|fh| fh.write html}
32 end
33 end
34 end