Skip to content

Commit 02ffea4

Browse files
authored
Asciidoctor: Change invocation rules for macros (#799)
Changes the invocation rules for legacy style admonition macros to only invoke the block version if it is alone on a line. This more closely mirrors the customizations that Elastic did to AsciiDoc.
1 parent baafd30 commit 02ffea4

File tree

2 files changed

+97
-52
lines changed

2 files changed

+97
-52
lines changed

resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ class ElasticCompatPreprocessor < Asciidoctor::Extensions::Preprocessor
115115
CODE_BLOCK_RX = /^-----*$/
116116
SNIPPET_RX = %r{^//\s*(AUTOSENSE|KIBANA|CONSOLE|SENSE:[^\n<]+)$}
117117
LEGACY_MACROS = 'added|beta|coming|deprecated|experimental'
118-
LEGACY_BLOCK_MACRO_RX = /^(#{LEGACY_MACROS})\[([^\]]*)\]/
119-
LEGACY_INLINE_MACRO_RX = /(#{LEGACY_MACROS})\[([^\]]*)\]/
118+
LEGACY_BLOCK_MACRO_RX = /^\s*(#{LEGACY_MACROS})\[(.*)\]\s*$/
119+
LEGACY_INLINE_MACRO_RX = /(#{LEGACY_MACROS})\[(.*)\]/
120120

121121
def process(_document, reader)
122122
reader.instance_variable_set :@in_attribute_only_block, false
@@ -175,7 +175,7 @@ def reader.process_line(line)
175175
end
176176

177177
# First convert the "block" version of these macros. We convert them
178-
# to block macros because they are at the start of the line....
178+
# to block macros because they are alone on a line
179179
line&.gsub!(LEGACY_BLOCK_MACRO_RX, '\1::[\2]')
180180
# Then convert the "inline" version of these macros. We convert them
181181
# to inline macros because they are *not* at the start of the line....

resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb

Lines changed: 94 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,57 +30,102 @@
3030

3131
include_examples "doesn't break line numbers"
3232

33-
[
34-
%w[added added note],
35-
%w[coming changed note],
36-
%w[deprecated deleted warning],
37-
].each do |(name, revisionflag, tag)|
38-
it "invokes the #{name} block macro when #{name}[version] starts a line" do
39-
actual = convert <<~ASCIIDOC
40-
== Example
41-
#{name}[some_version]
42-
ASCIIDOC
43-
expected = <<~DOCBOOK
44-
<chapter id="_example">
45-
<title>Example</title>
46-
<#{tag} revisionflag="#{revisionflag}" revision="some_version">
47-
<simpara></simpara>
48-
</#{tag}>
49-
</chapter>
50-
DOCBOOK
51-
expect(actual).to eq(expected.strip)
52-
end
53-
54-
it "invokes the #{name} inline macro when #{name}[version] is otherwise on the line" do
55-
actual = convert <<~ASCIIDOC
56-
== Example
57-
words #{name}[some_version]
58-
ASCIIDOC
59-
expected = <<~DOCBOOK
60-
<chapter id="_example">
61-
<title>Example</title>
62-
<simpara>words <phrase revisionflag="#{revisionflag}" revision="some_version"/>
63-
</simpara>
64-
</chapter>
65-
DOCBOOK
66-
expect(actual).to eq(expected.strip)
67-
end
68-
69-
it "doesn't mind skipped #{name} block macros" do
70-
actual = convert <<~ASCIIDOC
71-
== Example
33+
context 'change admonitions' do
34+
shared_examples 'change admonition' do
35+
include_context 'convert without logs'
36+
37+
shared_examples 'invokes the block macro' do
38+
let(:expected) do
39+
<<~DOCBOOK
40+
<#{tag} revisionflag="#{revisionflag}" revision="some_version">
41+
<simpara></simpara>
42+
</#{tag}>
43+
DOCBOOK
44+
end
45+
it 'invokes the block macro' do
46+
expect(converted).to include(expected)
47+
end
48+
end
49+
context 'when the admonition is alone on a line' do
50+
let(:input) { "#{name}[some_version]" }
51+
include_examples 'invokes the block macro'
52+
end
53+
context 'when the admonition has spaces before it' do
54+
let(:input) { " #{name}[some_version]" }
55+
include_examples 'invokes the block macro'
56+
end
57+
context 'when the admonition has spaces after it' do
58+
let(:input) { "#{name}[some_version] " }
59+
include_examples 'invokes the block macro'
60+
end
61+
context 'when the admonition has a `]` in it' do
62+
let(:input) { "#{name}[some_version, link:link.html[Title]]" }
63+
include_examples 'invokes the block macro'
64+
let(:expected) do
65+
<<~DOCBOOK
66+
<#{tag} revisionflag="#{revisionflag}" revision="some_version">
67+
<simpara><ulink url="link.html">Title</ulink></simpara>
68+
</#{tag}>
69+
DOCBOOK
70+
end
71+
end
7272

73-
ifeval::["true" == "false"]
74-
#{name}[some_version]
75-
#endif::[]
76-
ASCIIDOC
77-
expected = <<~DOCBOOK
78-
<chapter id="_example">
79-
<title>Example</title>
73+
shared_examples 'invokes the inline macro' do
74+
it 'invokes the inline macro' do
75+
expect(converted).to include(
76+
%(<phrase revisionflag="#{revisionflag}" revision="some_version"/>)
77+
)
78+
end
79+
end
80+
context "when the admonition is surrounded by other text" do
81+
let(:input) { "words #{name}[some_version] words" }
82+
include_examples 'invokes the inline macro'
83+
end
84+
context "when the admonition has text before it" do
85+
let(:input) { "words #{name}[some_version]" }
86+
include_examples 'invokes the inline macro'
87+
end
88+
context "when the admonition has text after it" do
89+
let(:input) { "#{name}[some_version] words" }
90+
include_examples 'invokes the inline macro'
91+
end
8092

81-
</chapter>
82-
DOCBOOK
83-
expect(actual).to eq(expected.strip)
93+
context 'when the admonition is skipped' do
94+
let(:input) do
95+
<<~ASCIIDOC
96+
words before skip
97+
ifeval::["true" == "false"]
98+
#{name}[some_version]
99+
endif::[]
100+
words after skip
101+
ASCIIDOC
102+
end
103+
it 'skips the admonition' do
104+
expect(converted).not_to include('revisionflag')
105+
end
106+
it 'properly converts the rest of the text' do
107+
expect(converted).to include('words before skip')
108+
expect(converted).to include('words after skip')
109+
end
110+
end
111+
end
112+
context 'for added' do
113+
include_context 'change admonition'
114+
let(:name) { 'added' }
115+
let(:revisionflag) { 'added' }
116+
let(:tag) { 'note' }
117+
end
118+
context 'for coming' do
119+
include_context 'change admonition'
120+
let(:name) { 'coming' }
121+
let(:revisionflag) { 'changed' }
122+
let(:tag) { 'note' }
123+
end
124+
context 'for added' do
125+
include_context 'change admonition'
126+
let(:name) { 'deprecated' }
127+
let(:revisionflag) { 'deleted' }
128+
let(:tag) { 'warning' }
84129
end
85130
end
86131

0 commit comments

Comments
 (0)