Skip to content

Commit d77b993

Browse files
authored
Asciidoctor: Force callouts into source listing (#530)
Asciidoc automatically adds callouts to all source listing regardless of whether you ask for them or not and we rely on this behavior in the Elasticsearch docs. This emulates it in asciidoctor. It isn't super clean because we have to edit the text on the way in, but that is life sometimes.
1 parent 66c864b commit d77b993

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,38 @@
3131
# Because asciidoctor clears attributes set in a block. See
3232
# https://github.com/asciidoctor/asciidoctor/issues/2993
3333
#
34+
# Turns
35+
# ["source","sh",subs="attributes"]
36+
# --------------------------------------------
37+
# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}.zip
38+
# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}.zip.sha512
39+
# shasum -a 512 -c elasticsearch-{version}.zip.sha512 <1>
40+
# unzip elasticsearch-{version}.zip
41+
# cd elasticsearch-{version}/ <2>
42+
# --------------------------------------------
43+
# <1> Compares the SHA of the downloaded `.zip` archive and the published checksum, which should output
44+
# `elasticsearch-{version}.zip: OK`.
45+
# <2> This directory is known as `$ES_HOME`.
46+
#
47+
# Into
48+
# ["source","sh",subs="attributes,callouts"]
49+
# --------------------------------------------
50+
# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}.zip
51+
# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}.zip.sha512
52+
# shasum -a 512 -c elasticsearch-{version}.zip.sha512 <1>
53+
# unzip elasticsearch-{version}.zip
54+
# cd elasticsearch-{version}/ <2>
55+
# --------------------------------------------
56+
# <1> Compares the SHA of the downloaded `.zip` archive and the published checksum, which should output
57+
# `elasticsearch-{version}.zip: OK`.
58+
# <2> This directory is known as `$ES_HOME`.
59+
# Because asciidoc adds callouts to all "source" blocks. We'd *prefer* to do
60+
# this in the tree processor because it is less messy but we can't because
61+
# asciidoctor checks the `:callout` sub before giving us a chance to add it.
62+
#
3463
class ElasticCompatPreprocessor < Extensions::Preprocessor
3564
IncludeTaggedDirectiveRx = /^include-tagged::([^\[][^\[]*)\[(#{CC_ANY}+)?\]$/
65+
SourceWithSubsRx = /^\["source", ?"[^"]+", ?subs="(#{CC_ANY}+)"\]$/
3666

3767
def process document, reader
3868
reader.instance_variable_set :@in_attribute_only_block, false
@@ -46,8 +76,8 @@ def reader.process_line line
4676
else
4777
line
4878
end
49-
elsif IncludeTaggedDirectiveRx =~ line then
50-
if preprocess_include_directive "elastic-include-tagged:#{$1}", $2 then
79+
elsif IncludeTaggedDirectiveRx =~ line
80+
if preprocess_include_directive "elastic-include-tagged:#{$1}", $2
5181
nil
5282
else
5383
# the line was not a valid include line and we've logged a warning
@@ -61,12 +91,18 @@ def reader.process_line line
6191
lines.shift
6292
while Asciidoctor::AttributeEntryRx =~ (check_line = lines.shift)
6393
end
64-
if check_line == '--' then
94+
if check_line == '--'
6595
@in_attribute_only_block = true
6696
line.clear
6797
else
6898
line
6999
end
100+
elsif SourceWithSubsRx =~ line
101+
line = super
102+
unless $1.include?('callouts')
103+
line.sub! "subs=\"#{$1}\"", "subs=\"#{$1},callouts\""
104+
end
105+
line
70106
else
71107
line = super
72108
line&.gsub!(/(added)\[([^\]]*)\]/, '\1::[\2]')

resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,40 @@
165165
expect(actual).to eq(expected.strip)
166166
end
167167

168+
it "adds callouts to substitutions for source blocks if there aren't any" do
169+
actual = convert <<~ASCIIDOC
170+
== Example
171+
["source","sh",subs="attributes"]
172+
--------------------------------------------
173+
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}.zip
174+
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}.zip.sha512
175+
shasum -a 512 -c elasticsearch-{version}.zip.sha512 <1>
176+
unzip elasticsearch-{version}.zip
177+
cd elasticsearch-{version}/ <2>
178+
--------------------------------------------
179+
<1> Compares the SHA of the downloaded `.zip` archive and the published checksum, which should output
180+
`elasticsearch-{version}.zip: OK`.
181+
<2> This directory is known as `$ES_HOME`.
182+
ASCIIDOC
183+
expected = <<~DOCBOOK
184+
<chapter id="_example">
185+
<title>Example</title>
186+
<programlisting language="sh" linenumbering="unnumbered">wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}.zip
187+
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-{version}.zip.sha512
188+
shasum -a 512 -c elasticsearch-{version}.zip.sha512 <1>
189+
unzip elasticsearch-{version}.zip
190+
cd elasticsearch-{version}/ <2></programlisting>
191+
<calloutlist>
192+
<callout arearefs="CO1-1">
193+
<para>Compares the SHA of the downloaded <literal>.zip</literal> archive and the published checksum, which should output
194+
<literal>elasticsearch-{version}.zip: OK</literal>.</para>
195+
</callout>
196+
<callout arearefs="CO1-2">
197+
<para>This directory is known as <literal>$ES_HOME</literal>.</para>
198+
</callout>
199+
</calloutlist>
200+
</chapter>
201+
DOCBOOK
202+
expect(actual).to eq(expected.strip)
203+
end
168204
end

0 commit comments

Comments
 (0)