Skip to content

Instantly share code, notes, and snippets.

@ktym
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktym/00bf7b0e9280c4ec1962 to your computer and use it in GitHub Desktop.
Save ktym/00bf7b0e9280c4ec1962 to your computer and use it in GitHub Desktop.
RDF format converter
#!/usr/bin/env ruby
#
# % gem install linkeddata
# % lodconvert.rb -h
# % lodconvert.rb [-i input_format] [-o output_format] inputfile > outputfile
#
require 'rubygems'
require 'linkeddata'
require 'getoptlong'
def help
puts DATA.read
end
# Set default output format to turtle
opts = {
:output => :ttl
}
args = GetoptLong.new(
[ '--input', '-i', GetoptLong::REQUIRED_ARGUMENT ],
[ '--output', '-o', GetoptLong::REQUIRED_ARGUMENT ],
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
)
args.each_option do |name, value|
case name
when /--input/
opts[:input] = value.to_sym
when /--output/
opts[:output] = value.to_sym
when /--help/
help
exit 0
end
end
if opts[:input]
hash = { :format => opts[:input] }
else
hash = {}
end
RDF::Graph.load(ARGV.shift, hash) do |graph|
puts graph.dump(opts[:output], :standard_prefixes => true)
end
__END__
NAME
lodconvert.rb -- RDF format converter
SYNOPSIS
lodconvert.rb --help
lodconvert.rb [--input format] --output format input_file > output_file
DESCRIPTION
Accepted formats includes:
rdf, rdfxml RDF/XML
ttl, turtle Turtle
ntriples N-Triples
jsonld JSON-LD
trig TriG
trix TriX
nquads N-Quads
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment