Class: Kramdown::AsciiDoc::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/kramdown-asciidoc/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(args = ARGV) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/kramdown-asciidoc/cli.rb', line 109

def self.run args = ARGV
  code, options = new.parse (Array args)
  return code unless code == 0 && options
  pipe_in = (input_file = options.delete :input_file) == '-'
  pipe_out = (output_file = options.delete :output_file) == '-'
  if pipe_in
    options[:to] = pipe_out || !output_file ? $stdout : output_file
    ::Kramdoc.convert $stdin, options
  elsif output_file && !pipe_out && (::File.expand_path input_file) == (::File.expand_path output_file)
    $stderr.write %(kramdoc: input and output cannot be the same file: #{input_file}\n)
    return 1
  else
    options[:to] = pipe_out ? $stdout : output_file if output_file
    ::Kramdoc.convert_file input_file, options
  end
  0
rescue ::IOError
  $stderr.write %(kramdoc: #{$!.message}\n)
  1
end

Instance Method Details

#parse(args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/kramdown-asciidoc/cli.rb', line 10

def parse args
  options = {
    attributes: {},
  }

  opt_parser = ::OptionParser.new do |opts|
    opts.program_name = 'kramdoc'
    opts.banner = "    Usage: \#{opts.program_name} [OPTION]... FILE\n\n    Converts Markdown to AsciiDoc.\n\n    END\n\n    opts.on '-o FILE', '--output=FILE', 'Set the output filename or stream' do |file|\n      options[:output_file] = file\n    end\n\n    opts.on '--format=GFM|kramdown|markdown', %w(kramdown markdown GFM), 'Set the flavor of Markdown to parse (default: GFM)' do |format|\n      options[:input] = format\n    end\n\n    opts.on '-a KEY[=VALUE]', '--attribute=KEY[=VALUE]', 'Set an attribute in the AsciiDoc document header (accepts: key, key!, or key=value)' do |attr|\n      key, val = attr.split '=', 2\n      val ||= ''\n      options[:attributes][key] = val\n    end\n\n    opts.on '--diagram-languages=VALUES', 'Specify source languages to treat as diagrams (default: plantuml,mermaid)' do |names|\n      options[:diagram_languages] = names.split ','\n    end\n\n    opts.on '--wrap=preserve|none|ventilate', [:none, :preserve, :ventilate], 'Set how lines are wrapped in the AsciiDoc document (default: preserve)' do |wrap|\n      options[:wrap] = wrap\n    end\n\n    opts.on '--imagesdir=DIR', 'Set the imagesdir attribute in the AsciiDoc document header (also remove the value from the start of image paths)' do |dir|\n      options[:imagesdir] = dir\n    end\n\n    opts.on '--heading-offset=NUMBER', ::Integer, 'Shift the heading level by the specified number' do |offset|\n      options[:heading_offset] = offset\n    end\n\n    opts.on '--[no-]html-to-native', 'Set whether to passthrough HTML or convert it to AsciiDoc syntax where possible (default: true)' do |html_to_native|\n      options[:html_to_native] = html_to_native\n    end\n\n    opts.on '--auto-ids', 'Set whether to auto-generate IDs for all section titles' do |auto_ids|\n      options[:auto_ids] = auto_ids\n    end\n\n    opts.on '--auto-id-prefix=STRING', 'Set the prefix to use for auto-generated section title IDs' do |string|\n      options[:auto_id_prefix] = string\n    end\n\n    opts.on '--auto-id-separator=CHAR', 'Set the separator char to use for auto-generated section title IDs' do |char|\n      options[:auto_id_separator] = char\n    end\n\n    opts.on '--lazy-ids', 'Set whether to drop IDs that match value of auto-generated ID' do |lazy_ids|\n      options[:lazy_ids] = lazy_ids\n    end\n\n    opts.on '--[no-]auto-links', 'Set whether to automatically convert bare URLs into links (default: true)' do |auto_links|\n      options[:auto_links] = auto_links\n    end\n\n    opts.on '-h', '--help', 'Display this help text and exit' do\n      $stdout.write opts.help\n      return 0\n    end\n\n    opts.on '-v', '--version', %(Display version information and exit) do\n      $stdout.write %(\#{opts.program_name} \#{::Kramdown::AsciiDoc::VERSION}\\n)\n      return 0\n    end\n  end\n\n  args = opt_parser.parse args\n\n  if args.empty?\n    opt_parser.warn 'Please specify a Markdown file to convert.'\n    $stdout.write opt_parser.help\n    1\n  elsif args.size == 1\n    options[:input_file] = args[0]\n    [0, options]\n  else\n    opt_parser.warn %(extra arguments detected (unparsed arguments: \#{(args.drop 1).join ' '}))\n    $stdout.write opt_parser.help\n    [1, options]\n  end\nrescue ::OptionParser::InvalidOption\n  $stderr.write %(\#{opt_parser.program_name}: \#{$!.message}\\n)\n  $stdout.write opt_parser.help\n  1\nend\n"