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 +4 -4
- data/lib/dita-topic/cli.rb +111 -11
- data/lib/dita-topic/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 717dd58c267369873a2fe78ec88ee18e1c8ff50c308a292374cb63cca102b14d
|
|
4
|
+
data.tar.gz: 3d6721ed5f09341567e887b90090e98fa6598050902f6ea52b606b08a1563459
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4f33bcd097400cc12f089794f09b4cbef47b7177f50fe1d6e076415b90abfb70d6ba39c9b4c93fdea33ac722913497ed8dc4f3c79f20646adce45c6b7b4e0a6b
|
|
7
|
+
data.tar.gz: ab89f66595f1724d584cbac39c07940920254b0264b275d8d026745e0824d60565bdc69f2a34aed03a047ce5f5a72af968faba956d8bc3a32b4dcd939f8199cf
|
data/lib/dita-topic/cli.rb
CHANGED
|
@@ -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 = {
|
|
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
|
|
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('-
|
|
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 =
|
|
222
|
+
output = @opts[:output] ? @opts[:output] : $stdout
|
|
128
223
|
else
|
|
224
|
+
suffix = @opts[:map] ? '.ditamap' : '.dita'
|
|
129
225
|
input = File.read(file)
|
|
130
|
-
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
|
-
|
|
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
|
data/lib/dita-topic/version.rb
CHANGED