-
Notifications
You must be signed in to change notification settings - Fork 23
/
Rakefile
85 lines (72 loc) · 2.54 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
75
76
77
78
79
80
81
82
83
84
85
# Build generated files
require 'rdf/turtle'
require 'json/ld'
require 'haml'
require 'htmlbeautifier'
task default: :index
MANIFESTS = Dir.glob("**/manifest*.ttl").reject {|f| f.include?('-az')}
desc "Build HTML manifests"
task index: MANIFESTS.
select {|m| File.exist?(m)}.
map {|m| m.sub(/manifest(.*)\.ttl$/, 'index\1.html')}
MANIFESTS.each do |ttl|
html = ttl.sub(/manifest(.*)\.ttl$/, 'index\1.html')
base = 'https://w3c.github.io/rdf-tests/' + ttl.sub('manifest.ttl', '')
# Find frame closest to file
frame_path, template_path = nil, nil
Pathname.new(ttl).ascend do |p|
f = File.join(p, 'manifest-frame.jsonld')
frame_path ||= f if File.exist?(f)
t = File.join(p, 'template.haml')
template_path ||= t if File.exist?(t)
end
frame_path ||= 'manifest-frame.jsonld'
if template_path
desc "Build #{html}"
file html => [ttl, frame_path, template_path] do
puts "Generate #{html}"
frame, template, man = File.read(frame_path), File.read(template_path), nil
ttl_path, ttl_file = File.dirname(ttl), File.basename(ttl)
Dir.chdir(ttl_path) do
RDF::Reader.open(ttl_file, base_uri: base) do |reader|
out = JSON::LD::Writer.buffer(frame: JSON.parse(frame), base_uri: base, simple_compact_iris: true) do |writer|
writer << reader
end
man = JSON.parse(out)
if man.key?('@graph')
Kernel.abort "Expected manifest to not have a single @graph entry"
end
# Fix up test entries
Array(man['entries']).each do |entry|
# Fix results which aren't IRIs
if res = entry['mf:result'] && entry['mf:result']['@value']
entry.delete('mf:result')
entry['result'] = res == 'true'
end
# Fix some empty arrays (rdf-mt)
%w(recognizedDatatypes unrecognizedDatatypes).each do |p|
if entry["mf:#{p}"].is_a?(Hash) && entry["mf:#{p}"]['@list'] == []
entry[p] = []
entry.delete("mf:#{p}")
end
end
end
end
end
haml_runner = if Haml::VERSION >= "6"
Haml::Template.new(format: :html5) {template}
else
Haml::Engine.new(template, format: :html5)
end
File.open(html, "w") do |f|
rendered = haml_runner.render(self,
man: man,
haml_indent: true,
ttl: ttl.split('/').last
)
beautified = HtmlBeautifier.beautify(rendered)
f.write(beautified)
end
end
end
end