Class: TTL2HTML::App
Constant Summary collapse
- QB_ORDER_URI =
"http://purl.org/linked-data/cube#order"- SCHEMA_POSITION_URI =
"http://schema.org/position"- SHACL_ORDER_URI =
"http://www.w3.org/ns/shacl#order"
Instance Method Summary collapse
- #build_breadcrumbs(uri, template, depth = 0) ⇒ Object
- #build_breadcrumbs_sub(parent, template, label_prop = nil) ⇒ Object
- #build_object_ref_count(triples) ⇒ Object
- #build_subject_index(triples) ⇒ Object
- #cleanup ⇒ Object
- #collect_inverse_triples(object, triples = Set.new, visited = Set.new) ⇒ Object
- #collect_inverse_triples_for_bnode(node, triples, visited) ⇒ Object
- #each_data(label = :each_data) ⇒ Object
- #extract_derivedfrom(data) ⇒ Object
- #extract_license(data) ⇒ Object
- #extract_toplevel ⇒ Object
- #extract_version_metadata(data) ⇒ Object
- #extract_versions ⇒ Object
- #find_inverse_roots(by_subject) ⇒ Object
- #format_inverse_object(object, by_subject, ref_count, visited, depth = 1) ⇒ Object
- #format_inverse_subject(subject, by_subject, ref_count, visited, depth = 1) ⇒ Object
- #format_node(value) ⇒ Object
- #format_turtle(subject, depth = 1, force = false) ⇒ Object
- #format_turtle_inverse(object) ⇒ Object
-
#initialize(config = "config.yml") ⇒ App
constructor
A new instance of App.
- #load_config(file) ⇒ Object
- #load_turtle(file) ⇒ Object
- #output_files ⇒ Object
- #output_html_files ⇒ Object
- #output_turtle_files ⇒ Object
- #shapes2labels(shapes) ⇒ Object
- #shapes2orders(shapes) ⇒ Object
- #shapes_parse(shapes) ⇒ Object
- #sort_key_for_resource(resource, depth = 1) ⇒ Object
Methods included from Util
#_uri_mapping_to_path, #make_mapping_uris_cache, #uri_mapping_to_path
Constructor Details
#initialize(config = "config.yml") ⇒ App
Returns a new instance of App.
18 19 20 21 22 23 24 25 26 |
# File 'lib/ttl2html.rb', line 18 def initialize(config = "config.yml") @config = load_config(config) if not @config[:base_uri] raise "load_config: base_uri not found" end @data = {} @data_inverse = {} @prefix = {} end |
Instance Method Details
#build_breadcrumbs(uri, template, depth = 0) ⇒ Object
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/ttl2html.rb', line 395 def (uri, template, depth = 0) results = [] data = @data[uri] if @config[:breadcrumbs] if depth == 0 first_label = template.get_title(data) first_label = data[@config[:breadcrumbs].first["label"]].first if @config[:breadcrumbs].first["label"] and data[@config[:breadcrumbs].first["label"]] results << { label: first_label } end @config[:breadcrumbs].each do |e| data_target = data data_target = @data_inverse[uri] if e["inverse"] if data_target if e["property"].kind_of? Array parent = nil data_target_sub = data_target e["property"].each do |prop| if data_target_sub[prop["property"]] data_target_sub[prop["property"]].each do |o| parent = o data_target_sub = @data[parent] end end end if parent results << (parent, template) results += (parent, template, depth + 1) return results end elsif data_target[e["property"]] data_target[e["property"]].each do |parent| results << (parent, template, e["label"]) results += (parent, template, depth + 1) return results end end end end end results end |
#build_breadcrumbs_sub(parent, template, label_prop = nil) ⇒ Object
436 437 438 439 440 441 442 443 444 |
# File 'lib/ttl2html.rb', line 436 def (parent, template, label_prop = nil) data_parent = @data[parent] label = template.get_title(data_parent) label = data_parent[label_prop].first if label_prop and data_parent[label_prop] { uri: parent, label: label, } end |
#build_object_ref_count(triples) ⇒ Object
176 177 178 179 180 181 182 |
# File 'lib/ttl2html.rb', line 176 def build_object_ref_count(triples) count = Hash.new(0) triples.each do |_subject, _predicate, object| count[object] += 1 if object.to_s.start_with?("_:") end count end |
#build_subject_index(triples) ⇒ Object
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/ttl2html.rb', line 166 def build_subject_index(triples) by_subject = Hash.new { |h, k| h[k] = Hash.new { |hh, kk| hh[kk] = [] } } triples.each do |subject, predicate, object| by_subject[subject][predicate] << object end by_subject.each_value do |predicates| predicates.each_value(&:uniq!) end by_subject end |
#cleanup ⇒ Object
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 |
# File 'lib/ttl2html.rb', line 647 def cleanup dirs = [] @data.select do |uri, v| uri.start_with? @config[:base_uri] end.sort_by do |uri, v| -(uri.size) end.each do |uri, v| html_file = uri_mapping_to_path(uri, @config, ".html") html_file = File.join(@config[:output_dir], html_file) if @config[:output_dir] dirs << File.dirname(html_file) File.unlink html_file if File.exist? html_file ttl_file = uri_mapping_to_path(uri, @config, ".ttl") ttl_file = File.join(@config[:output_dir], ttl_file) if @config[:output_dir] File.unlink ttl_file if File.exist? ttl_file dir = uri.sub(@config[:base_uri], "") dir = File.join(@config[:output_dir], dir) if @config[:output_dir] dirs << dir end index_html = "index.html" index_html = File.join(@config[:output_dir], "index.html") if @config[:output_dir] if @config[:top_class] and File.exist? index_html File.unlink index_html end about_html = (@config[:about_file] || "about.html") about_html = File.join(@config[:output_dir], about_html) if @config[:output_dir] File.unlink about_html if File.exist? about_html dirs = dirs.uniq.sort_by{|e| -(e.size) } #p dirs dirs.each do |dir| next if dir == "." # failsafe... next if dir == @config[:output_dir] # failsafe... FileUtils.remove_entry_secure(dir) if File.exist? dir end end |
#collect_inverse_triples(object, triples = Set.new, visited = Set.new) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/ttl2html.rb', line 140 def collect_inverse_triples(object, triples = Set.new, visited = Set.new) return triples if object.to_s.start_with?("_:") return triples unless object.to_s.start_with?(@config[:base_uri].to_s) return triples unless @data_inverse.key?(object.to_s) return triples if visited.include?(object.to_s) visited << object.to_s @data_inverse[object.to_s].each do |predicate, subjects| subjects.each do |subject| triples << [subject.to_s, predicate.to_s, object.to_s] collect_inverse_triples_for_bnode(subject.to_s, triples, visited) if subject.to_s.start_with?("_:") end end triples end |
#collect_inverse_triples_for_bnode(node, triples, visited) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ttl2html.rb', line 154 def collect_inverse_triples_for_bnode(node, triples, visited) return triples unless @data_inverse.key?(node) return triples if visited.include?(node) visited << node @data_inverse[node].each do |predicate, subjects| subjects.each do |subject| triples << [subject.to_s, predicate.to_s, node] collect_inverse_triples_for_bnode(subject.to_s, triples, visited) if subject.to_s.start_with?("_:") end end triples end |
#each_data(label = :each_data) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/ttl2html.rb', line 237 def each_data(label = :each_data) = { output: $stderr, title: label.to_s, format: "(%t) %a %e %P% Processed: %c from %C" } data = @data.keys.sort_by do|uri| local_path = uri_mapping_to_path(uri, @config, ".html") #p [ local_path.size, local_path.count("/"), local_path ] [ local_path.size, local_path.count("/"), local_path ] end.reverse Parallel.each(data, progress: ) do |uri| next if not uri.start_with? @config[:base_uri] yield uri, @data[uri] end end |
#extract_derivedfrom(data) ⇒ Object
543 544 545 546 547 548 549 550 551 552 553 |
# File 'lib/ttl2html.rb', line 543 def extract_derivedfrom(data) derivedfrom = {} wasDerivedFrom = data["http://www.w3.org/ns/prov#wasDerivedFrom"]&.first if @data[wasDerivedFrom] derivedfrom = { url: @data[wasDerivedFrom]["http://www.w3.org/1999/02/22-rdf-syntax-ns#value"]&.first, label: @data[wasDerivedFrom]["http://www.w3.org/2000/01/rdf-schema#label"] } end derivedfrom end |
#extract_license(data) ⇒ Object
554 555 556 557 558 559 560 561 562 563 564 565 566 567 |
# File 'lib/ttl2html.rb', line 554 def extract_license(data) license = {} if data["http://purl.org/dc/terms/license"] license_data = @data[data["http://purl.org/dc/terms/license"].first] if license_data license[:url] = license_data["http://www.w3.org/1999/02/22-rdf-syntax-ns#value"]&.first license[:icon] = license_data["http://xmlns.com/foaf/0.1/thumbnail"]&.first license[:label] = license_data["http://www.w3.org/2000/01/rdf-schema#label"] elsif data["http://purl.org/dc/terms/license"].first =~ URI::regexp license[:url] = license[:label] = data["http://purl.org/dc/terms/license"].first end end license end |
#extract_toplevel ⇒ Object
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 |
# File 'lib/ttl2html.rb', line 568 def extract_toplevel result = {} toplevel = nil @data.each do |s, v| if @data[s]["http://purl.org/pav/hasCurrentVersion"] toplevel = s end end data = @data[toplevel.to_s] if toplevel license = extract_license(data) derivedfrom = extract_derivedfrom(data) if data["http://purl.org/dc/terms/publisher"] publisher_data = @data[data["http://purl.org/dc/terms/publisher"].first] email = publisher_data["http://xmlns.com/foaf/0.1/mbox"]&.first contact = { email: email } name = publisher_data["http://xmlns.com/foaf/0.1/name"] contact[:name] = name if name members = [] if publisher_data["http://xmlns.com/foaf/0.1/member"] publisher_data["http://xmlns.com/foaf/0.1/member"].each do |member| member_data = @data[member] members << { name: member_data["http://xmlns.com/foaf/0.1/name"], org: member_data["http://www.w3.org/2006/vcard/ns#organization-name"] } end contact[:members] = members end end if data["http://rdfs.org/ns/void#sparqlEndpoint"] endpoint = data["http://rdfs.org/ns/void#sparqlEndpoint"].first end if data["http://www.w3.org/ns/dcat#accessService"] service = data["http://www.w3.org/ns/dcat#accessService"].first service_data = @data[service] if service_data endpoint = service_data["http://www.w3.org/ns/dcat#endpointURL"]&.first endpoint_landingpage = service_data["http://www.w3.org/ns/dcat#landingPage"]&.first end end result = { uri: toplevel.to_s, description: data["http://purl.org/dc/terms/description"], license: license, contact: contact, endpoint: endpoint, endpoint_landingpage: endpoint_landingpage, derivedfrom: derivedfrom, } end result end |
#extract_version_metadata(data) ⇒ Object
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'lib/ttl2html.rb', line 491 def (data) description = data["http://purl.org/dc/terms/description"] link = nil if not description qrev = data["http://www.w3.org/ns/prov#qualifiedRevision"]&.first if @data[qrev] description = @data[qrev]["http://www.w3.org/2000/01/rdf-schema#comment"] link = @data[qrev]["http://www.w3.org/2000/01/rdf-schema#seeAlso"]&.first end end subset = [] if data["http://rdfs.org/ns/void#subset"] data["http://rdfs.org/ns/void#subset"].each do |s| abort "#{s} not found" if not @data[s] subset << (@data[s]) end end date = data["http://purl.org/pav/createdOn"]&.first date = data["http://purl.org/dc/terms/issued"]&.first if date.nil? return { version: data["http://purl.org/pav/version"]&.first, triples: data["http://rdfs.org/ns/void#triples"]&.first, datadump: data["http://rdfs.org/ns/void#dataDump"]&.first, bytesize: data["http://www.w3.org/ns/dcat#byteSize"]&.first, date: date, description: description, subset: subset, link: link, license: extract_license(data), derivedfrom: extract_derivedfrom(data), } end |
#extract_versions ⇒ Object
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/ttl2html.rb', line 523 def extract_versions versions = [] ["http://purl.org/pav/hasVersion", "http://purl.org/pav/hasCurrentVersion", "http://purl.org/dc/terms/hasVersion"].each do |prop| objects = [] @data.each do |s, v| if @data[s][prop] objects += @data[s][prop] end end objects.each do |o| uri = o.to_s version = @data[uri] next if not version next if not version["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"] next if not version["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"].include? "http://rdfs.org/ns/void#Dataset" versions << (version) end end versions.sort_by{|v| [ v[:date], v[:version] ] } end |
#find_inverse_roots(by_subject) ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/ttl2html.rb', line 183 def find_inverse_roots(by_subject) all_subjects = by_subject.keys all_objects = by_subject.values.flat_map { |preds| preds.values.flatten }.uniq all_subjects.reject do |subject| subject.start_with?("_:") || all_objects.include?(subject) end.sort end |
#format_inverse_object(object, by_subject, ref_count, visited, depth = 1) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/ttl2html.rb', line 213 def format_inverse_object(object, by_subject, ref_count, visited, depth = 1) if object.to_s.start_with?("_:") && by_subject.key?(object.to_s) if ref_count[object.to_s] <= 1 format_inverse_subject(object.to_s, by_subject, ref_count, visited, depth) else object.to_s end else format_node(object) end end |
#format_inverse_subject(subject, by_subject, ref_count, visited, depth = 1) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/ttl2html.rb', line 190 def format_inverse_subject(subject, by_subject, ref_count, visited, depth = 1) props = by_subject[subject] return format_node(subject) if props.nil? || props.empty? indent = " " * (depth - 1) inner = " " * depth if subject.start_with?("_:") return "[]" if visited.include?(subject) visited = visited.dup visited << subject head = "[\n#{inner}" tail = "\n#{indent}]" else head = "<#{subject}> " tail = "" end body = props.keys.sort.map do |predicate| objects = props[predicate].sort.map do |object| format_inverse_object(object, by_subject, ref_count, visited, depth + 1) end.join(", ") "<#{predicate}> #{objects}" end.join(";\n#{inner}") head + body + tail end |
#format_node(value) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/ttl2html.rb', line 224 def format_node(value) turtle = RDF::Turtle::Writer.new if value.to_s.start_with?("_:") value.to_s elsif RDF::URI::IRI =~ value.to_s "<#{value}>" elsif value.respond_to?(:first) && value.first.kind_of?(Symbol) turtle.format_literal(RDF::Literal.new(value[1], language: value[0])) else turtle.format_literal(value) end end |
#format_turtle(subject, depth = 1, force = false) ⇒ Object
95 96 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/ttl2html.rb', line 95 def format_turtle(subject, depth = 1, force = false) turtle = RDF::Turtle::Writer.new result = "" #p [:format_turtle, subject, depth, force] return result if !force && @cache[:output_turtle_files].include?(subject) if subject =~ /^_:/ result << "[\n#{" "*depth}" else result << "<#{subject}>\n#{" "*depth}" end result << @data[subject.to_s].keys.sort.map do |predicate| str = "<#{predicate}> " #p [subject, predicate, @data[subject.to_s][predicate]] str << @data[subject.to_s][predicate].sort_by do |object| #p [subject, predicate, object, depth] sort_key_for_resource(object, depth) end.map do |object| if /^_:/ =~ object.to_s # blank node: format_turtle(object, depth + 1, force) elsif RDF::URI::IRI =~ object.to_s turtle.format_uri(RDF::URI.new object) elsif object.respond_to?(:first) and object.first.kind_of?(Symbol) turtle.format_literal(RDF::Literal.new(object[1], language: object[0])) else turtle.format_literal(object) end end.join(", ") str end.join(";\n#{" "*depth}") result << "." if not subject =~ /^_:/ result << "\n" result << "#{" "*(depth-1)}]" if subject =~ /^_:/ @cache[:output_turtle_files] << subject unless force result end |
#format_turtle_inverse(object) ⇒ Object
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ttl2html.rb', line 130 def format_turtle_inverse(object) triples = collect_inverse_triples(object) return "" if triples.empty? by_subject = build_subject_index(triples) ref_count = build_object_ref_count(triples) roots = find_inverse_roots(by_subject) roots.map do |root| "#{format_inverse_subject(root, by_subject, ref_count, Set.new, 1)}.\n" end.join end |
#load_config(file) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ttl2html.rb', line 28 def load_config(file) config = { output_turtle: true } File.open(file) do |io| YAML.safe_load(io, permitted_classes: [Regexp]).each do |k, v| config[k.intern] = v end end [ :css_file, :javascript_file ].each do |k| if config[k] config[k] = Array(config[k]).flatten end end config end |
#load_turtle(file) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ttl2html.rb', line 43 def load_turtle(file) $stderr.puts "loading #{file}..." count = 0 subjects = Set.new if file.end_with?(".gz") io = Zlib::GzipReader.open(file) else io = File.open(file) end RDF::Format.for(:turtle).reader.new(io) do |reader| reader.each_statement do |statement| s = statement.subject v = statement.predicate o = statement.object count += 1 subjects << s @data[s.to_s] ||= {} @data[s.to_s][v.to_s] ||= [] if o.is_a? RDF::URI or o.is_a? RDF::Node @data[s.to_s][v.to_s] << o.to_s @data_inverse[o.to_s] ||= {} @data_inverse[o.to_s][v.to_s] ||= [] @data_inverse[o.to_s][v.to_s] << s.to_s else @data[s.to_s][v.to_s] << o end end @prefix.merge! reader.prefixes end $stderr.puts "#{count} triples. #{subjects.size} subjects." @data end |
#output_files ⇒ Object
642 643 644 645 |
# File 'lib/ttl2html.rb', line 642 def output_files output_html_files output_turtle_files if @config[:output_turtle] end |
#output_html_files ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/ttl2html.rb', line 253 def output_html_files template = Template.new("", @config) shapes = [] @data.each do |s, v| if v[RDF.type.to_s] and @data[s][RDF.type.to_s].include?("http://www.w3.org/ns/shacl#NodeShape") shapes << s end end labels = shapes2labels(shapes) versions = extract_versions toplevel = extract_toplevel about_required = template.find_template_path("about.html") || !shapes.empty? || !versions.empty? || !toplevel.empty? about_file = (@config[:about_file] || "about.html") if about_required @config[:labels_with_class] ||= {} labels.each do |klass, props| props.each do |property, label| @config[:labels_with_class][klass] ||= {} if @config[:labels_with_class][klass][property] next else @config[:labels_with_class][klass][property] = template.get_language_literal(label) end end end @config[:orders_with_class] = shapes2orders(shapes) Dir.mkdir @config[:output_dir] if @config[:output_dir] and not File.exist? @config[:output_dir] template = Template.new("default.html.erb", @config) each_data(:output_html_files) do |uri, v| param = @config.dup param[:uri] = uri param[:turtle_uri] = uri + ".ttl" param[:data] = v param[:data_inverse] = @data_inverse[uri] param[:data_inverse_global] = @data_inverse param[:data_global] = @data param[:title] = template.get_title(v) if @config[:subtitle_property] or @config[:subtitle_property_perclass] param[:subtitle] = template.get_subtitle(v) end if param[:breadcrumbs] param[:breadcrumbs_items] = (uri, template) end file = uri_mapping_to_path(uri, @config, ".html") #p [:each_data, uri, file] if @config[:output_dir] file = File.join(@config[:output_dir], file) end if template.find_template_path("_default.html.erb") param[:additional_content] = template.to_html_raw("_default.html.erb", param) end param[:about_file] = about_file if about_required template.output_to(file, param) end index_html = "index.html" index_html = File.join(@config[:output_dir], "index.html") if @config[:output_dir] if @config.has_key? :top_class subjects = [] @data.each do |s, v| if @data[s][RDF.type.to_s] and @data[s][RDF.type.to_s].include?(@config[:top_class]) subjects << s end end if subjects.empty? $stderr.puts "WARN: top_class parameter specified as [#{@config[:top_class]}], but there is no instance data." else template = Template.new("index.html.erb", @config) param = @config.dup param[:class_label] = template.get_title(@data[@config[:top_class]], nil) param[:class_label] ||= @config[:top_class].split(/[\/\#]/).last.capitalize param[:data_global] = @data param[:data_inverse_global] = @data_inverse param[:versions] = versions param[:toplevel] = toplevel param[:description] = template.to_html_raw("description.html", {}) if template.find_template_path("description.html") subjects.sort_by do |subject| sort_key_for_resource(subject) end.each do |subject| objects = [] if @config.has_key? :top_additional_property @config[:top_additional_property].each do |property| if @data[subject][property] objects += @data[subject][property] end end end param[:index_data] ||= [] param[:index_data] << { subject.to_s => objects } end param[:output_file] = index_html param[:index_list] = template.to_html_raw("index-list.html.erb", param) param[:about_file] = about_file if about_required template.output_to(index_html, param) end end if about_required about_html = about_file about_html = File.join(@config[:output_dir], about_html) if @config[:output_dir] template = Template.new("about.html.erb", @config) param = @config.dup param[:about_file] = about_file param[:content] = template.to_html_raw("about.html", {}) if template.find_template_path("about.html") param[:data_global] = @data param[:versions] = versions param[:toplevel] = toplevel param[:description] = template.to_html_raw("description.html", {}) if template.find_template_path("description.html") param[:shapes] = {} shapes.each do |subject| orders = [] if param[:shape_orders] param[:shape_orders].index(subject) orders << ( param[:shape_orders].index(subject) or Float::INFINITY ) end orders << subject label = comment = nil target_class = @data[subject.to_s]["http://www.w3.org/ns/shacl#targetClass"] if target_class target_class = target_class.first if @data[target_class] label = template.get_title(@data[target_class], nil) comment = template.get_language_literal(@data[target_class]["http://www.w3.org/2000/01/rdf-schema#comment"]) if @data[target_class]["http://www.w3.org/2000/01/rdf-schema#comment"] else label = template.format_property(target_class, param) end else label = template.get_title(@data[subject.to_s]) end html = template.(@data, subject.to_s, @prefix) next if html.nil? param[:shapes][subject] = { label: label, comment: comment, html: html, target_class: target_class, order: orders, } end template.output_to(about_html, param) end end |
#output_turtle_files ⇒ Object
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 |
# File 'lib/ttl2html.rb', line 622 def output_turtle_files Dir.mkdir @config[:output_dir] if @config[:output_dir] and not File.exist? @config[:output_dir] each_data(:output_turtle_files) do |uri, v| file = uri_mapping_to_path(uri, @config, ".ttl") if @config[:output_dir] Dir.mkdir @config[:output_dir] if not File.exist? @config[:output_dir] file = File.join(@config[:output_dir], file) end dir = File.dirname(file) FileUtils.mkdir_p(dir) if not File.exist?(dir) @cache ||= {} @cache[:output_turtle_files] = Set.new str = format_turtle(uri) str << format_turtle_inverse(uri) open(file, "w") do |io| io.puts str.strip end end end |
#shapes2labels(shapes) ⇒ Object
470 471 472 473 474 475 476 477 478 479 |
# File 'lib/ttl2html.rb', line 470 def shapes2labels(shapes) labels = {} shapes_parse(shapes) do |target_class, property| path = @data[property]["http://www.w3.org/ns/shacl#path"].first name = @data[property]["http://www.w3.org/ns/shacl#name"] labels[target_class] ||= {} labels[target_class][path] = name end labels end |
#shapes2orders(shapes) ⇒ Object
480 481 482 483 484 485 486 487 488 489 |
# File 'lib/ttl2html.rb', line 480 def shapes2orders(shapes) orders = {} shapes_parse(shapes) do |target_class, property| path = @data[property]["http://www.w3.org/ns/shacl#path"].first order = @data[property]["http://www.w3.org/ns/shacl#order"] orders[target_class] ||= {} orders[target_class][path] = order&.first&.to_i end orders end |
#shapes_parse(shapes) ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/ttl2html.rb', line 446 def shapes_parse(shapes) shapes.each do |shape| target_class = @data[shape]["http://www.w3.org/ns/shacl#targetClass"]&.first if target_class properties = @data[shape.to_s]["http://www.w3.org/ns/shacl#property"] if @data[shape.to_s]["http://www.w3.org/ns/shacl#or"] properties ||= [] node_list = @data[shape.to_s]["http://www.w3.org/ns/shacl#or"].first while node_list and @data[node_list] do sub_shape = @data[node_list]["http://www.w3.org/1999/02/22-rdf-syntax-ns#first"].first if @data[sub_shape] and @data[sub_shape]["http://www.w3.org/ns/shacl#property"] properties += @data[sub_shape]["http://www.w3.org/ns/shacl#property"] end node_list = @data[node_list]["http://www.w3.org/1999/02/22-rdf-syntax-ns#rest"].first end end if not properties.empty? properties.each do |property| yield target_class, property end end end end end |
#sort_key_for_resource(resource, depth = 1) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ttl2html.rb', line 78 def sort_key_for_resource(resource, depth = 1) qb_order = Float::INFINITY schema_position = Float::INFINITY shacl_order = Float::INFINITY if @data[resource.to_s] qb_order = @data[resource.to_s][QB_ORDER_URI].first.to_i if @data[resource.to_s][QB_ORDER_URI] schema_position = @data[resource.to_s][SCHEMA_POSITION_URI].first.to_i if @data[resource.to_s][SCHEMA_POSITION_URI] shacl_order = @data[resource.to_s][SHACL_ORDER_URI].first.to_i if @data[resource.to_s][SHACL_ORDER_URI] end if resource.to_s =~ /^_:/ and depth < 5 [ schema_position, qb_order, shacl_order, format_turtle(resource, depth + 1, true) ] else [ schema_position, qb_order, shacl_order, resource.to_s] end end |