Skip to content

Commit 44f428e

Browse files
authored
Asciidoctor: Remaining change admonishments (#573)
Adds support for `coming` and `deprecated` which function very similarly to `added`.
1 parent dd6232b commit 44f428e

File tree

7 files changed

+245
-183
lines changed

7 files changed

+245
-183
lines changed

resources/asciidoctor/lib/added/extension.rb

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
require 'asciidoctor/extensions'
2+
3+
include Asciidoctor
4+
5+
##
6+
# Extensions for marking when something was added, when it *will* be added, or
7+
# when it was deprecated.
8+
#
9+
# Usage
10+
#
11+
# added::[6.0.0-beta1]
12+
# coming::[6.0.0-beta1]
13+
# deprecated::[6.0.0-beta1]
14+
# Foo added:[6.0.0-beta1]
15+
# Foo coming:[6.0.0-beta1]
16+
# Foo deprecated:[6.0.0-beta1]
17+
#
18+
class ChangeAdmonishment < Extensions::Group
19+
def activate registry
20+
[
21+
[:added, 'added'],
22+
[:coming, 'changed'],
23+
[:deprecated, 'deleted'],
24+
].each { |(name, revisionflag)|
25+
registry.block_macro ChangeAdmonishmentBlock.new(revisionflag), name
26+
registry.inline_macro ChangeAdmonishmentInline.new(revisionflag), name
27+
}
28+
end
29+
30+
class ChangeAdmonishmentBlock < Extensions::BlockMacroProcessor
31+
use_dsl
32+
name_positional_attributes :version, :passtext
33+
34+
def initialize(revisionflag)
35+
super
36+
@revisionflag = revisionflag
37+
end
38+
39+
def process parent, target, attrs
40+
version = attrs[:version]
41+
# We can *almost* go through the standard :admonition conversion but
42+
# that won't render the revisionflag or the revision. So we have to
43+
# go with this funny compound pass thing.
44+
note = Block.new(parent, :pass, :content_model => :compound)
45+
note << Block.new(note, :pass,
46+
:source => "<note revisionflag=\"#{@revisionflag}\" revision=\"#{version}\">")
47+
note << Block.new(note, :paragraph,
48+
:source => attrs[:passtext],
49+
:subs => Substitutors::NORMAL_SUBS)
50+
note << Block.new(note, :pass, :source => "</note>")
51+
end
52+
end
53+
54+
class ChangeAdmonishmentInline < Extensions::InlineMacroProcessor
55+
use_dsl
56+
name_positional_attributes :version, :text
57+
with_format :short
58+
59+
def initialize(revisionflag)
60+
super
61+
@revisionflag = revisionflag
62+
end
63+
64+
def process parent, target, attrs
65+
if attrs[:text]
66+
<<~DOCBOOK
67+
<phrase revisionflag="#{@revisionflag}" revision="#{attrs[:version]}">
68+
#{attrs[:text]}
69+
</phrase>
70+
DOCBOOK
71+
else
72+
<<~DOCBOOK
73+
<phrase revisionflag="#{@revisionflag}" revision="#{attrs[:version]}"/>
74+
DOCBOOK
75+
end
76+
end
77+
end
78+
end

resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@
77
#
88
# Turns
99
# added[6.0.0-beta1]
10+
# coming[6.0.0-beta1]
11+
# deprecated[6.0.0-beta1]
1012
# Into
1113
# added::[6.0.0-beta1]
14+
# coming::[6.0.0-beta1]
15+
# deprecated::[6.0.0-beta1]
1216
# Because `::` is required by asciidoctor to invoke block macros but isn't
1317
# required by asciidoc.
1418
#
1519
# Turns
1620
# words words added[6.0.0-beta1]
21+
# words words changed[6.0.0-beta1]
22+
# words words deprecated[6.0.0-beta1]
1723
# Into
1824
# words words added:[6.0.0-beta1]
25+
# words words changed:[6.0.0-beta1]
26+
# words words deprecated:[6.0.0-beta1]
1927
# Because `:` is required by asciidoctor to invoke inline macros but isn't
2028
# required by asciidoc.
2129
#
@@ -140,12 +148,13 @@ def reader.process_line line
140148
@code_block_start = line
141149
end
142150
end
151+
supported = 'added|coming|deprecated'
143152
# First convert the "block" version of these macros. We convert them
144153
# to block macros because they are at the start of the line....
145-
line&.gsub!(/^(added)\[([^\]]*)\]/, '\1::[\2]')
154+
line&.gsub!(/^(#{supported})\[([^\]]*)\]/, '\1::[\2]')
146155
# Then convert the "inline" version of these macros. We convert them
147156
# to inline macros because they are *not* at the start of the line....
148-
line&.gsub!(/(added)\[([^\]]*)\]/, '\1:[\2]')
157+
line&.gsub!(/(#{supported})\[([^\]]*)\]/, '\1:[\2]')
149158
end
150159
end
151160
reader

resources/asciidoctor/lib/extensions.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
require_relative 'added/extension'
1+
require_relative 'change_admonishment/extension'
22
require_relative 'copy_images/extension'
33
require_relative 'cramped_include/extension'
44
require_relative 'edit_me/extension'
55
require_relative 'elastic_compat_tree_processor/extension'
66
require_relative 'elastic_compat_preprocessor/extension'
77
require_relative 'elastic_include_tagged/extension'
88

9-
Extensions.register Added
9+
Extensions.register ChangeAdmonishment
1010
Extensions.register do
1111
# Enable storing the source locations so we can look at them. This is required
1212
# for EditMe to get a nice location.

resources/asciidoctor/spec/added_spec.rb

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
require 'change_admonishment/extension'
2+
3+
RSpec.describe ChangeAdmonishment do
4+
before(:each) do
5+
Extensions.register ChangeAdmonishment
6+
end
7+
8+
after(:each) do
9+
Extensions.unregister_all
10+
end
11+
12+
[
13+
['added', 'added'],
14+
['coming', 'changed'],
15+
['deprecated', 'deleted']
16+
].each { |(name, revisionflag)|
17+
it "#{name}'s block version creates a note" do
18+
actual = convert <<~ASCIIDOC
19+
== Example
20+
#{name}::[some_version]
21+
ASCIIDOC
22+
expected = <<~DOCBOOK
23+
<chapter id="_example">
24+
<title>Example</title>
25+
<note revisionflag="#{revisionflag}" revision="some_version">
26+
<simpara></simpara>
27+
</note>
28+
</chapter>
29+
DOCBOOK
30+
expect(actual).to eq(expected.strip)
31+
end
32+
33+
it "#{name}'s block version supports asciidoc in the passtext" do
34+
actual = convert <<~ASCIIDOC
35+
== Example
36+
#{name}::[some_version,See <<some-reference>>]
37+
[[some-reference]]
38+
=== Some Reference
39+
ASCIIDOC
40+
expected = <<~DOCBOOK
41+
<chapter id="_example">
42+
<title>Example</title>
43+
<note revisionflag="#{revisionflag}" revision="some_version">
44+
<simpara>See <xref linkend="some-reference"/></simpara>
45+
</note>
46+
<section id="some-reference">
47+
<title>Some Reference</title>
48+
49+
</section>
50+
</chapter>
51+
DOCBOOK
52+
expect(actual).to eq(expected.strip)
53+
end
54+
55+
it "#{name}'s block version is not invoked without the ::" do
56+
actual = convert <<~ASCIIDOC
57+
== Example
58+
#{name}[some_version]
59+
ASCIIDOC
60+
expected = <<~DOCBOOK
61+
<chapter id="_example">
62+
<title>Example</title>
63+
<simpara>#{name}[some_version]</simpara>
64+
</chapter>
65+
DOCBOOK
66+
expect(actual).to eq(expected.strip)
67+
end
68+
69+
it "#{name}'s inline version creates a phrase" do
70+
actual = convert <<~ASCIIDOC
71+
== Example
72+
words #{name}:[some_version]
73+
ASCIIDOC
74+
expected = <<~DOCBOOK
75+
<chapter id="_example">
76+
<title>Example</title>
77+
<simpara>words <phrase revisionflag="#{revisionflag}" revision="some_version"/>
78+
</simpara>
79+
</chapter>
80+
DOCBOOK
81+
expect(actual).to eq(expected.strip)
82+
end
83+
84+
it "#{name}'s inline version creates a phrase with extra text if provided" do
85+
actual = convert <<~ASCIIDOC
86+
== Example
87+
words #{name}:[some_version, more words]
88+
ASCIIDOC
89+
expected = <<~DOCBOOK
90+
<chapter id="_example">
91+
<title>Example</title>
92+
<simpara>words <phrase revisionflag="#{revisionflag}" revision="some_version">
93+
more words
94+
</phrase>
95+
</simpara>
96+
</chapter>
97+
DOCBOOK
98+
expect(actual).to eq(expected.strip)
99+
end
100+
101+
it "#{name}'s inline version is not invoked without the :" do
102+
actual = convert <<~ASCIIDOC
103+
== Example
104+
words #{name}[some_version]
105+
ASCIIDOC
106+
expected = <<~DOCBOOK
107+
<chapter id="_example">
108+
<title>Example</title>
109+
<simpara>words #{name}[some_version]</simpara>
110+
</chapter>
111+
DOCBOOK
112+
expect(actual).to eq(expected.strip)
113+
end
114+
}
115+
end

0 commit comments

Comments
 (0)