Class: AsciidoctorDitaMap::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/dita-map/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



31
32
33
34
35
# File 'lib/dita-map/cli.rb', line 31

def initialize argv
  @output    = nil
  @converter = Convert.new
  @args      = self.parse_args argv
end

Instance Method Details

#parse_args(argv) ⇒ Object



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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dita-map/cli.rb', line 37

def parse_args argv
  parser = OptionParser.new do |opt|
    opt.banner  = "Usage: #{NAME} [OPTION...] [FILE...]\n"
    opt.banner += "       #{NAME} -h|-v\n\n"

    opt.on('-o', '--out-file FILE', 'specify the output file; by default, the output file name is based on the input file') do |output|
      @output = (output.strip == '-') ? $stdout : output
    end

    opt.on('-a', '--attribute ATTRIBUTE', 'set a document attribute in the form of name, name!, or name=value pair; can be supplied multiple times') do |value|
      @converter.attr.append value
    end

    opt.separator ''

    opt.on('-p', '--prepend-file FILE', 'prepend a file to all input files; can be supplied multiple times') do |file|
      raise OptionParser::InvalidArgument, "not a file: #{file}" unless File.exist? file and File.file? file
      raise OptionParser::InvalidArgument, "file not readable: #{file}" unless File.readable? file

      @converter.prep << File.read(file)
      @converter.prep << "\n"
    end

    opt.on('-i', '--include-self', 'make the supplied file the toplevel topicref') do
      @converter.opts[:self] = true
    end

    opt.separator ''

    opt.on('-I', '--no-id', 'do not generate the map id attribute') do
      @converter.opts[:id] = false
    end

    opt.on('-M', '--no-maptitle', 'do not generate the map title') do
      @converter.opts[:title] = false
    end

    opt.on('-A', '--no-assembly', 'do treat assemblies as maps') do
      @converter.opts[:assembly] = false
    end

    opt.on('-C', '--no-chunk', 'do not generate the chunk attribute') do
      @converter.opts[:chunk] = false
    end

    opt.on('-L', '--no-locktitle', 'do not generate the locktitle attribute') do
      @converter.opts[:locktitle] = false
    end

    opt.on('-N', '--no-navtitle', 'do not generate the navtitle attribute') do
      @converter.opts[:navtitle] = false
    end

    opt.on('-O', '--no-toc', 'do not generate the toc attribute') do
      @converter.opts[:toc] = false
    end

    opt.on('-T', '--no-type', 'do not generate the type attribute') do
      @converter.opts[:type] = false
    end

    opt.separator ''

    opt.on('-v', '--verbose', 'report additional problems in the supplied files') do
      @converter.opts[:verbose] = true
    end

    opt.on('-z', '--zero-offset', 'allow include directives with zero leveloffset') do
      @converter.opts[:zero_offset] = true
    end

    opt.separator ''

    opt.on('-h', '--help', 'display this help and exit') do
      puts opt
      exit
    end

    opt.on('-V', '--version', 'display version information and exit') do
      puts "#{NAME} #{VERSION}"
      exit
    end
  end

  args = parser.parse argv

  if args.length == 0 or args[0].strip == '-'
    return [$stdin]
  end

  args.each do |file|
    raise OptionParser::InvalidArgument, "not a file: #{file}" unless File.exist? file and File.file? file
    raise OptionParser::InvalidArgument, "file not readable: #{file}" unless File.readable? file
  end

  return args
end

#runObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dita-map/cli.rb', line 135

def run
  @args.each do |file|
    if file == $stdin
      base_dir = Pathname.new(Dir.pwd).expand_path
      input    = $stdin.read
      output   = @output ? @output : $stdout
    else
      base_dir = Pathname.new(file).dirname.expand_path
      input    = File.read(file)
      output   = @output ? @output : Pathname.new(file).sub_ext('.ditamap').to_s
    end

    if @converter.opts[:self] and file != $stdin
      result = @converter.run input, base_dir, file
    else
      result = @converter.run input, base_dir
    end

    if output == $stdout
      $stdout.write result
    else
      File.write output, result
    end
  end
end