kramdown-asciidoc 1.0.0.alpha.12 → 1.0.0.alpha.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.adoc +61 -2
- data/Gemfile +0 -5
- data/README.adoc +67 -5
- data/kramdown-asciidoc.gemspec +2 -1
- data/lib/kramdown-asciidoc.rb +1 -0
- data/lib/kramdown-asciidoc/api.rb +7 -4
- data/lib/kramdown-asciidoc/cli.rb +9 -1
- data/lib/kramdown-asciidoc/converter.rb +104 -21
- data/lib/kramdown-asciidoc/core_ext/regexp/is_match.rb +5 -3
- data/lib/kramdown-asciidoc/kramdown_ext/parser/base.rb +7 -0
- data/lib/kramdown-asciidoc/version.rb +1 -1
- data/lib/kramdown-asciidoc/writer.rb +16 -22
- data/spec/api_spec.rb +70 -0
- data/spec/cli_spec.rb +38 -6
- data/spec/scenarios/attributes/sorting.adoc +6 -0
- data/spec/scenarios/attributes/sorting.md +3 -0
- data/spec/scenarios/attributes/sorting.opts +3 -0
- data/spec/scenarios/codeblock/dlist-like.adoc +4 -0
- data/spec/scenarios/codeblock/dlist-like.md +2 -0
- data/spec/scenarios/codeblock/list-like.adoc +5 -0
- data/spec/scenarios/codeblock/list-like.md +3 -0
- data/spec/scenarios/heading/anchor-with-dot.adoc +2 -0
- data/spec/scenarios/heading/anchor-with-dot.md +1 -0
- data/spec/scenarios/heading/{auto-id-prefix.adoc → auto_ids/auto-id-prefix.adoc} +0 -0
- data/spec/scenarios/heading/{auto-id-prefix.md → auto_ids/auto-id-prefix.md} +0 -0
- data/spec/scenarios/heading/{auto-id-prefix.opts → auto_ids/auto-id-prefix.opts} +1 -1
- data/spec/scenarios/heading/auto_ids/character-reference.adoc +8 -0
- data/spec/scenarios/heading/auto_ids/character-reference.md +5 -0
- data/spec/scenarios/heading/{auto-ids.opts → auto_ids/character-reference.opts} +0 -0
- data/spec/scenarios/heading/{no-auto-ids.adoc → auto_ids/disabled.adoc} +0 -0
- data/spec/scenarios/heading/{no-auto-ids.md → auto_ids/disabled.md} +0 -0
- data/spec/scenarios/heading/{no-auto-ids.opts → auto_ids/disabled.opts} +0 -0
- data/spec/scenarios/heading/auto_ids/dot-separator.adoc +2 -0
- data/spec/scenarios/heading/auto_ids/dot-separator.md +1 -0
- data/spec/scenarios/heading/auto_ids/dot-separator.opts +2 -0
- data/spec/scenarios/heading/{auto-ids.adoc → auto_ids/explicit-id.adoc} +4 -1
- data/spec/scenarios/heading/{auto-ids.md → auto_ids/explicit-id.md} +3 -1
- data/spec/scenarios/heading/auto_ids/explicit-id.opts +1 -0
- data/spec/scenarios/heading/auto_ids/kramdown-input-character-reference.adoc +8 -0
- data/spec/scenarios/heading/auto_ids/kramdown-input-character-reference.md +5 -0
- data/spec/scenarios/heading/auto_ids/kramdown-input-character-reference.opts +2 -0
- data/spec/scenarios/heading/auto_ids/kramdown-input.adoc +14 -0
- data/spec/scenarios/heading/auto_ids/kramdown-input.md +9 -0
- data/spec/scenarios/heading/auto_ids/kramdown-input.opts +2 -0
- data/spec/scenarios/heading/auto_ids/no-ids.adoc +14 -0
- data/spec/scenarios/heading/auto_ids/no-ids.md +9 -0
- data/spec/scenarios/heading/auto_ids/no-ids.opts +1 -0
- data/spec/scenarios/heading/lazy_ids/auto-ids-sync-id-attrs.adoc +7 -0
- data/spec/scenarios/heading/lazy_ids/auto-ids-sync-id-attrs.md +5 -0
- data/spec/scenarios/heading/lazy_ids/auto-ids-sync-id-attrs.opts +5 -0
- data/spec/scenarios/heading/lazy_ids/auto-ids.adoc +10 -0
- data/spec/scenarios/heading/lazy_ids/auto-ids.md +7 -0
- data/spec/scenarios/heading/lazy_ids/auto-ids.opts +4 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-dot-separator.adoc +9 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-dot-separator.md +7 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-dot-separator.opts +3 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-prefix.adoc +9 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-prefix.md +7 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-prefix.opts +3 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-separator.adoc +9 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-separator.md +7 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-separator.opts +3 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-zero-separator.adoc +7 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-zero-separator.md +5 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids-zero-separator.opts +3 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids.adoc +15 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids.md +13 -0
- data/spec/scenarios/heading/lazy_ids/explicit-ids.opts +1 -0
- data/spec/scenarios/html_element/image-with-id.adoc +2 -0
- data/spec/scenarios/html_element/image-with-id.md +1 -0
- metadata +131 -24
- data/spec/writer_spec.rb +0 -10
@@ -0,0 +1,7 @@
|
|
1
|
+
class Kramdown::Parser::Base
|
2
|
+
# Overload parse to force value of :auto_ids option to false; handled by converter instead
|
3
|
+
def self.parse source, options
|
4
|
+
(parser = new source, (options.merge auto_ids: false, auto_id_stripping: false)).parse
|
5
|
+
[parser.root, parser.warnings]
|
6
|
+
end
|
7
|
+
end
|
@@ -2,11 +2,13 @@ module Kramdown; module AsciiDoc
|
|
2
2
|
class Writer
|
3
3
|
LF = ?\n
|
4
4
|
|
5
|
-
|
5
|
+
attr_accessor :doctitle
|
6
6
|
attr_reader :body
|
7
7
|
|
8
8
|
def initialize
|
9
|
-
@
|
9
|
+
@prologue = []
|
10
|
+
@doctitle = nil
|
11
|
+
@attributes = {}
|
10
12
|
@body = []
|
11
13
|
@nesting_stack = []
|
12
14
|
@block_delimiter = nil
|
@@ -14,29 +16,16 @@ module Kramdown; module AsciiDoc
|
|
14
16
|
@list_level = { list: 0, dlist: 0 }
|
15
17
|
end
|
16
18
|
|
17
|
-
def
|
18
|
-
|
19
|
-
doctitle_line.slice 2, doctitle_line.length
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def doctitle= new_doctitle
|
24
|
-
if (doctitle_idx = @header.index {|l| l.start_with? '= ' })
|
25
|
-
@header[doctitle_idx] = %(= #{new_doctitle})
|
26
|
-
else
|
27
|
-
@header.unshift %(= #{new_doctitle})
|
28
|
-
end
|
29
|
-
nil
|
19
|
+
def add_attributes new_attributes
|
20
|
+
@attributes.update new_attributes
|
30
21
|
end
|
31
22
|
|
32
|
-
def
|
33
|
-
|
34
|
-
nil
|
23
|
+
def add_prologue_line line
|
24
|
+
@prologue << line
|
35
25
|
end
|
36
26
|
|
37
|
-
def
|
38
|
-
@
|
39
|
-
nil
|
27
|
+
def add_prologue_lines lines
|
28
|
+
@prologue.push(*lines)
|
40
29
|
end
|
41
30
|
|
42
31
|
def start_block
|
@@ -125,7 +114,12 @@ module Kramdown; module AsciiDoc
|
|
125
114
|
end
|
126
115
|
|
127
116
|
def to_s
|
128
|
-
|
117
|
+
header = @prologue.dup
|
118
|
+
header << %(= #{@doctitle}) if @doctitle
|
119
|
+
@attributes.sort.each do |name, val|
|
120
|
+
header << (val.empty? ? %(:#{name}:) : %(:#{name}: #{val}))
|
121
|
+
end unless @attributes.empty?
|
122
|
+
(header.empty? ? @body : (header + (@body.empty? ? [] : [''] + @body))).join LF
|
129
123
|
end
|
130
124
|
end
|
131
125
|
end; end
|
data/spec/api_spec.rb
CHANGED
@@ -76,6 +76,15 @@ describe Kramdown::AsciiDoc do
|
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
+
it 'does not mutate options argument' do
|
80
|
+
the_output_file = Pathname.new output_file 'convert-with-options.adoc'
|
81
|
+
opts = { encode: true, to: the_output_file, attributes: {} }
|
82
|
+
hash = opts.hash
|
83
|
+
(expect subject.convert '**File > Save**', opts).to be_nil
|
84
|
+
(expect the_output_file.read).to eql %(:experimental:\n\nmenu:File[Save]\n)
|
85
|
+
(expect opts.hash).to eql hash
|
86
|
+
end
|
87
|
+
|
79
88
|
it 'adds line feed (EOL) to end of output document if non-empty' do
|
80
89
|
(expect subject.convert 'paragraph').to end_with ?\n
|
81
90
|
end
|
@@ -83,6 +92,32 @@ describe Kramdown::AsciiDoc do
|
|
83
92
|
it 'does not add line feed (EOL) to end of output document if empty' do
|
84
93
|
(expect subject.convert '').to be_empty
|
85
94
|
end
|
95
|
+
|
96
|
+
it 'duplicates value of :attributes option' do
|
97
|
+
input = <<~EOS
|
98
|
+
# Document Title
|
99
|
+
|
100
|
+
# Part 1
|
101
|
+
|
102
|
+
## Chapter A
|
103
|
+
|
104
|
+
so it begins
|
105
|
+
EOS
|
106
|
+
|
107
|
+
expected = <<~EOS
|
108
|
+
= Document Title
|
109
|
+
:doctype: book
|
110
|
+
|
111
|
+
= Part 1
|
112
|
+
|
113
|
+
== Chapter A
|
114
|
+
|
115
|
+
so it begins
|
116
|
+
EOS
|
117
|
+
|
118
|
+
(expect subject.convert input, attributes: (attributes = {})).to eql expected
|
119
|
+
(expect attributes).to be_empty
|
120
|
+
end
|
86
121
|
end
|
87
122
|
|
88
123
|
context '#convert_file' do
|
@@ -144,6 +179,41 @@ describe Kramdown::AsciiDoc do
|
|
144
179
|
`#{ruby} -E ISO-8859-1:ISO-8859-1 #{Shellwords.escape script_file}`
|
145
180
|
(expect IO.read the_output_file, mode: 'r:UTF-8').to eql %(#{source}\n)
|
146
181
|
end
|
182
|
+
|
183
|
+
it 'does not mutate options argument' do
|
184
|
+
another_source_file = Pathname.new output_file 'convert-file-with-options.md'
|
185
|
+
another_source_file.write '**File > Save**'
|
186
|
+
the_output_file = Pathname.new output_file 'convert-file-with-options.adoc'
|
187
|
+
opts = { to: the_output_file, attributes: {} }
|
188
|
+
hash = opts.hash
|
189
|
+
(expect subject.convert_file another_source_file, opts).to be_nil
|
190
|
+
(expect the_output_file.read).to eql %(:experimental:\n\nmenu:File[Save]\n)
|
191
|
+
(expect opts.hash).to eql hash
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'passes result through postprocess callback if given' do
|
195
|
+
the_output_file = output_file %(convert-file-api-#{object_id}.adoc)
|
196
|
+
postprocess = -> (asciidoc) { asciidoc.gsub 'become', 'become glorious' }
|
197
|
+
(expect subject.convert_file the_source_file, postprocess: postprocess).to be_nil
|
198
|
+
(expect Pathname.new the_output_file).to exist
|
199
|
+
(expect (IO.read the_output_file)).to eql %(Markdown was _here_, but it has become glorious *AsciiDoc*!\n)
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'passes krawdown document to postprocess method if arity is not 1' do
|
203
|
+
the_output_file = output_file %(convert-file-api-#{object_id}.adoc)
|
204
|
+
postprocess = -> (asciidoc, kramdown_doc) { asciidoc.gsub 'Markdown', kramdown_doc.options[:input] }
|
205
|
+
(expect subject.convert_file the_source_file, postprocess: postprocess).to be_nil
|
206
|
+
(expect Pathname.new the_output_file).to exist
|
207
|
+
(expect (IO.read the_output_file)).to eql %(GFM was _here_, but it has become *AsciiDoc*!\n)
|
208
|
+
end
|
209
|
+
|
210
|
+
it 'uses original source if postprocess callback returns falsy' do
|
211
|
+
the_output_file = output_file %(convert-file-api-#{object_id}.adoc)
|
212
|
+
postprocess = -> (asciidoc) { nil }
|
213
|
+
(expect subject.convert_file the_source_file, postprocess: postprocess).to be_nil
|
214
|
+
(expect Pathname.new the_output_file).to exist
|
215
|
+
(expect (IO.read the_output_file)).to eql %(Markdown was _here_, but it has become *AsciiDoc*!\n)
|
216
|
+
end
|
147
217
|
end
|
148
218
|
end
|
149
219
|
|
data/spec/cli_spec.rb
CHANGED
@@ -206,21 +206,24 @@ describe Kramdown::AsciiDoc::Cli do
|
|
206
206
|
(expect $stdout.string).to eql expected
|
207
207
|
end
|
208
208
|
|
209
|
-
it '
|
210
|
-
the_source_file = scenario_file 'heading/
|
211
|
-
expected = IO.read scenario_file 'heading/
|
209
|
+
it 'auto generates IDs for section titles when --auto-ids is used' do
|
210
|
+
the_source_file = scenario_file 'heading/auto_ids/no-ids.md'
|
211
|
+
expected = IO.read scenario_file 'heading/auto_ids/no-ids.adoc'
|
212
212
|
(expect subject.run %W(-o - --auto-ids #{the_source_file})).to eql 0
|
213
213
|
(expect $stdout.string).to eql expected
|
214
214
|
end
|
215
215
|
|
216
|
-
it 'adds prefix specified by --auto-id-prefix to any
|
217
|
-
the_source_file = scenario_file 'heading/
|
216
|
+
it 'adds prefix specified by --auto-id-prefix to any auto-generated section title ID' do
|
217
|
+
the_source_file = scenario_file 'heading/auto_ids/explicit-id.md'
|
218
218
|
expected = <<~EOS
|
219
219
|
[#_heading-1]
|
220
220
|
= Heading 1
|
221
221
|
|
222
|
+
[#_heading]
|
223
|
+
== Heading
|
224
|
+
|
222
225
|
[#_heading-2]
|
223
|
-
== Heading
|
226
|
+
== Heading
|
224
227
|
|
225
228
|
[#explicit-id]
|
226
229
|
=== Heading 3
|
@@ -232,6 +235,35 @@ describe Kramdown::AsciiDoc::Cli do
|
|
232
235
|
(expect $stdout.string).to eql expected
|
233
236
|
end
|
234
237
|
|
238
|
+
it 'uses separator specified by --auto-id-separator to any auto-generated section title ID' do
|
239
|
+
the_source_file = scenario_file 'heading/auto_ids/explicit-id.md'
|
240
|
+
expected = <<~EOS
|
241
|
+
[#_heading_1]
|
242
|
+
= Heading 1
|
243
|
+
|
244
|
+
[#_heading]
|
245
|
+
== Heading
|
246
|
+
|
247
|
+
[#_heading_2]
|
248
|
+
== Heading
|
249
|
+
|
250
|
+
[#explicit-id]
|
251
|
+
=== Heading 3
|
252
|
+
|
253
|
+
[#_back_to_heading_2]
|
254
|
+
== Back to Heading 2
|
255
|
+
EOS
|
256
|
+
(expect subject.run %W(-o - --auto-id-prefix=_ --auto-id-separator=_ --auto-ids #{the_source_file})).to eql 0
|
257
|
+
(expect $stdout.string).to eql expected
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'drops ID if matches auto-generated value when --lazy-ids is used' do
|
261
|
+
the_source_file = scenario_file 'heading/lazy_ids/explicit-ids.md'
|
262
|
+
expected = IO.read scenario_file 'heading/lazy_ids/explicit-ids.adoc'
|
263
|
+
(expect subject.run %W(-o - --lazy-ids #{the_source_file})).to eql 0
|
264
|
+
(expect $stdout.string).to eql expected
|
265
|
+
end
|
266
|
+
|
235
267
|
it 'adds specified attributes to document header' do
|
236
268
|
the_source_file = scenario_file 'root/header-and-body.md'
|
237
269
|
(expect subject.run %W(-o - -a idprefix -a idseparator=- #{the_source_file})).to eql 0
|
@@ -0,0 +1 @@
|
|
1
|
+
## <a id="a.b.and.c">A, B, & C</a>
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
## A, B, and C
|
@@ -0,0 +1 @@
|
|
1
|
+
:auto_ids: true
|
@@ -0,0 +1 @@
|
|
1
|
+
:auto_ids: true
|