Class: AsciidoctorDitaMap::Cli

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

Instance Method Summary collapse

Constructor Details

#initialize(name, argv) ⇒ Cli

Returns a new instance of Cli.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dita-map/cli.rb', line 33

def initialize name, argv
  @attr = []
  @opts = {
    :id => true,
    :navtitle => true,
    :output => false,
    :title => true,
    :type => true,
    :self => false,
    :verbose => false
  }
  @prep = []
  @name = name
  @args = self.parse_args argv
end

Instance Method Details

#compose_mapref_attributes(file_name, type) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/dita-map/cli.rb', line 126

def compose_mapref_attributes file_name, type
  target_file        = file_name.sub(/\.adoc$/, '.ditamap')
  attributes         = { 'href' => target_file, 'format' => 'ditamap' }
  attributes['type'] = type if @opts[:type]

  return attributes
end

#compose_topicref_attributes(file_name, title, type) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/dita-map/cli.rb', line 134

def compose_topicref_attributes file_name, title, type
  target_file            = file_name.sub(/\.adoc$/, '.dita')

  attributes             = { 'href' => target_file }
  attributes['navtitle'] = title if @opts[:navtitle] and title
  attributes['type']     = type if @opts[:type] and type and ['concept', 'reference', 'task'].include? type

  return attributes
end

#convert_map(input, base_dir, prepended = '', file = nil) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/dita-map/cli.rb', line 191

def convert_map input, base_dir, prepended = '', file = nil
  result = ''

  include_files, map = parse_map prepended + input, base_dir

  xml = REXML::Document.new
  xml.context[:attribute_quote] = :quote
  xml << REXML::XMLDecl.new('1.0', 'utf-8')
  xml << REXML::DocType.new('map', 'PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd"')

  if map[:id] and @opts[:id]
    xml_root  = xml.add_element('map', { 'id' => map[:id] })
  else
    xml_root  = xml.add_element('map')
  end

  if map[:title] and @opts[:title]
    xml_title      = xml_root.add_element('title')
    xml_title.text = map[:title]
  end

  if @opts[:self] and file
    attributes = compose_topicref_attributes file, map[:title], map[:type]
    xml_self   = xml_root.add_element('topicref', attributes)
    stack      = [{ :offset => 0, :element => xml_self }]
  else
    stack      = [{ :offset => 0, :element => xml_root }]
  end

  include_files.each do |file|
    target      = file[:target]
    offset      = file[:offset]
    last_offset = stack.last[:offset]
    full_path   = base_dir + target

    if not File.exist? full_path and @opts[:verbose]
      warn "#{@name}: warning: file not found: #{target}"
    end

    begin
      include_title, include_type = parse_topic prepended + File.read(full_path)
      next if ['attributes', 'snippet'].include? include_type
    rescue
      warn "#{@name}: warning: unable to read included file: #{target}"
      include_title, include_type = nil, nil
    end

    if offset == 0
      warn "#{@name}: warning: invalid leveloffset - expected 1, got 0: #{target}"
      offset = 1
    elsif offset > last_offset and offset - last_offset > 1
      expected_offset = last_offset + 1
      warn "#{@name}: warning: invalid leveloffset - expected #{expected_offset}, got #{offset}: #{target}"
    end

    while stack.last[:offset] >= offset
      stack.pop
    end

    xml_parent = stack.last[:element]

    if include_type == 'map'
      attributes  = compose_mapref_attributes target, include_type
      xml_element = xml_parent.add_element('mapref', attributes)
    else
      attributes  = compose_topicref_attributes target, include_title, include_type
      xml_element = xml_parent.add_element('topicref', attributes)
    end

    stack.push ({ :offset => offset, :element => xml_element })
  end

  formatter = REXML::Formatters::Pretty.new(2, true)
  formatter.compact = true
  formatter.write(xml, result)

  result << "\n"

  return result
end

#get_content_type(attributes) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/dita-map/cli.rb', line 144

def get_content_type attributes
  document_type  = attributes['_mod-docs-content-type'] ? attributes['_mod-docs-content-type'].downcase : nil
  document_type  = attributes['_content-type'] ? attributes['_content-type'].downcase : nil unless document_type
  document_type  = attributes['_module-type'] ? attributes['_module-type'].downcase : nil unless document_type

  if document_type
    document_type.sub!(/^assembly$/, 'concept')
    document_type.sub!(/^procedure$/, 'task')
  end

  unless ['concept', 'reference', 'task', 'map', 'attributes', 'snippet'].include? document_type
    return nil
  end

  return document_type
end

#parse_args(argv) ⇒ Object



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
# File 'lib/dita-map/cli.rb', line 49

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|
      @opts[: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|
      @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

      @prep.append file
    end

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

    opt.separator ''

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

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

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

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

    opt.separator ''

    opt.on('-v', '--verbose', 'report additional problems in the supplied files') do
      @opts[:verbose] = 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

#parse_map(input, base_dir) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/dita-map/cli.rb', line 170

def parse_map input, base_dir
  Asciidoctor::Extensions.register do
    include_processor CatalogIncludeDirectives
  end

  doc = Asciidoctor.load input, safe: :safe, catalog_assets: true, attributes: @attr, base_dir: base_dir

  include_files  = doc.catalog[:include_files] ? doc.catalog[:include_files] : []
  map_id    = doc.id ? doc.id.gsub(/["']/, '') : nil
  map_title = doc.title ? doc.title.gsub(/<[^>]*>/, '') : nil
  map_type  = get_content_type doc.attributes

  info = {
    :id    => map_id,
    :title => map_title,
    :type  => map_type
  }

  return include_files, info
end

#parse_topic(input) ⇒ Object



161
162
163
164
165
166
167
168
# File 'lib/dita-map/cli.rb', line 161

def parse_topic input
  doc = Asciidoctor.load input, safe: :secure, attributes: @attr

  document_title = doc.title ? doc.title.gsub(/<[^>]*>/, '') : nil
  document_type  = get_content_type doc.attributes

  return document_title, document_type
end

#runObject



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/dita-map/cli.rb', line 272

def run
  prepended = ''

  @prep.each do |file|
    prepended << File.read(file)
    prepended << "\n"
  end

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

    if @opts[:self] and file != $stdin
      result = convert_map input, base_dir, prepended, file
    else
      result = convert_map input, base_dir, prepended
    end

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