Class: AsciidoctorDitaTopic::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/dita-topic/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
# File 'lib/dita-topic/cli.rb', line 33

def initialize name, argv
  @attr = ['experimental']
  @opts = {
    :output => false,
    :standalone => true,
    :modules => true,
    :no_includes => 0
  }
  @prep = []
  @name = name
  @args = self.parse_args argv
end

Instance Method Details

#convert_topic(input, base_dir) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/dita-topic/cli.rb', line 133

def convert_topic input, base_dir
  if not @opts[:modules]
    Asciidoctor::Extensions.register do
      include_processor FilterIncludeDirectives
    end
  end
  return Asciidoctor.convert input, backend: 'dita-topic', standalone: @opts[:standalone], safe: :unsafe, attributes: @attr, base_dir: base_dir
end

#parse_args(argv) ⇒ Object



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

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.on('-s', '--no-header-footer', 'disable enclosing the content in <topic> and generating <title>') do
      @opts[:standalone] = false
    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', '--no-includes', 'disable processing of include directives; specify this option twice to also apply on prepended files') do
      @opts[:no_includes] += 1
    end

    opt.on('-A', '--no-modules', 'disable processing of include directives with assemblies and modules') do
      @opts[:modules] = false
    end

    opt.separator ''

    opt.on('-l', '--author-line', 'enable processing of author lines as metadata') do
      @attr.append 'dita-topic-authors=on'
    end

    opt.on('-t', '--section-type', 'add content type as outputclass to sections') do
      @attr.append 'dita-topic-type=on'
    end

    opt.on('-C', '--no-callouts', 'disable processing of callouts') do
      @attr.append 'dita-topic-callouts=off'
    end

    opt.on('-M', '--no-semantic-markup', 'disable processing of semantic markup') do
      @attr.append 'dita-topic-semantic=off'
    end

    opt.on('-S', '--no-sidebars', 'disable processing of sidebars') do
      @attr.append 'dita-topic-sidebars=off'
    end

    opt.on('-T', '--no-titles', 'disable processing of floating titles') do
      @attr.append 'dita-topic-titles=off'
    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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/dita-topic/cli.rb', line 142

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('.dita').to_s
    end

    prepended.gsub!(/^:_(?:mod-docs-content|content|module)-type:[ \t]+\S/, '//\&')
    prepended.gsub!(Asciidoctor::IncludeDirectiveRx, '//\&') if @opts[:no_includes] > 1
    input.gsub!(Asciidoctor::IncludeDirectiveRx, '//\&') if @opts[:no_includes] > 0

    result = convert_topic prepended + input, base_dir

    result.gsub!(/<ph (?:(?:platform|product|audience|otherprops)="[^"]+" ?)+><\/ph> */, '')

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