@@ -41,7 +41,132 @@ def self.detect(sample)
4141 # Exclude CSVW metadata
4242 !sample . include? ( "http://www.w3.org/ns/csvw" )
4343 end
44-
44+
45+ ##
46+ # Hash of CLI commands appropriate for this format
47+ # @return [Hash{Symbol => Lambda(Array, Hash)}]
48+ def self . cli_commands
49+ {
50+ expand : -> ( files , options ) do
51+ out = options [ :output ] || $stdout
52+ out . set_encoding ( Encoding ::UTF_8 ) if RUBY_PLATFORM == "java"
53+ options = options . merge ( expandContext : options . delete ( :context ) ) if options . has_key? ( :context )
54+ if options [ :format ] == :jsonld
55+ if files . empty?
56+ # If files are empty, either use options[:execute]
57+ input = options [ :evaluate ] ? StringIO . new ( options [ :evaluate ] ) : STDIN
58+ input . set_encoding ( options . fetch ( :encoding , Encoding ::UTF_8 ) )
59+ JSON ::LD ::API . expand ( input , options ) do |expanded |
60+ out . puts expanded . to_json ( JSON ::LD ::JSON_STATE )
61+ end
62+ else
63+ files . each do |file |
64+ JSON ::LD ::API . expand ( file , options ) do |expanded |
65+ out . puts expanded . to_json ( JSON ::LD ::JSON_STATE )
66+ end
67+ end
68+ end
69+ else
70+ # Turn RDF into JSON-LD first
71+ RDF ::CLI . parse ( files , options ) do |reader |
72+ JSON ::LD ::API . fromRdf ( reader ) do |expanded |
73+ out . puts expanded . to_json ( JSON ::LD ::JSON_STATE )
74+ end
75+ end
76+ end
77+ end ,
78+ compact : -> ( files , options ) do
79+ raise ArgumentError , "Compacting requires a context" unless options [ :context ]
80+ out = options [ :output ] || $stdout
81+ out . set_encoding ( Encoding ::UTF_8 ) if RUBY_PLATFORM == "java"
82+ if options [ :format ] == :jsonld
83+ if files . empty?
84+ # If files are empty, either use options[:execute]
85+ input = options [ :evaluate ] ? StringIO . new ( options [ :evaluate ] ) : STDIN
86+ input . set_encoding ( options . fetch ( :encoding , Encoding ::UTF_8 ) )
87+ JSON ::LD ::API . compact ( input , options [ :context ] , options ) do |compacted |
88+ out . puts compacted . to_json ( JSON ::LD ::JSON_STATE )
89+ end
90+ else
91+ files . each do |file |
92+ JSON ::LD ::API . compact ( file , options [ :context ] , options ) do |compacted |
93+ out . puts compacted . to_json ( JSON ::LD ::JSON_STATE )
94+ end
95+ end
96+ end
97+ else
98+ # Turn RDF into JSON-LD first
99+ RDF ::CLI . parse ( files , options ) do |reader |
100+ JSON ::LD ::API . fromRdf ( reader ) do |expanded |
101+ JSON ::LD ::API . compact ( expanded , options [ :context ] , options ) do |compacted |
102+ out . puts compacted . to_json ( JSON ::LD ::JSON_STATE )
103+ end
104+ end
105+ end
106+ end
107+ end ,
108+ flatten : -> ( files , options ) do
109+ out = options [ :output ] || $stdout
110+ out . set_encoding ( Encoding ::UTF_8 ) if RUBY_PLATFORM == "java"
111+ if options [ :format ] == :jsonld
112+ if files . empty?
113+ # If files are empty, either use options[:execute]
114+ input = options [ :evaluate ] ? StringIO . new ( options [ :evaluate ] ) : STDIN
115+ input . set_encoding ( options . fetch ( :encoding , Encoding ::UTF_8 ) )
116+ JSON ::LD ::API . flatten ( input , options [ :context ] , options ) do |flattened |
117+ out . puts flattened . to_json ( JSON ::LD ::JSON_STATE )
118+ end
119+ else
120+ files . each do |file |
121+ JSON ::LD ::API . flatten ( file , options [ :context ] , options ) do |flattened |
122+ out . puts flattened . to_json ( JSON ::LD ::JSON_STATE )
123+ end
124+ end
125+ end
126+ else
127+ # Turn RDF into JSON-LD first
128+ RDF ::CLI . parse ( files , options ) do |reader |
129+ JSON ::LD ::API . fromRdf ( reader ) do |expanded |
130+ JSON ::LD ::API . flatten ( expanded , options [ :context ] , options ) do |flattened |
131+ out . puts flattened . to_json ( JSON ::LD ::JSON_STATE )
132+ end
133+ end
134+ end
135+ end
136+ end ,
137+ frame : -> ( files , options ) do
138+ raise ArgumentError , "Framing requires a frame" unless options [ :frame ]
139+ out = options [ :output ] || $stdout
140+ out . set_encoding ( Encoding ::UTF_8 ) if RUBY_PLATFORM == "java"
141+ if options [ :format ] == :jsonld
142+ if files . empty?
143+ # If files are empty, either use options[:execute]
144+ input = options [ :evaluate ] ? StringIO . new ( options [ :evaluate ] ) : STDIN
145+ input . set_encoding ( options . fetch ( :encoding , Encoding ::UTF_8 ) )
146+ JSON ::LD ::API . frame ( input , options [ :frame ] , options ) do |framed |
147+ out . puts framed . to_json ( JSON ::LD ::JSON_STATE )
148+ end
149+ else
150+ files . each do |file |
151+ JSON ::LD ::API . frame ( file , options [ :frame ] , options ) do |framed |
152+ out . puts framed . to_json ( JSON ::LD ::JSON_STATE )
153+ end
154+ end
155+ end
156+ else
157+ # Turn RDF into JSON-LD first
158+ RDF ::CLI . parse ( files , options ) do |reader |
159+ JSON ::LD ::API . fromRdf ( reader ) do |expanded |
160+ JSON ::LD ::API . frame ( expanded , options [ :frame ] , options ) do |framed |
161+ out . puts framed . to_json ( JSON ::LD ::JSON_STATE )
162+ end
163+ end
164+ end
165+ end
166+ end ,
167+ }
168+ end
169+
45170 ##
46171 # Override normal symbol generation
47172 def self . to_sym
0 commit comments