Skip to content

Commit

Permalink
* Update CLI options with base coming from base_uri, and local frame/…
Browse files Browse the repository at this point in the history
…context files opened.

* For arguments which are URLs, if it is not an absolute URI, read it into a StringIO.
  • Loading branch information
gkellogg committed Nov 15, 2020
1 parent 42b0cb6 commit a48b3e3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
16 changes: 10 additions & 6 deletions lib/json/ld/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ def self.cli_commands
out = options[:output] || $stdout
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
options = options.merge(expandContext: options.delete(:context)) if options.has_key?(:context)
options[:base] ||= options[:base_uri]
if options[:format] == :jsonld
if files.empty?
# If files are empty, either use options[:execute]
# If files are empty, either use options[:evaluate] or STDIN
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
JSON::LD::API.expand(input, validate: false, **options) do |expanded|
Expand Down Expand Up @@ -93,9 +94,10 @@ def self.cli_commands
raise ArgumentError, "Compacting requires a context" unless options[:context]
out = options[:output] || $stdout
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
options[:base] ||= options[:base_uri]
if options[:format] == :jsonld
if files.empty?
# If files are empty, either use options[:execute]
# If files are empty, either use options[:evaluate] or STDIN
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
JSON::LD::API.compact(input, options[:context], **options) do |compacted|
Expand Down Expand Up @@ -126,7 +128,7 @@ def self.cli_commands
control: :url2,
use: :required,
on: ["--context CONTEXT"],
description: "Context to use when compacting.") {|arg| RDF::URI(arg)},
description: "Context to use when compacting.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
]
},
flatten: {
Expand All @@ -137,9 +139,10 @@ def self.cli_commands
lambda: ->(files, **options) do
out = options[:output] || $stdout
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
options[:base] ||= options[:base_uri]
if options[:format] == :jsonld
if files.empty?
# If files are empty, either use options[:execute]
# If files are empty, either use options[:evaluate] or STDIN
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
JSON::LD::API.flatten(input, options[:context], **options) do |flattened|
Expand Down Expand Up @@ -173,9 +176,10 @@ def self.cli_commands
raise ArgumentError, "Framing requires a frame" unless options[:frame]
out = options[:output] || $stdout
out.set_encoding(Encoding::UTF_8) if RUBY_PLATFORM == "java"
options[:base] ||= options[:base_uri]
if options[:format] == :jsonld
if files.empty?
# If files are empty, either use options[:execute]
# If files are empty, either use options[:evaluate] or STDIN
input = options[:evaluate] ? StringIO.new(options[:evaluate]) : STDIN
input.set_encoding(options.fetch(:encoding, Encoding::UTF_8))
JSON::LD::API.frame(input, options[:frame], **options) do |framed|
Expand Down Expand Up @@ -207,7 +211,7 @@ def self.cli_commands
control: :url2,
use: :required,
on: ["--frame FRAME"],
description: "Frame to use when serializing.") {|arg| RDF::URI(arg)}
description: "Frame to use when serializing.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))}
]
},
}
Expand Down
2 changes: 1 addition & 1 deletion lib/json/ld/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def self.options
control: :url2,
datatype: RDF::URI,
on: ["--expand-context CONTEXT"],
description: "Context to use when expanding.") {|arg| RDF::URI(arg)},
description: "Context to use when expanding.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
RDF::CLI::Option.new(
symbol: :extractAllScripts,
datatype: TrueClass,
Expand Down
13 changes: 10 additions & 3 deletions lib/json/ld/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def self.options
datatype: RDF::URI,
control: :url2,
on: ["--context CONTEXT"],
description: "Context to use when compacting.") {|arg| RDF::URI(arg)},
description: "Context to use when compacting.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
RDF::CLI::Option.new(
symbol: :embed,
datatype: %w(@always @once @never),
Expand All @@ -107,6 +107,13 @@ def self.options
control: :checkbox,
on: ["--[no-]explicit"],
description: "Only include explicitly declared properties in output (false)") {|arg| arg},
RDF::CLI::Option.new(
symbol: :frame,
datatype: RDF::URI,
control: :url2,
use: :required,
on: ["--frame FRAME"],
description: "Frame to use when serializing.") {|arg| RDF::URI(arg).absolute? ? RDF::URI(arg) : StringIO.new(File.read(arg))},
RDF::CLI::Option.new(
symbol: :lowercaseLanguage,
datatype: TrueClass,
Expand Down Expand Up @@ -137,7 +144,7 @@ def self.options
default: 'null',
control: :select,
on: ["--rdf-direction DIR", %w(i18n-datatype compound-literal)],
description: "How to serialize literal direction (i18n-datatype compound-literal)") {|arg| RDF::URI(arg)},
description: "How to serialize literal direction (i18n-datatype compound-literal)") {|arg| arg},
RDF::CLI::Option.new(
symbol: :requireAll,
datatype: TrueClass,
Expand Down Expand Up @@ -202,7 +209,7 @@ def default_context=(url); @default_context = url; end
end

##
# Initializes the RDF-LD writer instance.
# Initializes the JSON-LD writer instance.
#
# @param [IO, File] output
# the output stream
Expand Down

0 comments on commit a48b3e3

Please sign in to comment.