asciidoctor-dita-topic 1.4.3 → 1.4.5
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 +105 -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: 8f6ab85df9ce5321111233e1d6010ffbaff45ef483429b52f1c4f1e21b24a599
|
|
4
|
+
data.tar.gz: 3032b2f55d0e62d5259ab80172c0ae6e22c64d0dad22d6af3e93de9b16432429
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3a0b76fb1014db44d1b4480ba9b3769104651ef209e74b3557e8c4834719ae44982604d7423729c3a57fdd226e275fe07bca439239cf8eb574c6b7d0f7b8a0be
|
|
7
|
+
data.tar.gz: c0b2aeb4ffc8bd7c21ff1cb26ea9112eb48b651e7137c696a2a2dda4789077f9e98ce754d04c2ede6523a0380d89527f1846a27549951442252e0e38b5e23bd4
|
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,83 @@ 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 input, output
|
|
202
|
+
Asciidoctor.convert input, backend: 'dita-topic', standalone: @opts[:standalone], safe: :unsafe, attributes: @attr, to_file: output
|
|
203
|
+
end
|
|
204
|
+
|
|
116
205
|
def run
|
|
117
206
|
prepended = ''
|
|
118
207
|
|
|
@@ -124,15 +213,20 @@ module AsciidoctorDitaTopic
|
|
|
124
213
|
@args.each do |file|
|
|
125
214
|
if file == $stdin
|
|
126
215
|
input = $stdin.read
|
|
127
|
-
output =
|
|
216
|
+
output = @opts[:output] ? @opts[:output] : $stdout
|
|
128
217
|
else
|
|
218
|
+
suffix = @opts[:map] ? '.ditamap' : '.dita'
|
|
129
219
|
input = File.read(file)
|
|
130
|
-
output =
|
|
220
|
+
output = @opts[:output] ? @opts[:output] : Pathname.new(file).sub_ext(suffix).to_s
|
|
131
221
|
end
|
|
132
222
|
|
|
133
223
|
input.gsub!(Asciidoctor::IncludeDirectiveRx, '//\&') unless @opts[:includes]
|
|
134
224
|
|
|
135
|
-
|
|
225
|
+
if @opts[:map]
|
|
226
|
+
convert_map file, prepended + input, output
|
|
227
|
+
else
|
|
228
|
+
convert_topic prepended + input, output
|
|
229
|
+
end
|
|
136
230
|
end
|
|
137
231
|
end
|
|
138
232
|
end
|
data/lib/dita-topic/version.rb
CHANGED