asciidoctor-dita-topic 1.4.3 → 1.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 11fd5a1643d253674e558d4f693b877f21ac76fe0187778145610d1c7f2fbe08
4
- data.tar.gz: 647dd1a9354135c1da3585980cb5b3033518663ae14747eed89ab8e292ac2c3d
3
+ metadata.gz: 717dd58c267369873a2fe78ec88ee18e1c8ff50c308a292374cb63cca102b14d
4
+ data.tar.gz: 3d6721ed5f09341567e887b90090e98fa6598050902f6ea52b606b08a1563459
5
5
  SHA512:
6
- metadata.gz: aae2ef4be77c4cb0501f2c178813f1bf867168b49409a55f3671e5c87cf4d69f1d60ed3cccf4cd3efbf91b81dd26890cea83559083396c658be9745ed0b5a9a0
7
- data.tar.gz: d799dc9c5b8ba1a04e86cc98b8dd857a2a9e50e010b145f6fdf12f9c3ea14a7bc32293c3d435fafb4c9e36df2cc1491f485e80756dd1197b4a81b96027384677
6
+ metadata.gz: 4f33bcd097400cc12f089794f09b4cbef47b7177f50fe1d6e076415b90abfb70d6ba39c9b4c93fdea33ac722913497ed8dc4f3c79f20646adce45c6b7b4e0a6b
7
+ data.tar.gz: ab89f66595f1724d584cbac39c07940920254b0264b275d8d026745e0824d60565bdc69f2a34aed03a047ce5f5a72af968faba956d8bc3a32b4dcd939f8199cf
@@ -22,6 +22,7 @@
22
22
  # OTHER DEALINGS IN THE SOFTWARE.
23
23
 
24
24
  require 'optparse'
25
+ require 'pathname'
25
26
  require 'asciidoctor'
26
27
  require_relative 'version'
27
28
  require_relative '../dita-topic'
@@ -30,7 +31,12 @@ module AsciidoctorDitaTopic
30
31
  class Cli
31
32
  def initialize name, argv
32
33
  @attr = ['experimental']
33
- @opts = {:output => true, :includes => true, :standalone => true}
34
+ @opts = {
35
+ :output => false,
36
+ :includes => true,
37
+ :standalone => true,
38
+ :map => false
39
+ }
34
40
  @prep = []
35
41
  @name = name
36
42
  @args = self.parse_args argv
@@ -41,15 +47,25 @@ module AsciidoctorDitaTopic
41
47
  opt.banner = "Usage: #{@name} [OPTION...] [FILE...]\n"
42
48
  opt.banner += " #{@name} -h|-v\n\n"
43
49
 
44
- opt.on('-o', '--out-file FILE', 'output file; by default, the output file name is based on the input file') do |output|
50
+ opt.on('-o', '--out-file FILE', 'specify the output file; by default, the output file name is based on the input file') do |output|
45
51
  @opts[:output] = (output.strip == '-') ? $stdout : output
46
52
  end
47
53
 
48
- opt.on('-a', '--attribute ATTRIBUTE', 'document attribute to set in the form of name, name!, or name=value pair') do |value|
54
+ 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|
49
55
  @attr.append value
50
56
  end
51
57
 
52
- opt.on('-p', '--prepend-file FILE', 'file to prepend to all input files') do |file|
58
+ opt.on('-s', '--no-header-footer', 'disable enclosing the content in <topic> and generating <title>') do
59
+ @opts[:standalone] = false
60
+ end
61
+
62
+ opt.separator ''
63
+
64
+ opt.on('-m', '--dita-map', 'generate a DITA map instead of a topic') do
65
+ @opts[:map] = true
66
+ end
67
+
68
+ opt.on('-p', '--prepend-file FILE', 'prepend a file to all input files; can be supplied multiple times') do |file|
53
69
  raise OptionParser::InvalidArgument, "not a file: #{file}" unless File.exist? file and File.file? file
54
70
  raise OptionParser::InvalidArgument, "file not readable: #{file}" unless File.readable? file
55
71
 
@@ -60,10 +76,6 @@ module AsciidoctorDitaTopic
60
76
  @opts[:includes] = false
61
77
  end
62
78
 
63
- opt.on('-s', '--no-header-footer', 'disable enclosing the content in <topic> and generating <title>') do
64
- @opts[:standalone] = false
65
- end
66
-
67
79
  opt.separator ''
68
80
 
69
81
  opt.on('-l', '--author-line', 'enable processing of author lines as metadata') do
@@ -113,6 +125,89 @@ module AsciidoctorDitaTopic
113
125
  return args
114
126
  end
115
127
 
128
+ def convert_map file, input, output
129
+ if file == $stdin
130
+ base_dir = Pathname.new(Dir.pwd).expand_path
131
+ offset = 0
132
+ else
133
+ base_dir = Pathname.new(file).dirname.expand_path
134
+ file = Pathname.new(file).sub_ext('.dita').basename
135
+ offset = 1
136
+ end
137
+
138
+ doc = Asciidoctor.load input, backend: 'dita-topic', safe: :unsafe, attributes: @attr, base_dir: base_dir, sourcemap: true
139
+ sections = doc.find_by context: :section
140
+
141
+ return unless sections
142
+
143
+ title = (sections.first.level == 0 and sections.first.title) ? sections.first.title : false
144
+
145
+ if @opts[:standalone]
146
+ result = ["<?xml version='1.0' encoding='utf-8' ?>"]
147
+ result << %(<!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd">)
148
+ result << %(<map>)
149
+ result << %( <title>#{title}</title>) if title
150
+ else
151
+ result = []
152
+ end
153
+
154
+ last_level = 0
155
+ last_file = ''
156
+
157
+ sections.each_index do |i|
158
+ section = sections[i]
159
+ level = section.level
160
+ title = section.title.gsub(/"|<[^>]*>|[<>]/, '')
161
+ filename = section.file ? Pathname.new(section.file).sub_ext('.dita').relative_path_from(base_dir) : Pathname.new(file)
162
+ current = last_level
163
+
164
+ next if filename == last_file
165
+
166
+ while current > level
167
+ current -= 1
168
+ result << ' ' * (current + offset) + %(</topicref>)
169
+ end
170
+
171
+ if level - last_level > 1
172
+ warn "WARNING: #{filename.basename}: line #{section.lineno}: section title out of sequence: expected level #{last_level + 1}, got level #{level}"
173
+ level = last_level + 1
174
+ end
175
+
176
+ indent = ' ' * (level + offset)
177
+ parent = (sections[i + 1] and sections[i + 1].level > level) ? true : false
178
+ result << indent + %(<topicref href="#{filename}" navtitle="#{title}"#{parent ? '>' : ' />'}) unless filename == $stdin
179
+
180
+ last_level = level
181
+ last_file = filename
182
+ end
183
+
184
+ while last_level > 0
185
+ last_level -= 1
186
+ break if last_level == 0 and file == $stdin
187
+ result << ' ' * (last_level + offset) + %(</topicref>)
188
+ end
189
+
190
+ if @opts[:standalone]
191
+ result << %(</map>)
192
+ end
193
+
194
+ if output == $stdout
195
+ $stdout.write result.join("\n")
196
+ else
197
+ File.write output, result.join("\n")
198
+ end
199
+ end
200
+
201
+ def convert_topic file, input, output
202
+ if file == $stdin
203
+ base_dir = Pathname.new(Dir.pwd).expand_path
204
+ else
205
+ base_dir = Pathname.new(file).dirname.expand_path
206
+ end
207
+
208
+ Asciidoctor.convert input, backend: 'dita-topic', standalone: @opts[:standalone], safe: :unsafe, attributes: @attr, to_file: output, base_dir: base_dir
209
+ end
210
+
116
211
  def run
117
212
  prepended = ''
118
213
 
@@ -124,15 +219,20 @@ module AsciidoctorDitaTopic
124
219
  @args.each do |file|
125
220
  if file == $stdin
126
221
  input = $stdin.read
127
- output = (@opts[:output] == true) ? $stdout : @opts[:output]
222
+ output = @opts[:output] ? @opts[:output] : $stdout
128
223
  else
224
+ suffix = @opts[:map] ? '.ditamap' : '.dita'
129
225
  input = File.read(file)
130
- output = (@opts[:output] == true) ? Pathname.new(file).sub_ext('.dita').to_s : @opts[:output]
226
+ output = @opts[:output] ? @opts[:output] : Pathname.new(file).sub_ext(suffix)
131
227
  end
132
228
 
133
229
  input.gsub!(Asciidoctor::IncludeDirectiveRx, '//\&') unless @opts[:includes]
134
230
 
135
- Asciidoctor.convert prepended + input, backend: 'dita-topic', standalone: @opts[:standalone], safe: :unsafe, attributes: @attr, to_file: output
231
+ if @opts[:map]
232
+ convert_map file, prepended + input, output
233
+ else
234
+ convert_topic file, prepended + input, output
235
+ end
136
236
  end
137
237
  end
138
238
  end
@@ -24,5 +24,5 @@
24
24
  # frozen_string_literal: true
25
25
 
26
26
  module AsciidoctorDitaTopic
27
- VERSION = '1.4.3'
27
+ VERSION = '1.4.4'
28
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciidoctor-dita-topic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.3
4
+ version: 1.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaromir Hradilek