Skip to content

Commit 95dc7a3

Browse files
authored
Asciidoctor: Work around "cramped" include statements (#529)
We do a fair bit of: ``` include::resources/1.adoc[] include::resources/2.adoc[] ``` In our asciidoc files. Asciidoctor would prefer that we do: ``` include::resources/1.adoc[] include::resources/2.adoc[] ``` but asciidoc doesn't care. To be compatible with asciidoc this works around asciidoctor's preference, preventing the parsing error that come about because of using these "cramped" include statements by adding a trailing new line every time we include an asciidoc file. This is a little crude but it works like a charm. With this, I can now build the Elasticsearch Java REST Client reference without `--lenient` and the document looks great! This is how long the build takes with asciidoctor: ``` real 0m7.652s user 0m7.080s sys 0m0.538s ``` And this is how long it takes with asciidoc: ``` real 0m44.987s user 0m38.715s sys 0m6.256s ```
2 parents d77b993 + a3e61f0 commit 95dc7a3

File tree

9 files changed

+142
-18
lines changed

9 files changed

+142
-18
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'asciidoctor/extensions'
2+
3+
include Asciidoctor
4+
5+
# Preprocessor to support more "cramped" include statements. Usually something
6+
# like
7+
# include::resources/1.adoc[]
8+
# include::resources/2.adoc[]
9+
# will result in syntax errors if 1.adoc ends in only a single new line. Things
10+
# like callout lists require that they be followed by an empty line or else
11+
# the thing below them will get sucked into the callout list. This isn't a
12+
# problem with asciidoc, and to be better compatible with it try and work around
13+
# this problem by adding an extra new line after every sequence of lines we
14+
# include. In theory this *shouldn't* bother us because we don't include things
15+
# that are sensitive to the extra line.
16+
class CrampedInclude < Extensions::Preprocessor
17+
def process document, reader
18+
def reader.prepare_lines data, opts = {}
19+
super << ''
20+
end
21+
reader
22+
end
23+
end

resources/asciidoctor/lib/extensions.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative 'added/extension'
2+
require_relative 'cramped_include/extension'
23
require_relative 'edit_me/extension'
34
require_relative 'elastic_compat_tree_processor/extension'
45
require_relative 'elastic_compat_preprocessor/extension'
@@ -8,6 +9,7 @@
89
# Enable storing the source locations so we can look at them. This is required
910
# for EditMe to get a nice location.
1011
document.sourcemap = true
12+
preprocessor CrampedInclude
1113
preprocessor ElasticCompatPreprocessor
1214
treeprocessor EditMe
1315
treeprocessor ElasticCompatTreeProcessor
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
require 'cramped_include/extension'
2+
require 'shared_examples/does_not_break_line_numbers'
3+
4+
RSpec.describe CrampedInclude do
5+
before(:each) do
6+
Extensions.register do
7+
preprocessor CrampedInclude
8+
end
9+
end
10+
11+
after(:each) do
12+
Extensions.unregister_all
13+
end
14+
15+
include_examples "doesn't break line numbers"
16+
17+
it "allows cramped includes of callout lists" do
18+
actual = convert <<~ASCIIDOC
19+
= Test
20+
21+
== Test
22+
23+
include::resources/cramped_include/colist1.adoc[]
24+
include::resources/cramped_include/colist2.adoc[]
25+
ASCIIDOC
26+
expected = <<~DOCBOOK
27+
<chapter id="_test">
28+
<title>Test</title>
29+
<section id="P1">
30+
<title>P1</title>
31+
<section id="P1_1">
32+
<title>P1.1</title>
33+
<programlisting language="java" linenumbering="unnumbered">words <1> <2></programlisting>
34+
<calloutlist>
35+
<callout arearefs="CO1-1">
36+
<para>foo</para>
37+
</callout>
38+
</calloutlist>
39+
</section>
40+
</section>
41+
<section id="P2">
42+
<title>P2</title>
43+
<section id="P2_1">
44+
<title>P2.1</title>
45+
<programlisting language="java" linenumbering="unnumbered">words <1> <2></programlisting>
46+
<calloutlist>
47+
<callout arearefs="CO2-1">
48+
<para>foo</para>
49+
</callout>
50+
</calloutlist>
51+
</section>
52+
</section>
53+
</chapter>
54+
DOCBOOK
55+
expect(actual).to eq(expected.strip)
56+
end
57+
58+
it "doesn't break includes of non-asciidoc files" do
59+
actual = convert <<~ASCIIDOC
60+
----
61+
include::resources/cramped_include/Example.java[]
62+
----
63+
ASCIIDOC
64+
expected = <<~DOCBOOK
65+
<preface>
66+
<title></title>
67+
<screen>public class Example {}</screen>
68+
</preface>
69+
DOCBOOK
70+
expect(actual).to eq(expected.strip)
71+
end
72+
end

resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'added/extension'
22
require 'elastic_compat_preprocessor/extension'
33
require 'elastic_include_tagged/extension'
4+
require 'shared_examples/does_not_break_line_numbers'
45

56
RSpec.describe ElasticCompatPreprocessor do
67
before(:each) do
@@ -15,6 +16,8 @@
1516
Extensions.unregister_all
1617
end
1718

19+
include_examples "doesn't break line numbers"
20+
1821
it "invokes added[version]" do
1922
actual = convert <<~ASCIIDOC
2023
== Example
@@ -51,24 +54,6 @@
5154
expect(actual).to eq(expected.strip)
5255
end
5356

54-
it "doesn't break line numbers" do
55-
input = <<~ASCIIDOC
56-
---
57-
---
58-
<1> callout
59-
ASCIIDOC
60-
expect { convert(input) }.to raise_error(
61-
ConvertError, /<stdin>: line 3: no callout found for <1>/)
62-
end
63-
64-
it "doesn't break line numbers in included files" do
65-
input = <<~ASCIIDOC
66-
include::resources/elastic_compat_preprocessor/missing_callout.adoc[]
67-
ASCIIDOC
68-
expect { convert(input) }.to raise_error(
69-
ConvertError, /missing_callout.adoc: line 3: no callout found for <1>/)
70-
end
71-
7257
it "un-blocks blocks containing only attributes" do
7358
actual = convert <<~ASCIIDOC
7459
:inheader: foo
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public class Example {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[P1]]
2+
=== P1
3+
4+
[[P1_1]]
5+
==== P1.1
6+
7+
["source","java",subs="attributes,callouts,macros"]
8+
--------------------------------------------------
9+
words <1> <2>
10+
--------------------------------------------------
11+
<1> foo
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[P2]]
2+
=== P2
3+
4+
[[P2_1]]
5+
==== P2.1
6+
7+
["source","java",subs="attributes,callouts,macros"]
8+
--------------------------------------------------
9+
words <1> <2>
10+
--------------------------------------------------
11+
<1> foo
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
RSpec.shared_examples "doesn't break line numbers" do
2+
it "doesn't break line numbers" do
3+
input = <<~ASCIIDOC
4+
---
5+
---
6+
<1> callout
7+
ASCIIDOC
8+
expect { convert(input) }.to raise_error(
9+
ConvertError, /<stdin>: line 3: no callout found for <1>/)
10+
end
11+
12+
it "doesn't break line numbers in included files" do
13+
input = <<~ASCIIDOC
14+
include::resources/does_not_break_line_numbers/missing_callout.adoc[]
15+
ASCIIDOC
16+
expect { convert(input) }.to raise_error(
17+
ConvertError, /missing_callout.adoc: line 3: no callout found for <1>/)
18+
end
19+
end

0 commit comments

Comments
 (0)