@@ -10,7 +10,7 @@ require 'getoptlong'
10
10
require 'open-uri'
11
11
require 'logger'
12
12
13
- def run ( input , options )
13
+ def run ( input , options , parser_options )
14
14
reader_class = RDF ::Reader . for ( options [ :input_format ] . to_sym )
15
15
raise "Reader not found for #{ options [ :input_format ] } " unless reader_class
16
16
@@ -19,38 +19,38 @@ def run(input, options)
19
19
20
20
# If input format is not JSON-LD, transform input to JSON-LD first
21
21
reader = if options [ :input_format ] != :jsonld
22
- reader_class . new ( input , options [ : parser_options] )
22
+ reader_class . new ( input , parser_options )
23
23
end
24
24
25
25
start = Time . new
26
26
if options [ :expand ]
27
- options = options . merge ( expandContext : options . delete ( :context ) ) if options . has_key? ( :context )
27
+ parser_options = parser_options . merge ( expandContext : parser_options . delete ( :context ) ) if parser_options . has_key? ( :context )
28
28
input = JSON ::LD ::API . fromRdf ( reader ) if reader
29
- output = JSON ::LD ::API . expand ( input , options )
29
+ output = JSON ::LD ::API . expand ( input , parser_options )
30
30
secs = Time . new - start
31
31
options [ :output ] . puts output . to_json ( JSON ::LD ::JSON_STATE )
32
32
STDERR . puts "Expanded in #{ secs } seconds." unless options [ :quiet ]
33
33
elsif options [ :compact ]
34
34
input = JSON ::LD ::API . fromRdf ( reader ) if reader
35
- output = JSON ::LD ::API . compact ( input , options [ :context ] , options )
35
+ output = JSON ::LD ::API . compact ( input , parser_options [ :context ] , parser_options )
36
36
secs = Time . new - start
37
37
options [ :output ] . puts output . to_json ( JSON ::LD ::JSON_STATE )
38
38
STDERR . puts "Compacted in #{ secs } seconds." unless options [ :quiet ]
39
39
elsif options [ :flatten ]
40
40
input = JSON ::LD ::API . fromRdf ( reader ) if reader
41
- output = JSON ::LD ::API . flatten ( input , options [ :context ] , options )
41
+ output = JSON ::LD ::API . flatten ( input , parser_options [ :context ] , parser_options )
42
42
secs = Time . new - start
43
43
options [ :output ] . puts output . to_json ( JSON ::LD ::JSON_STATE )
44
44
STDERR . puts "Flattened in #{ secs } seconds." unless options [ :quiet ]
45
45
elsif options [ :frame ]
46
46
input = JSON ::LD ::API . fromRdf ( reader ) if reader
47
- output = JSON ::LD ::API . frame ( input , options [ :frame ] , options )
47
+ output = JSON ::LD ::API . frame ( input , parser_options [ :frame ] , parser_options )
48
48
secs = Time . new - start
49
49
options [ :output ] . puts output . to_json ( JSON ::LD ::JSON_STATE )
50
50
STDERR . puts "Framed in #{ secs } seconds." unless options [ :quiet ]
51
51
else
52
- options = options . merge ( expandContext : options . delete ( :context ) ) if options . has_key? ( :context )
53
- parser_options = options [ :parser_options ] . merge ( standard_prefixes : true )
52
+ parser_options = parser_options . merge ( expandContext : parser_options . delete ( :context ) ) if parser_options . has_key? ( :context )
53
+ parser_options [ : standard_prefixes] = true
54
54
reader ||= JSON ::LD ::Reader . new ( input , parser_options )
55
55
num = 0
56
56
RDF ::Writer . for ( options [ :output_format ] ) . new ( options [ :output ] , parser_options ) do |w |
@@ -86,7 +86,6 @@ parser_options = {
86
86
}
87
87
88
88
options = {
89
- parser_options : parser_options ,
90
89
output : STDOUT ,
91
90
output_format : :jsonld ,
92
91
input_format : :jsonld ,
@@ -141,49 +140,47 @@ opts = GetoptLong.new(*OPT_ARGS.map {|o| o[0..-2]})
141
140
opts . each do |opt , arg |
142
141
case opt
143
142
when '--debug' then logger . level = Logger ::DEBUG
144
- when '--compact' then options [ :compact ] = true
145
- when "--compactArrays" then options [ :compactArrays ] = ( arg || 'true' ) == 'true'
146
- when '--context' then options [ :context ] = arg
143
+ when '--compact' then parser_options [ :compact ] = true
144
+ when "--compactArrays" then parser_options [ :compactArrays ] = ( arg || 'true' ) == 'true'
145
+ when '--context' then parser_options [ :context ] = RDF :: URI ( arg ) . absolute? ? arg : File . open ( arg )
147
146
when '--evaluate' then input = arg
148
147
when '--expand' then options [ :expand ] = true
149
- when "--expanded" then options [ :expanded ] = ( arg || 'true' ) == 'true'
150
- when "--explicit" then options [ :compactArrays ] = ( arg || 'true' ) == 'true'
148
+ when "--expanded" then parser_options [ :expanded ] = ( arg || 'true' ) == 'true'
149
+ when "--explicit" then parser_options [ :compactArrays ] = ( arg || 'true' ) == 'true'
151
150
when '--format' then options [ :output_format ] = arg . to_sym
152
151
when '--flatten' then options [ :flatten ] = arg
153
- when '--frame' then options [ :frame ] = arg
152
+ when '--frame' then options [ :frame ] = parser_otpions [ :frame ] = RDF :: URI ( arg ) . absolute? ? arg : File . open ( arg )
154
153
when '--input-format' then options [ :input_format ] = arg . to_sym
155
- when "--omitDefault" then options [ :omitDefault ] = ( arg || 'true' ) == 'true'
154
+ when "--omitDefault" then parser_options [ :omitDefault ] = ( arg || 'true' ) == 'true'
156
155
when '--output' then options [ :output ] = File . open ( arg , "w" )
157
156
when '--parse-only' then options [ :parse_only ] = true
158
- when '--processingMode' then options [ :processingMode ] = arg
157
+ when '--processingMode' then parser_options [ :processingMode ] = arg
159
158
when '--quiet'
160
159
options [ :quiet ] = true
161
160
logger . level = Logger ::FATAL
162
- when "--rename_bnodes" then options [ :rename_bnodes ] = ( arg || 'true' ) == 'true'
163
- when "--requireAll" then options [ :requireAll ] = ( arg || 'true' ) == 'true'
161
+ when "--rename_bnodes" then parser_options [ :rename_bnodes ] = ( arg || 'true' ) == 'true'
162
+ when "--requireAll" then parser_options [ :requireAll ] = ( arg || 'true' ) == 'true'
164
163
when '--stream' then parser_options [ :stream ] = true
165
- when "--unique_bnodes" then options [ :unique_bnodes ] = ( arg || 'true' ) == 'true'
164
+ when "--unique_bnodes" then parser_options [ :unique_bnodes ] = ( arg || 'true' ) == 'true'
166
165
when '--uri' then parser_options [ :base ] = arg
167
166
when '--validate' then parser_options [ :validate ] = true
168
167
when '--help' then usage
169
168
when '--embed'
170
169
case arg
171
- when '@always' , '@never' , '@link' , '@last' , '@first '
172
- options [ :embed ] = arg
170
+ when '@always' , '@never' , '@link' , '@once '
171
+ parser_options [ :embed ] = arg
173
172
when 'true'
174
- options [ :embed ] = '@never'
173
+ parser_options [ :embed ] = '@never'
175
174
when 'false'
176
- options [ :embed ] = '@first'
175
+ parser_options [ :embed ] = '@first'
177
176
else
178
- STDERR . puts "--embed option takes one of @always, @never, @link, @first, or @last "
177
+ STDERR . puts "--embed option takes one of @always, @never, @link, or @once "
179
178
exit ( 1 )
180
179
end
181
180
end
182
181
end
183
182
184
183
# Hack
185
- options [ :parser_options ] [ :context ] = options [ :context ] if parser_options [ :stream ]
186
-
187
184
if !( options . keys & [ :expand , :compact , :flatten , :frame ] ) . empty? &&
188
185
( parser_options [ :stream ] || options [ :output_format ] != :jsonld )
189
186
STDERR . puts "Incompatible options"
@@ -192,11 +189,11 @@ end
192
189
193
190
if ARGV . empty?
194
191
s = input ? input : $stdin. read
195
- run ( StringIO . new ( s ) , options )
192
+ run ( StringIO . new ( s ) , options , parser_options )
196
193
else
197
194
ARGV . each do |file |
198
195
# Call with opened files
199
- RDF ::Util ::File . open_file ( file , options ) { |f | run ( f , options ) }
196
+ RDF ::Util ::File . open_file ( file , options ) { |f | run ( f , options , parser_options ) }
200
197
end
201
198
end
202
199
puts
0 commit comments