Module: RDF::Vocabulary::VocabFormatExtensions

Defined in:
lib/rdf/vocab/extensions.rb

Instance Method Summary collapse

Instance Method Details

#cli_commandsHash{Symbol => Lambda(Array, Hash)}

Hash of CLI commands appropriate for this format

Returns:

  • (Hash{Symbol => Lambda(Array, Hash)})


502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
# File 'lib/rdf/vocab/extensions.rb', line 502

def cli_commands
  super.merge({
    "gen-vocab": {
      description: "Generate a vocabulary using a special serialization.",
      parse: false,  # Only parse if there are input files, otherwise, uses vocabulary
      help: "gen-vocab --uri <vocabulary-URI> [--output-format ttl|jsonld|html] [options] [files]\nGenerate a vocabulary from repository using a special serialization.",
      lambda: ->(files, **options) do
        $stdout.puts "Generate Vocabulary"
        raise ArgumentError, "Must specify vocabulary URI" unless options[:base_uri]

        # Parse input graphs, if repository is not already created
        if RDF::CLI.repository.empty? && !files.empty?
          RDF::CLI.parse(files, **options) do |reader|
            RDF::CLI.repository << reader
          end
        end

        # Lookup vocabulary, or generate a new vocabulary from this URI
        vocab = RDF::Vocabulary.find(options[:base_uri]) || begin
          raise ArgumentError, "Must specify vocabulary prefix if vocabulary not built-in" unless options[:prefix]
          RDF::Vocabulary.from_graph(RDF::CLI.repository, url: options[:base_uri], class_name: options[:prefix].to_s.upcase)
        end

        prefixes = {}
        prefixes[options[:prefix]] = options[:base_uri] if options[:prefix]
        out = options[:output] || $stdout
        case options[:output_format]
        when :ttl, nil then out.write vocab.to_ttl(graph: RDF::CLI.repository, prefixes: prefixes)
        when :jsonld then out.write vocab.to_jsonld(graph: RDF::CLI.repository, prefixes: prefixes)
        when :html then out.write vocab.to_html(graph: RDF::CLI.repository, prefixes: prefixes, template: options[:template])
        else
          # Use whatever writer we find
          writer = RDF::Writer.for(options[:output_format]) || RDF::NTriples::Writer
          writer.new(out, **options) do |w|
            if RDF::CLI.repository.empty?
              vocab.each_statement {|s| w << s}
            else
              w << RDF::CLI.repository
            end
          end
        end
      end,
      options: [
        RDF::CLI::Option.new(
          symbol: :prefix,
          datatype: String,
          on: ["--prefix PREFIX"],
          description: "Prefix associated with vocabulary, if not built-in."),
        RDF::CLI::Option.new(
          symbol: :template,
          datatype: String,
          on: ["--template TEMPLATE"],
          description: "Path to local template for generating HTML, either Haml or ERB, depending on file extension.\n" +
                      "See https://github.com/ruby-rdf/rdf-vocab/tree/develop/etc for built-in templates."),
      ]
    }
  })
end