Class: Metanorma::Document::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/document/cli.rb

Defined Under Namespace

Classes: Error, ToMirrorOptions

Class Method Summary collapse

Class Method Details

.execute_to_mirror(options) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/metanorma/document/cli.rb', line 82

def self.execute_to_mirror(options)
  pipeline = Mirror::Output::Pipeline.new(
    xml_path: options.xml_path,
    flavor: options.flavor,
    title: options.title,
    id_strategy: options.id_strategy,
  )
  guide = pipeline.process

  json = Mirror::Serialization::JsonSerializer.serialize_pretty(guide.content)
  write_output(json, options.output)
end

.parse_to_mirror_options(argv) ⇒ Object

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/metanorma/document/cli.rb', line 37

def self.parse_to_mirror_options(argv)
  options = ToMirrorOptions.new(output: nil, flavor: nil,
                                id_strategy: nil, title: nil)

  parser = OptionParser.new do |opts|
    opts.banner = "Usage: metanorma-document to-mirror <xml_path> [options]"

    opts.on("-o", "--output PATH",
            "Output JSON path (default: stdout)") do |path|
      options.output = path
    end

    opts.on("-f", "--flavor FLAVOR",
            "Document flavor (default: auto-detect)") do |flavor|
      options.flavor = flavor
    end

    opts.on("--id-strategy STRATEGY",
            "ID strategy: preserve (default), positional") do |strategy|
      case strategy
      when "positional"
        options.id_strategy = Mirror::IdStrategy::Positional.new
      when "preserve"
        options.id_strategy = Mirror::IdStrategy::Preserve.new
      else
        raise Error,
              "Unknown ID strategy: #{strategy}. Use 'preserve' or 'positional'."
      end
    end

    opts.on("--title TITLE", "Document title override") do |title|
      options.title = title
    end
  end

  parser.parse!(argv)

  xml_path = argv.shift
  raise Error, "XML path required" unless xml_path
  raise Error, "File not found: #{xml_path}" unless File.exist?(xml_path)

  options.xml_path = xml_path
  options
end

.run(argv) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/metanorma/document/cli.rb', line 20

def self.run(argv)
  command = argv.shift
  case command
  when "to-mirror"
    to_mirror(argv)
  when nil, "-h", "--help"
    puts usage
  else
    raise Error, "Unknown command: #{command}"
  end
end

.to_mirror(argv) ⇒ Object



32
33
34
35
# File 'lib/metanorma/document/cli.rb', line 32

def self.to_mirror(argv)
  options = parse_to_mirror_options(argv)
  execute_to_mirror(options)
end

.usageObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/metanorma/document/cli.rb', line 104

def self.usage
  <<~USAGE
    Usage: metanorma-document <command> [options]

    Commands:
      to-mirror    Convert presentation XML to mirror JSON

    Run `metanorma-document <command> --help` for command-specific options.
  USAGE
end

.write_output(json, output_path) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/metanorma/document/cli.rb', line 95

def self.write_output(json, output_path)
  if output_path
    FileUtils.mkdir_p(File.dirname(output_path))
    File.write(output_path, json)
  else
    $stdout.puts(json)
  end
end