Skip to content

Commit 044e33a

Browse files
authored
Asciidoctor: Support added inline macro (#548)
Adds support for the added inline macro which would normally be specified like this: ``` words added:[version] ``` But for compatibility with Elastic's asciidoc customizations we support specifying it like this as well: ``` words added[version] ``` Note that we'll want to build on this to add support for `coming` and `deprecated` as well.
1 parent 7d8b6d1 commit 044e33a

File tree

5 files changed

+117
-11
lines changed

5 files changed

+117
-11
lines changed

resources/asciidoctor/lib/added/extension.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
include Asciidoctor
44

5+
class Added < Extensions::Group
6+
def activate registry
7+
registry.block_macro AddedBlock
8+
registry.inline_macro AddedInline
9+
end
10+
end
11+
512
# Extension for marking when something was added.
613
#
714
# Usage
@@ -23,3 +30,30 @@ def process parent, target, attrs
2330
create_pass_block parent, docbook, {}, subs: nil
2431
end
2532
end
33+
34+
# Extension for marking when something was added.
35+
#
36+
# Usage
37+
#
38+
# Foo added:[6.0.0-beta1]
39+
#
40+
class AddedInline < Extensions::InlineMacroProcessor
41+
use_dsl
42+
named :added
43+
name_positional_attributes :version, :text
44+
with_format :short
45+
46+
def process parent, target, attrs
47+
if attrs[:text]
48+
<<~DOCBOOK
49+
<phrase revisionflag="added" revision="#{attrs[:version]}">
50+
#{attrs[:text]}
51+
</phrase>
52+
DOCBOOK
53+
else
54+
<<~DOCBOOK
55+
<phrase revisionflag="added" revision="#{attrs[:version]}"/>
56+
DOCBOOK
57+
end
58+
end
59+
end

resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99
# added[6.0.0-beta1]
1010
# Into
1111
# added::[6.0.0-beta1]
12-
# Because `::` is required by asciidoctor but isn't by asciidoc.
12+
# Because `::` is required by asciidoctor to invoke block macros but isn't
13+
# required by asciidoc.
14+
#
15+
# Turns
16+
# words words added[6.0.0-beta1]
17+
# Into
18+
# words words added:[6.0.0-beta1]
19+
# Because `:` is required by asciidoctor to invoke inline macros but isn't
20+
# required by asciidoc.
1321
#
1422
# Turns
1523
# include-tagged::foo[tag]
@@ -132,7 +140,12 @@ def reader.process_line line
132140
@code_block_start = line
133141
end
134142
end
135-
line&.gsub!(/(added)\[([^\]]*)\]/, '\1::[\2]')
143+
# First convert the "block" version of these macros. We convert them
144+
# to block macros because they are at the start of the line....
145+
line&.gsub!(/^(added)\[([^\]]*)\]/, '\1::[\2]')
146+
# Then convert the "inline" version of these macros. We convert them
147+
# to inline macros because they are *not* at the start of the line....
148+
line&.gsub!(/(added)\[([^\]]*)\]/, '\1:[\2]')
136149
end
137150
end
138151
reader

resources/asciidoctor/lib/extensions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require_relative 'elastic_compat_preprocessor/extension'
66
require_relative 'elastic_include_tagged/extension'
77

8+
Extensions.register Added
89
Extensions.register do
910
# Enable storing the source locations so we can look at them. This is required
1011
# for EditMe to get a nice location.
@@ -14,5 +15,4 @@
1415
treeprocessor EditMe
1516
treeprocessor ElasticCompatTreeProcessor
1617
include_processor ElasticIncludeTagged
17-
block_macro AddedBlock
1818
end

resources/asciidoctor/spec/added_spec.rb

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
require 'added/extension'
22

3-
RSpec.describe AddedBlock do
3+
RSpec.describe Added do
44
before(:each) do
5-
Extensions.register do
6-
block_macro AddedBlock
7-
end
5+
Extensions.register Added
86
end
97

108
after(:each) do
119
Extensions.unregister_all
1210
end
1311

14-
it "creates a note" do
12+
it "block version creates a note" do
1513
actual = convert <<~ASCIIDOC
1614
== Example
1715
added::[some_version]
@@ -27,7 +25,7 @@
2725
expect(actual).to eq(expected.strip)
2826
end
2927

30-
it "is not invoked without the ::" do
28+
it "block version is not invoked without the ::" do
3129
actual = convert <<~ASCIIDOC
3230
== Example
3331
added[some_version]
@@ -40,4 +38,50 @@
4038
DOCBOOK
4139
expect(actual).to eq(expected.strip)
4240
end
41+
42+
it "inline version creates a phrase" do
43+
actual = convert <<~ASCIIDOC
44+
== Example
45+
words added:[some_version]
46+
ASCIIDOC
47+
expected = <<~DOCBOOK
48+
<chapter id="_example">
49+
<title>Example</title>
50+
<simpara>words <phrase revisionflag="added" revision="some_version"/>
51+
</simpara>
52+
</chapter>
53+
DOCBOOK
54+
expect(actual).to eq(expected.strip)
55+
end
56+
57+
it "inline version creates a phrase with extra text if provided" do
58+
actual = convert <<~ASCIIDOC
59+
== Example
60+
words added:[some_version, more words]
61+
ASCIIDOC
62+
expected = <<~DOCBOOK
63+
<chapter id="_example">
64+
<title>Example</title>
65+
<simpara>words <phrase revisionflag="added" revision="some_version">
66+
more words
67+
</phrase>
68+
</simpara>
69+
</chapter>
70+
DOCBOOK
71+
expect(actual).to eq(expected.strip)
72+
end
73+
74+
it "inline version is not invoked without the :" do
75+
actual = convert <<~ASCIIDOC
76+
== Example
77+
words added[some_version]
78+
ASCIIDOC
79+
expected = <<~DOCBOOK
80+
<chapter id="_example">
81+
<title>Example</title>
82+
<simpara>words added[some_version]</simpara>
83+
</chapter>
84+
DOCBOOK
85+
expect(actual).to eq(expected.strip)
86+
end
4387
end

resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
RSpec.describe ElasticCompatPreprocessor do
77
before(:each) do
8+
Extensions.register Added
89
Extensions.register do
910
preprocessor ElasticCompatPreprocessor
1011
include_processor ElasticIncludeTagged
11-
block_macro AddedBlock
1212
end
1313
end
1414

@@ -18,7 +18,7 @@
1818

1919
include_examples "doesn't break line numbers"
2020

21-
it "invokes added[version]" do
21+
it "invokes the added block macro when added[version] starts a line" do
2222
actual = convert <<~ASCIIDOC
2323
== Example
2424
added[some_version]
@@ -34,6 +34,21 @@
3434
expect(actual).to eq(expected.strip)
3535
end
3636

37+
it "invokes the added inline macro when added[version] is otherwise on the line" do
38+
actual = convert <<~ASCIIDOC
39+
== Example
40+
words added[some_version]
41+
ASCIIDOC
42+
expected = <<~DOCBOOK
43+
<chapter id="_example">
44+
<title>Example</title>
45+
<simpara>words <phrase revisionflag="added" revision="some_version"/>
46+
</simpara>
47+
</chapter>
48+
DOCBOOK
49+
expect(actual).to eq(expected.strip)
50+
end
51+
3752
it "invokes include-tagged::" do
3853
actual = convert <<~ASCIIDOC
3954
== Example

0 commit comments

Comments
 (0)