Skip to content

Commit a48b3e3

Browse files
committed
* Update CLI options with base coming from base_uri, and local frame/context files opened.
* For arguments which are URLs, if it is not an absolute URI, read it into a StringIO.
1 parent 42b0cb6 commit a48b3e3

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

lib/json/ld/format.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ def self.cli_commands
5858
out = options[:output] || $stdout
5959
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
6060
options = options.merge(expandContext: options.delete(:context)) if options.has_key?(:context)
61+
options[:base] ||= options[:base_uri]
6162
if options[:format] == :jsonld
6263
if files.empty?
63-
# If files are empty, either use options[:execute]
64+
# If files are empty, either use options[:evaluate] or STDIN
6465
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
6566
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
6667
JSON::LD::API.expand(input, validate: false, **options) do |expanded|
@@ -93,9 +94,10 @@ def self.cli_commands
9394
raise ArgumentError, "Compacting requires a context" unless options[:context]
9495
out = options[:output] || $stdout
9596
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
97+
options[:base] ||= options[:base_uri]
9698
if options[:format] == :jsonld
9799
if files.empty?
98-
# If files are empty, either use options[:execute]
100+
# If files are empty, either use options[:evaluate] or STDIN
99101
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
100102
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
101103
JSON::LD::API.compact(input, options[:context], **options) do |compacted|
@@ -126,7 +128,7 @@ def self.cli_commands
126128
control: :url2,
127129
use: :required,
128130
on: ["--context CONTEXT"],
129-
description: "Context to use when compacting.") {|arg| RDF::URI(arg)},
131+
description: "Context to use when compacting.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
130132
]
131133
},
132134
flatten: {
@@ -137,9 +139,10 @@ def self.cli_commands
137139
lambda: ->(files, **options) do
138140
out = options[:output] || $stdout
139141
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
142+
options[:base] ||= options[:base_uri]
140143
if options[:format] == :jsonld
141144
if files.empty?
142-
# If files are empty, either use options[:execute]
145+
# If files are empty, either use options[:evaluate] or STDIN
143146
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
144147
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
145148
JSON::LD::API.flatten(input, options[:context], **options) do |flattened|
@@ -173,9 +176,10 @@ def self.cli_commands
173176
raise ArgumentError, "Framing requires a frame" unless options[:frame]
174177
out = options[:output] || $stdout
175178
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
179+
options[:base] ||= options[:base_uri]
176180
if options[:format] == :jsonld
177181
if files.empty?
178-
# If files are empty, either use options[:execute]
182+
# If files are empty, either use options[:evaluate] or STDIN
179183
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
180184
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
181185
JSON::LD::API.frame(input, options[:frame], **options) do |framed|
@@ -207,7 +211,7 @@ def self.cli_commands
207211
control: :url2,
208212
use: :required,
209213
on: ["--frame FRAME"],
210-
description: "Frame to use when serializing.") {|arg| RDF::URI(arg)}
214+
description: "Frame to use when serializing.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))}
211215
]
212216
},
213217
}

lib/json/ld/reader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def self.options
2020
control: :url2,
2121
datatype: RDF::URI,
2222
on: ["--expand-context CONTEXT"],
23-
description: "Context to use when expanding.") {|arg| RDF::URI(arg)},
23+
description: "Context to use when expanding.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
2424
RDF::CLI::Option.new(
2525
symbol: :extractAllScripts,
2626
datatype: TrueClass,

lib/json/ld/writer.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def self.options
9393
datatype: RDF::URI,
9494
control: :url2,
9595
on: ["--context CONTEXT"],
96-
description: "Context to use when compacting.") {|arg| RDF::URI(arg)},
96+
description: "Context to use when compacting.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
9797
RDF::CLI::Option.new(
9898
symbol: :embed,
9999
datatype: %w(@always @once @never),
@@ -107,6 +107,13 @@ def self.options
107107
control: :checkbox,
108108
on: ["--[no-]explicit"],
109109
description: "Only include explicitly declared properties in output (false)") {|arg| arg},
110+
RDF::CLI::Option.new(
111+
symbol: :frame,
112+
datatype: RDF::URI,
113+
control: :url2,
114+
use: :required,
115+
on: ["--frame FRAME"],
116+
description: "Frame to use when serializing.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
110117
RDF::CLI::Option.new(
111118
symbol: :lowercaseLanguage,
112119
datatype: TrueClass,
@@ -137,7 +144,7 @@ def self.options
137144
default: 'null',
138145
control: :select,
139146
on: ["--rdf-direction DIR", %w(i18n-datatype compound-literal)],
140-
description: "How to serialize literal direction (i18n-datatype compound-literal)") {|arg| RDF::URI(arg)},
147+
description: "How to serialize literal direction (i18n-datatype compound-literal)") {|arg| arg},
141148
RDF::CLI::Option.new(
142149
symbol: :requireAll,
143150
datatype: TrueClass,
@@ -202,7 +209,7 @@ def default_context=(url); @default_context = url; end
202209
end
203210

204211
##
205-
# Initializes the RDF-LD writer instance.
212+
# Initializes the JSON-LD writer instance.
206213
#
207214
# @param [IO, File] output
208215
# the output stream

0 commit comments

Comments
 (0)