asciidoctor-dita-topic 1.4.2 → 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 +112 -12
- data/lib/dita-topic/version.rb +1 -1
- data/lib/dita-topic.rb +12 -12
- 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
|
|
@@ -38,18 +44,28 @@ module AsciidoctorDitaTopic
|
|
|
38
44
|
|
|
39
45
|
def parse_args argv
|
|
40
46
|
parser = OptionParser.new do |opt|
|
|
41
|
-
opt.banner = "Usage: #{@name} [OPTION...] FILE
|
|
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
data/lib/dita-topic.rb
CHANGED
|
@@ -203,7 +203,7 @@ class DitaTopic < Asciidoctor::Converter::Base
|
|
|
203
203
|
# Process individual list items:
|
|
204
204
|
node.items.each do |terms, description|
|
|
205
205
|
# Compose the metadata attributes:
|
|
206
|
-
metadata
|
|
206
|
+
metadata = extract_attributes terms[0].text
|
|
207
207
|
|
|
208
208
|
# Open the definition entry:
|
|
209
209
|
result << %(<dlentry#{metadata}>)
|
|
@@ -534,16 +534,16 @@ class DitaTopic < Asciidoctor::Converter::Base
|
|
|
534
534
|
# Process individual list items:
|
|
535
535
|
node.items.each do |item|
|
|
536
536
|
# Compose the metadata attributes:
|
|
537
|
-
metadata
|
|
537
|
+
metadata = extract_attributes item.text
|
|
538
538
|
metadata = compose_metadata item.role if item.role
|
|
539
539
|
|
|
540
540
|
# Check if the list item contains multiple block elements:
|
|
541
541
|
if item.blocks?
|
|
542
|
-
result << %(<li#{compose_id item.id}#{metadata}>#{text})
|
|
542
|
+
result << %(<li#{compose_id item.id}#{metadata}>#{item.text})
|
|
543
543
|
result << item.content
|
|
544
544
|
result << %(</li>)
|
|
545
545
|
else
|
|
546
|
-
result << %(<li#{compose_id item.id}#{metadata}>#{text}</li>)
|
|
546
|
+
result << %(<li#{compose_id item.id}#{metadata}>#{item.text}</li>)
|
|
547
547
|
end
|
|
548
548
|
end
|
|
549
549
|
|
|
@@ -709,7 +709,7 @@ class DitaTopic < Asciidoctor::Converter::Base
|
|
|
709
709
|
# Process each row:
|
|
710
710
|
rows.each do |row|
|
|
711
711
|
# Compose the metadata attributes:
|
|
712
|
-
metadata
|
|
712
|
+
metadata = extract_attributes row[0].text
|
|
713
713
|
|
|
714
714
|
# Open the row:
|
|
715
715
|
result << %(<row#{metadata}>)
|
|
@@ -777,7 +777,7 @@ class DitaTopic < Asciidoctor::Converter::Base
|
|
|
777
777
|
# Process individual list items:
|
|
778
778
|
node.items.each do |item|
|
|
779
779
|
# Compose the metadata attributes:
|
|
780
|
-
metadata
|
|
780
|
+
metadata = extract_attributes item.text
|
|
781
781
|
metadata = compose_metadata item.role if item.role
|
|
782
782
|
|
|
783
783
|
# Check if the list item is part of a checklist:
|
|
@@ -789,11 +789,11 @@ class DitaTopic < Asciidoctor::Converter::Base
|
|
|
789
789
|
|
|
790
790
|
# Check if the list item contains multiple block elements:
|
|
791
791
|
if item.blocks?
|
|
792
|
-
result << %(<li#{compose_id item.id}#{metadata}>#{check_box}#{text})
|
|
792
|
+
result << %(<li#{compose_id item.id}#{metadata}>#{check_box}#{item.text})
|
|
793
793
|
result << item.content
|
|
794
794
|
result << %(</li>)
|
|
795
795
|
else
|
|
796
|
-
result << %(<li#{compose_id item.id}#{metadata}>#{check_box}#{text}</li>)
|
|
796
|
+
result << %(<li#{compose_id item.id}#{metadata}>#{check_box}#{item.text}</li>)
|
|
797
797
|
end
|
|
798
798
|
end
|
|
799
799
|
|
|
@@ -864,7 +864,7 @@ class DitaTopic < Asciidoctor::Converter::Base
|
|
|
864
864
|
# Process individual list items:
|
|
865
865
|
node.items.each do |terms, description|
|
|
866
866
|
# Compose the metadata attributes:
|
|
867
|
-
metadata
|
|
867
|
+
metadata = extract_attributes terms[0].text
|
|
868
868
|
|
|
869
869
|
# Open the list item:
|
|
870
870
|
result << %(<li#{metadata}>)
|
|
@@ -907,7 +907,7 @@ class DitaTopic < Asciidoctor::Converter::Base
|
|
|
907
907
|
# Process individual list items:
|
|
908
908
|
node.items.each do |terms, description|
|
|
909
909
|
# Compose the metadata attributes:
|
|
910
|
-
metadata
|
|
910
|
+
metadata = extract_attributes terms[0].text
|
|
911
911
|
|
|
912
912
|
# Open the table row:
|
|
913
913
|
result << %(<row#{metadata}>)
|
|
@@ -1076,9 +1076,9 @@ class DitaTopic < Asciidoctor::Converter::Base
|
|
|
1076
1076
|
def extract_attributes text
|
|
1077
1077
|
# Extract metadata attributes from an empty ph element:
|
|
1078
1078
|
if /^\s*<ph(?<attributes> [^>]+)><\/ph>\s*/ =~ text
|
|
1079
|
-
return attributes
|
|
1079
|
+
return attributes
|
|
1080
1080
|
else
|
|
1081
|
-
return ''
|
|
1081
|
+
return ''
|
|
1082
1082
|
end
|
|
1083
1083
|
end
|
|
1084
1084
|
|