Class: TTL2HTML::App

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/ttl2html.rb

Constant Summary collapse

QB_ORDER_URI =
"http://purl.org/linked-data/cube#order"
SCHEMA_POSITION_URI =
"http://schema.org/position"
SCHEMA_POSITION_URI_S =
"https://schema.org/position"
SHACL_ORDER_URI =
"http://www.w3.org/ns/shacl#order"

Instance Method Summary collapse

Methods included from Util

#_uri_mapping_to_path, #make_mapping_uris_cache, #safe_output_path, #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



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/ttl2html.rb', line 421

def build_breadcrumbs(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 << build_breadcrumbs_sub(parent, template)
            results += build_breadcrumbs(parent, template, depth + 1)
            return results
          end
        elsif data_target[e["property"]]
          data_target[e["property"]].each do |parent|
            results << build_breadcrumbs_sub(parent, template, e["label"])
            results += build_breadcrumbs(parent, template, depth + 1)
            return results
          end
        end
      end
    end
  end
  results
end

#build_breadcrumbs_sub(parent, template, label_prop = nil) ⇒ Object



462
463
464
465
466
467
468
469
470
# File 'lib/ttl2html.rb', line 462

def build_breadcrumbs_sub(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



198
199
200
201
202
203
204
# File 'lib/ttl2html.rb', line 198

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



188
189
190
191
192
193
194
195
196
197
# File 'lib/ttl2html.rb', line 188

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

#cleanupObject



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/ttl2html.rb', line 682

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]
    html_file = safe_output_path(html_file)
    if html_file and File.file? html_file
      dirs << File.dirname(html_file)
      File.unlink html_file
    end
    ttl_file = uri_mapping_to_path(uri, @config, ".ttl")
    ttl_file = File.join(@config[:output_dir], ttl_file) if @config[:output_dir]
    ttl_file = safe_output_path(ttl_file)
    if ttl_file and File.file? ttl_file
      dirs << File.dirname(ttl_file)
      File.unlink ttl_file
    end
  end
  index_html = "index.html"
  index_html = File.join(@config[:output_dir], "index.html") if @config[:output_dir]
  index_html = safe_output_path(index_html)
  if index_html and @config[:top_class] and File.file? 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]
  about_html = safe_output_path(about_html)
  if about_html and File.file? about_html
    File.unlink about_html
  end

  dirs = dirs.uniq.sort_by{|e| -(e.size) }
  #p dirs
  dirs.each do |dir|
    dir = safe_output_path(dir)
    next unless dir
    next if dir == File.expand_path(".") # failsafe...
    next if @config[:output_dir] and dir == File.expand_path(@config[:output_dir]) # failsafe...
    if dir and File.exist?(dir) and File.directory?(dir)
      FileUtils.remove_entry_secure(dir)
    end
  end
end

#collect_inverse_triples(object, triples = Set.new, visited = Set.new) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ttl2html.rb', line 152

def collect_inverse_triples(object, triples = Set.new, visited = Set.new)
  return triples if visited.include?(object.to_s)
  visited << object.to_s
  if @data_inverse.key?(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
  end
  triples
end

#collect_inverse_triples_for_bnode(node, triples, visited) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/ttl2html.rb', line 165

def collect_inverse_triples_for_bnode(node, triples, visited)
  return triples if visited.include?(node)
  visited << node
  if @data.key?(node)
    @data[node].each do |predicate, objects|
      objects.each do |object|
        triples << [node, predicate.to_s, object]
        if object.to_s.start_with?("_:")
          collect_inverse_triples_for_bnode(object.to_s, triples, visited)
        end
      end
    end
  end
  if @data_inverse.key?(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
  end
  triples
end

#each_data(label = :each_data) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/ttl2html.rb', line 258

def each_data(label = :each_data)
  progressbar_options = {
    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.count("/")), -(local_path.size), local_path ]
  end
  Parallel.each(data, progress: progressbar_options) do |uri|
    next if not uri.start_with? @config[:base_uri]
    yield uri, @data[uri]
  end
end

#extract_derivedfrom(data) ⇒ Object



569
570
571
572
573
574
575
576
577
578
579
# File 'lib/ttl2html.rb', line 569

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



580
581
582
583
584
585
586
587
588
589
590
591
592
593
# File 'lib/ttl2html.rb', line 580

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_toplevelObject



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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/ttl2html.rb', line 594

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



517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/ttl2html.rb', line 517

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_versionsObject



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/ttl2html.rb', line 549

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



205
206
207
208
209
210
211
212
# File 'lib/ttl2html.rb', line 205

def find_inverse_roots(by_subject)
  all_subjects = by_subject.keys
  all_objects  = by_subject.values.flat_map { |preds| preds.values.flatten }.uniq
  roots = all_subjects.reject do |subject|
    all_objects.include?(subject)
  end
  roots.sort_by { |subject| sort_key_for_resource(subject) }
end

#format_inverse_object(object, by_subject, ref_count, visited, depth = 1, turtle_writer = RDF::Turtle::Writer.new(nil, prefixes: @prefix)) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/ttl2html.rb', line 237

def format_inverse_object(object, by_subject, ref_count, visited, depth = 1, turtle_writer = RDF::Turtle::Writer.new(nil, prefixes: @prefix))
  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, turtle_writer)
  end
end

#format_inverse_subject(subject, by_subject, ref_count, visited, depth = 1) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/ttl2html.rb', line 213

def format_inverse_subject(subject, by_subject, ref_count, visited, depth = 1)
  turtle_writer = RDF::Turtle::Writer.new(nil, prefixes: @prefix)
  props = by_subject[subject]
  return format_node(subject, turtle_writer) 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 = format_uri(subject, turtle_writer) << " "
    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, turtle_writer)
    end.join(", ")
    format_uri(predicate, turtle_writer) << " " << objects
  end.join(";\n#{inner}")
  head + body + tail
end

#format_node(value, writer = RDF::Turtle::Writer.new(nil, prefixes: @prefix)) ⇒ Object



248
249
250
251
252
253
254
255
256
# File 'lib/ttl2html.rb', line 248

def format_node(value, writer = RDF::Turtle::Writer.new(nil, prefixes: @prefix))
  if value.to_s.start_with?("_:")
    value.to_s
  elsif RDF::URI::IRI =~ value.to_s
    format_uri(value, writer)
  else
    writer.format_literal(value)
  end
end

#format_turtle(subject, depth = 1, force = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ttl2html.rb', line 106

def format_turtle(subject, depth = 1, force = false)
  turtle_writer = RDF::Turtle::Writer.new(nil, prefixes: @prefix)
  result = ""
  #p [:format_turtle, subject, depth, force]
  return result if !force && @cache[:output_turtle_files].include?(subject)
  if subject =~ /^_:/
    result << "[\n#{"  "*depth}"
  else
    result << format_uri(subject) << "\n#{"  "*depth}"
  end
  result << @data[subject.to_s].keys.sort.map do |predicate|
    str = format_uri(predicate, turtle_writer) << " "
    #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)
    end.map do |object|
      if /^_:/ =~ object.to_s # blank node:
        format_turtle(object, depth + 1, force)
      elsif RDF::URI::IRI =~ object.to_s
        format_uri(object, turtle_writer)
      else
        if object.respond_to?(:datatype) and object.datatype?
          datatype = format_uri(object.datatype) # to add @used_prefixes
        end
        turtle_writer.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



142
143
144
145
146
147
148
149
150
151
# File 'lib/ttl2html.rb', line 142

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

#format_uri(uri, writer = RDF::Turtle::Writer.new(nil, prefixes: @prefix)) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/ttl2html.rb', line 99

def format_uri(uri, writer = RDF::Turtle::Writer.new(nil, prefixes: @prefix))
  result = writer.format_uri(RDF::URI(uri))
  if result =~ /^#{RDF::Turtle::Terminals::PN_PREFIX}?:/
    @used_prefixes << result.split(":").first
  end
  result
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_filesObject



677
678
679
680
# File 'lib/ttl2html.rb', line 677

def output_files
  output_html_files
  output_turtle_files if @config[:output_turtle]
end

#output_html_filesObject



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
394
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
# File 'lib/ttl2html.rb', line 274

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)
  FileUtils.mkdir_p(@config[:output_dir]) if @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] = build_breadcrumbs(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
    file = safe_output_path(file)
    if file
      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
  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
      index_html = safe_output_path(index_html)
      template.output_to(index_html, param) if index_html
    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.expand_shape(@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
    about_html = safe_output_path(about_html)
    template.output_to(about_html, param) if about_html
  end
end

#output_turtle_filesObject



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
# File 'lib/ttl2html.rb', line 648

def output_turtle_files
  FileUtils.mkdir_p(@config[:output_dir]) if @config[:output_dir]
  each_data(:output_turtle_files) do |uri, v|
    file = uri_mapping_to_path(uri, @config, ".ttl")
    if @config[:output_dir]
      file = File.join(@config[:output_dir], file)
    end
    file = safe_output_path(file)
    next if not file
    dir = File.dirname(file)
    FileUtils.mkdir_p(dir) if not File.exist?(dir)
    @cache ||= {}
    @cache[:output_turtle_files] = Set.new
    @used_prefixes = Set.new
    str = format_turtle(uri)
    str << format_turtle_inverse(uri)
    File.open(file, "w") do |io|
      @used_prefixes.each do |prefix|
        if prefix.empty?
          io.puts "@prefix : <#{@prefix[nil]}>."
        else
          io.puts "@prefix #{prefix}: <#{@prefix[prefix.to_sym]}>."
        end
      end
      io.puts str.strip
    end
  end
end

#shapes2labels(shapes) ⇒ Object



496
497
498
499
500
501
502
503
504
505
# File 'lib/ttl2html.rb', line 496

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



506
507
508
509
510
511
512
513
514
515
# File 'lib/ttl2html.rb', line 506

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



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/ttl2html.rb', line 472

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) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ttl2html.rb', line 80

def sort_key_for_resource(resource)
  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]
    schema_position = @data[resource.to_s][SCHEMA_POSITION_URI_S].first.to_i if @data[resource.to_s][SCHEMA_POSITION_URI_S]
    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 @data[resource.to_s]
    resource_str = "{" + @data[resource.to_s].sort_by do |p, o|
      [p, o]
    end.join("\t") + "}"
    [ schema_position, qb_order, shacl_order, resource_str ]
  else
    [ schema_position, qb_order, shacl_order, resource.to_s ]
  end
end