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.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.adoc +61 -2
  3. data/Gemfile +0 -5
  4. data/README.adoc +67 -5
  5. data/kramdown-asciidoc.gemspec +2 -1
  6. data/lib/kramdown-asciidoc.rb +1 -0
  7. data/lib/kramdown-asciidoc/api.rb +7 -4
  8. data/lib/kramdown-asciidoc/cli.rb +9 -1
  9. data/lib/kramdown-asciidoc/converter.rb +104 -21
  10. data/lib/kramdown-asciidoc/core_ext/regexp/is_match.rb +5 -3
  11. data/lib/kramdown-asciidoc/kramdown_ext/parser/base.rb +7 -0
  12. data/lib/kramdown-asciidoc/version.rb +1 -1
  13. data/lib/kramdown-asciidoc/writer.rb +16 -22
  14. data/spec/api_spec.rb +70 -0
  15. data/spec/cli_spec.rb +38 -6
  16. data/spec/scenarios/attributes/sorting.adoc +6 -0
  17. data/spec/scenarios/attributes/sorting.md +3 -0
  18. data/spec/scenarios/attributes/sorting.opts +3 -0
  19. data/spec/scenarios/codeblock/dlist-like.adoc +4 -0
  20. data/spec/scenarios/codeblock/dlist-like.md +2 -0
  21. data/spec/scenarios/codeblock/list-like.adoc +5 -0
  22. data/spec/scenarios/codeblock/list-like.md +3 -0
  23. data/spec/scenarios/heading/anchor-with-dot.adoc +2 -0
  24. data/spec/scenarios/heading/anchor-with-dot.md +1 -0
  25. data/spec/scenarios/heading/{auto-id-prefix.adoc → auto_ids/auto-id-prefix.adoc} +0 -0
  26. data/spec/scenarios/heading/{auto-id-prefix.md → auto_ids/auto-id-prefix.md} +0 -0
  27. data/spec/scenarios/heading/{auto-id-prefix.opts → auto_ids/auto-id-prefix.opts} +1 -1
  28. data/spec/scenarios/heading/auto_ids/character-reference.adoc +8 -0
  29. data/spec/scenarios/heading/auto_ids/character-reference.md +5 -0
  30. data/spec/scenarios/heading/{auto-ids.opts → auto_ids/character-reference.opts} +0 -0
  31. data/spec/scenarios/heading/{no-auto-ids.adoc → auto_ids/disabled.adoc} +0 -0
  32. data/spec/scenarios/heading/{no-auto-ids.md → auto_ids/disabled.md} +0 -0
  33. data/spec/scenarios/heading/{no-auto-ids.opts → auto_ids/disabled.opts} +0 -0
  34. data/spec/scenarios/heading/auto_ids/dot-separator.adoc +2 -0
  35. data/spec/scenarios/heading/auto_ids/dot-separator.md +1 -0
  36. data/spec/scenarios/heading/auto_ids/dot-separator.opts +2 -0
  37. data/spec/scenarios/heading/{auto-ids.adoc → auto_ids/explicit-id.adoc} +4 -1
  38. data/spec/scenarios/heading/{auto-ids.md → auto_ids/explicit-id.md} +3 -1
  39. data/spec/scenarios/heading/auto_ids/explicit-id.opts +1 -0
  40. data/spec/scenarios/heading/auto_ids/kramdown-input-character-reference.adoc +8 -0
  41. data/spec/scenarios/heading/auto_ids/kramdown-input-character-reference.md +5 -0
  42. data/spec/scenarios/heading/auto_ids/kramdown-input-character-reference.opts +2 -0
  43. data/spec/scenarios/heading/auto_ids/kramdown-input.adoc +14 -0
  44. data/spec/scenarios/heading/auto_ids/kramdown-input.md +9 -0
  45. data/spec/scenarios/heading/auto_ids/kramdown-input.opts +2 -0
  46. data/spec/scenarios/heading/auto_ids/no-ids.adoc +14 -0
  47. data/spec/scenarios/heading/auto_ids/no-ids.md +9 -0
  48. data/spec/scenarios/heading/auto_ids/no-ids.opts +1 -0
  49. data/spec/scenarios/heading/lazy_ids/auto-ids-sync-id-attrs.adoc +7 -0
  50. data/spec/scenarios/heading/lazy_ids/auto-ids-sync-id-attrs.md +5 -0
  51. data/spec/scenarios/heading/lazy_ids/auto-ids-sync-id-attrs.opts +5 -0
  52. data/spec/scenarios/heading/lazy_ids/auto-ids.adoc +10 -0
  53. data/spec/scenarios/heading/lazy_ids/auto-ids.md +7 -0
  54. data/spec/scenarios/heading/lazy_ids/auto-ids.opts +4 -0
  55. data/spec/scenarios/heading/lazy_ids/explicit-ids-dot-separator.adoc +9 -0
  56. data/spec/scenarios/heading/lazy_ids/explicit-ids-dot-separator.md +7 -0
  57. data/spec/scenarios/heading/lazy_ids/explicit-ids-dot-separator.opts +3 -0
  58. data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-prefix.adoc +9 -0
  59. data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-prefix.md +7 -0
  60. data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-prefix.opts +3 -0
  61. data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-separator.adoc +9 -0
  62. data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-separator.md +7 -0
  63. data/spec/scenarios/heading/lazy_ids/explicit-ids-empty-separator.opts +3 -0
  64. data/spec/scenarios/heading/lazy_ids/explicit-ids-zero-separator.adoc +7 -0
  65. data/spec/scenarios/heading/lazy_ids/explicit-ids-zero-separator.md +5 -0
  66. data/spec/scenarios/heading/lazy_ids/explicit-ids-zero-separator.opts +3 -0
  67. data/spec/scenarios/heading/lazy_ids/explicit-ids.adoc +15 -0
  68. data/spec/scenarios/heading/lazy_ids/explicit-ids.md +13 -0
  69. data/spec/scenarios/heading/lazy_ids/explicit-ids.opts +1 -0
  70. data/spec/scenarios/html_element/image-with-id.adoc +2 -0
  71. data/spec/scenarios/html_element/image-with-id.md +1 -0
  72. metadata +131 -24
  73. data/spec/writer_spec.rb +0 -10
@@ -1,3 +1,5 @@
1
- class Regexp
2
- alias match? === unless method_defined? :match? # nocov
3
- end
1
+ module Kramdown; module AsciiDoc; module CoreExt
2
+ refine Regexp do
3
+ alias match? === unless method_defined? :match? # nocov
4
+ end
5
+ end; end; end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Kramdown; module AsciiDoc
2
- VERSION = '1.0.0.alpha.12'
2
+ VERSION = '1.0.0.alpha.13'
3
3
  end; end
@@ -2,11 +2,13 @@ module Kramdown; module AsciiDoc
2
2
  class Writer
3
3
  LF = ?\n
4
4
 
5
- attr_reader :header
5
+ attr_accessor :doctitle
6
6
  attr_reader :body
7
7
 
8
8
  def initialize
9
- @header = []
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 doctitle
18
- if (doctitle_line = @header.find {|l| l.start_with? '= ' })
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 add_attributes attributes
33
- attributes.each {|k, v| add_attribute k, v }
34
- nil
23
+ def add_prologue_line line
24
+ @prologue << line
35
25
  end
36
26
 
37
- def add_attribute name, value
38
- @header << %(:#{name}:#{value.empty? ? '' : ' ' + value.to_s})
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
- (@header.empty? ? @body : (@header + (@body.empty? ? [] : [''] + @body))).join LF
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
@@ -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
 
@@ -206,21 +206,24 @@ describe Kramdown::AsciiDoc::Cli do
206
206
  (expect $stdout.string).to eql expected
207
207
  end
208
208
 
209
- it 'automatically generates IDs for section titles when --auto-ids is used' do
210
- the_source_file = scenario_file 'heading/auto-ids.md'
211
- expected = IO.read scenario_file 'heading/auto-ids.adoc'
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 automatically generated section title ID' do
217
- the_source_file = scenario_file 'heading/auto-ids.md'
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 2
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,6 @@
1
+ = Document Title
2
+ :bar: bar
3
+ :experimental:
4
+ :foo: foo
5
+
6
+ menu:Terminal[Quit]
@@ -0,0 +1,3 @@
1
+ # Document Title
2
+
3
+ **Terminal > Quit**
@@ -0,0 +1,3 @@
1
+ :attributes:
2
+ foo: 'foo'
3
+ bar: 'bar'
@@ -0,0 +1,4 @@
1
+ ....
2
+ filter :: *
3
+ matched 10 results
4
+ ....
@@ -0,0 +1,2 @@
1
+ filter :: *
2
+ matched 10 results
@@ -0,0 +1,5 @@
1
+ ....
2
+ * Connected to localhost (127.0.0.1) port 5252 (#0)
3
+ > GET / HTTP/1.1
4
+ > Host: localhost:5252
5
+ ....
@@ -0,0 +1,3 @@
1
+ * Connected to localhost (127.0.0.1) port 5252 (#0)
2
+ > GET / HTTP/1.1
3
+ > Host: localhost:5252
@@ -0,0 +1,2 @@
1
+ [id=a.b.and.c]
2
+ == A, B, & C
@@ -0,0 +1 @@
1
+ ## <a id="a.b.and.c">A, B, & C</a>
@@ -1,2 +1,2 @@
1
- :auto_id_prefix: id-
2
1
  :auto_ids: true
2
+ :auto_id_prefix: id-
@@ -0,0 +1,8 @@
1
+ [#use-for-copyright]
2
+ == Use &copy; for copyright
3
+
4
+ [#when-to-use-in-tables]
5
+ == When to use | in tables
6
+
7
+ [#all-in]
8
+ == *All In!*
@@ -0,0 +1,5 @@
1
+ ## Use &copy; for copyright
2
+
3
+ ## When to use &#124; in tables
4
+
5
+ ## **All In!**
@@ -0,0 +1,2 @@
1
+ [id=a.b.and.c]
2
+ == A, B, and C
@@ -0,0 +1 @@
1
+ ## A, B, and C
@@ -0,0 +1,2 @@
1
+ :auto_ids: true
2
+ :auto_id_separator: .
@@ -1,8 +1,11 @@
1
1
  [#heading-1]
2
2
  = Heading 1
3
3
 
4
+ [#heading]
5
+ == Heading
6
+
4
7
  [#heading-2]
5
- == Heading 2
8
+ == Heading
6
9
 
7
10
  [#explicit-id]
8
11
  === Heading 3
@@ -1,6 +1,8 @@
1
1
  # Heading 1
2
2
 
3
- ## Heading 2
3
+ ## Heading
4
+
5
+ ## Heading
4
6
 
5
7
  ### <a id="explicit-id"></a>Heading 3
6
8
 
@@ -0,0 +1 @@
1
+ :auto_ids: true
@@ -0,0 +1,8 @@
1
+ [#use-for-copyright]
2
+ == Use &copy; for copyright
3
+
4
+ [#when-to-use-in-tables]
5
+ == When to use | in tables
6
+
7
+ [#all-in]
8
+ == *All In!*
@@ -0,0 +1,5 @@
1
+ ## Use &copy; for copyright
2
+
3
+ ## When to use &#124; in tables
4
+
5
+ ## **All In!**
@@ -0,0 +1,2 @@
1
+ :auto_ids: true
2
+ :input: kramdown
@@ -0,0 +1,14 @@
1
+ [#document-title]
2
+ = Document Title
3
+
4
+ [#heading]
5
+ == Heading
6
+
7
+ [#heading-2]
8
+ == Heading
9
+
10
+ [#explicit-id]
11
+ === Heading 3
12
+
13
+ [#back-to-heading-2]
14
+ == Back to Heading 2
@@ -0,0 +1,9 @@
1
+ # Document Title
2
+
3
+ ## Heading
4
+
5
+ ## Heading
6
+
7
+ ### Heading 3 {#explicit-id}
8
+
9
+ ## Back to Heading 2
@@ -0,0 +1,2 @@
1
+ :auto_ids: true
2
+ :input: kramdown
@@ -0,0 +1,14 @@
1
+ [#heading-1]
2
+ = Heading 1
3
+
4
+ [#heading]
5
+ == Heading
6
+
7
+ [#heading-2]
8
+ == Heading
9
+
10
+ [#heading-3]
11
+ === Heading 3
12
+
13
+ [#back-to-heading-2]
14
+ == Back to Heading 2
@@ -0,0 +1,9 @@
1
+ # Heading 1
2
+
3
+ ## Heading
4
+
5
+ ## Heading
6
+
7
+ ### Heading 3
8
+
9
+ ## Back to Heading 2
@@ -0,0 +1 @@
1
+ :auto_ids: true