@@ -12,76 +12,75 @@ task index: MANIFESTS.
12
12
select { |m | File . exist? ( m ) } .
13
13
map { |m | m . sub ( /manifest(.*)\. ttl$/ , 'index\1.html' ) }
14
14
15
- desc "Build JSON-LD manifests"
16
- task manifests_jsonld : MANIFESTS .
17
- map { |m | m . sub ( '.ttl' , '.jsonld' ) }
18
-
19
15
MANIFESTS . each do |ttl |
20
- jsonld = ttl . sub ( '.ttl' , '.jsonld' )
21
16
html = ttl . sub ( /manifest(.*)\. ttl$/ , 'index\1.html' )
22
17
23
18
# Find frame closest to file
24
- frame , exe , template = nil , nil , nil
19
+ frame , template = nil , nil
25
20
Pathname . new ( ttl ) . ascend do |p |
26
21
f = File . join ( p , 'manifest-frame.jsonld' )
27
22
frame ||= f if File . exist? ( f )
28
23
29
- e = File . join ( p , 'manifest-post' )
30
- exe ||= e if File . executable? ( e )
31
-
32
24
t = File . join ( p , 'template.haml' )
33
25
template ||= t if File . exist? ( t )
34
26
end
35
27
frame ||= 'manifest-frame.jsonld'
36
28
37
29
if template
38
30
desc "Build #{ html } "
39
- file html => [ jsonld , template ] do
31
+ file html => [ ttl , frame , template ] do
40
32
puts "Genrate #{ html } "
41
- temp = File . read ( template )
42
- man = ::JSON . load ( File . read ( jsonld ) )
33
+ temp , man = File . read ( template ) , nil
43
34
44
- File . open ( html , "w" ) do |f |
45
- f . write Haml ::Engine . new ( temp , format : :html5 ) .
46
- render ( self ,
47
- man : man ,
48
- ttl : ttl . split ( '/' ) . last ,
49
- jsonld : jsonld . split ( '/' ) . last
50
- )
51
- end
52
- end
53
- end
35
+ RDF ::Reader . open ( ttl ) do |reader |
36
+ out = JSON ::LD ::Writer . buffer ( frame : frame , simple_compact_iris : true ) do |writer |
37
+ writer << reader
38
+ end
54
39
55
- desc "Build #{ jsonld } "
56
- file jsonld => [ ttl , frame ] do
57
- puts "Genrate #{ jsonld } "
58
- RDF :: Reader . open ( ttl ) do |reader |
59
- out = JSON :: LD :: Writer . buffer ( frame : frame , simple_compact_iris : true ) do | writer |
60
- writer << reader
61
- end
40
+ man = JSON . parse ( out )
41
+ if man [ '@graph' ] . is_a? ( Array ) && man [ '@graph' ] . length == 1
42
+ # Remove '@graph'
43
+ man [ '@graph' ] [ 0 ] . each do |k , v |
44
+ man [ k ] = v
45
+ end
46
+ man . delete ( '@graph' )
62
47
63
- man = JSON . parse ( out )
64
- if man [ '@graph' ] . is_a? ( Array ) && man [ '@graph ' ] . length == 1
65
- # Remove '@graph'
66
- man [ '@graph' ] [ 0 ] . each do | k , v |
67
- man [ k ] = v
68
- end
69
- man . delete ( '@graph' )
48
+ # Remove nil 'entries'
49
+ man . delete ( 'mf:entries' ) if man [ 'mf:entries ' ] . nil?
50
+
51
+ # Replace .ttl includes with .jsonld includes
52
+ if man [ 'include' ] . is_a? ( Array )
53
+ man [ 'include' ] = man [ 'include' ] . map { | i | i . sub ( '.ttl' , '.jsonld' ) }
54
+ end
70
55
71
- # Remove nil 'entries'
72
- man . delete ( 'mf:entries' ) if man [ 'mf:entries' ] . nil?
56
+ # Fix up test entries
57
+ Array ( man [ 'entries' ] ) . each do |entry |
58
+ # Fix results which aren't IRIs
59
+ if res = entry [ 'mf:result' ] && entry [ 'mf:result' ] [ '@value' ]
60
+ entry . delete ( 'mf:result' )
61
+ entry [ 'result' ] = res == 'true'
62
+ end
73
63
74
- # Replace .ttl includes with .jsonld includes
75
- if man [ 'include' ] . is_a? ( Array )
76
- man [ 'include' ] = man [ 'include' ] . map { |i | i . sub ( '.ttl' , '.jsonld' ) }
64
+ # Fix some empty arrays (rdf-mt)
65
+ %w( recognizedDatatypes unrecognizedDatatypes ) . each do |p |
66
+ if entry [ "mf:#{ p } " ] . is_a? ( Hash ) && entry [ "mf:#{ p } " ] [ '@list' ] == [ ]
67
+ entry [ p ] = [ ]
68
+ entry . delete ( "mf:#{ p } " )
69
+ end
70
+ end
71
+ end
72
+ else
73
+ Kernel . abort "Expected manifest to have a single @graph entry"
77
74
end
78
- else
79
- Kernel . abort "Expected manifest to have a single @graph entry"
80
75
end
81
- File . open ( jsonld , "w" ) { |f | f . puts man . to_json ( JSON ::LD ::JSON_STATE ) }
82
- end
83
76
84
- # If there is a post-processing script, run it
85
- system exe , jsonld , jsonld if exe
77
+ File . open ( html , "w" ) do |f |
78
+ f . write Haml ::Engine . new ( temp , format : :html5 ) .
79
+ render ( self ,
80
+ man : man ,
81
+ ttl : ttl . split ( '/' ) . last
82
+ )
83
+ end
84
+ end
86
85
end
87
86
end
0 commit comments