Skip to content

Asciidoctor: Work around "cramped" include statements #529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions resources/asciidoctor/lib/cramped_include/extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'asciidoctor/extensions'

include Asciidoctor

# Preprocessor to support more "cramped" include statements. Usually something
# like
# include::resources/1.adoc[]
# include::resources/2.adoc[]
# will result in syntax errors if 1.adoc ends in only a single new line. Things
# like callout lists require that they be followed by an empty line or else
# the thing below them will get sucked into the callout list. This isn't a
# problem with asciidoc, and to be better compatible with it try and work around
# this problem by adding an extra new line after every sequence of lines we
# include. In theory this *shouldn't* bother us because we don't include things
# that are sensitive to the extra line.
class CrampedInclude < Extensions::Preprocessor
def process document, reader
def reader.prepare_lines data, opts = {}
super << ''
end
reader
end
end
2 changes: 2 additions & 0 deletions resources/asciidoctor/lib/extensions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'added/extension'
require_relative 'cramped_include/extension'
require_relative 'edit_me/extension'
require_relative 'elastic_compat_tree_processor/extension'
require_relative 'elastic_compat_preprocessor/extension'
Expand All @@ -8,6 +9,7 @@
# Enable storing the source locations so we can look at them. This is required
# for EditMe to get a nice location.
document.sourcemap = true
preprocessor CrampedInclude
preprocessor ElasticCompatPreprocessor
treeprocessor EditMe
treeprocessor ElasticCompatTreeProcessor
Expand Down
72 changes: 72 additions & 0 deletions resources/asciidoctor/spec/cramped_include_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'cramped_include/extension'
require 'shared_examples/does_not_break_line_numbers'

RSpec.describe CrampedInclude do
before(:each) do
Extensions.register do
preprocessor CrampedInclude
end
end

after(:each) do
Extensions.unregister_all
end

include_examples "doesn't break line numbers"

it "allows cramped includes of callout lists" do
actual = convert <<~ASCIIDOC
= Test

== Test

include::resources/cramped_include/colist1.adoc[]
include::resources/cramped_include/colist2.adoc[]
ASCIIDOC
expected = <<~DOCBOOK
<chapter id="_test">
<title>Test</title>
<section id="P1">
<title>P1</title>
<section id="P1_1">
<title>P1.1</title>
<programlisting language="java" linenumbering="unnumbered">words <1> <2></programlisting>
<calloutlist>
<callout arearefs="CO1-1">
<para>foo</para>
</callout>
</calloutlist>
</section>
</section>
<section id="P2">
<title>P2</title>
<section id="P2_1">
<title>P2.1</title>
<programlisting language="java" linenumbering="unnumbered">words <1> <2></programlisting>
<calloutlist>
<callout arearefs="CO2-1">
<para>foo</para>
</callout>
</calloutlist>
</section>
</section>
</chapter>
DOCBOOK
expect(actual).to eq(expected.strip)
end

it "doesn't break includes of non-asciidoc files" do
actual = convert <<~ASCIIDOC
----
include::resources/cramped_include/Example.java[]
----
ASCIIDOC
expected = <<~DOCBOOK
<preface>
<title></title>
<screen>public class Example {}</screen>
</preface>
DOCBOOK
expect(actual).to eq(expected.strip)
end
end
21 changes: 3 additions & 18 deletions resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'added/extension'
require 'elastic_compat_preprocessor/extension'
require 'elastic_include_tagged/extension'
require 'shared_examples/does_not_break_line_numbers'

RSpec.describe ElasticCompatPreprocessor do
before(:each) do
Expand All @@ -15,6 +16,8 @@
Extensions.unregister_all
end

include_examples "doesn't break line numbers"

it "invokes added[version]" do
actual = convert <<~ASCIIDOC
== Example
Expand Down Expand Up @@ -51,24 +54,6 @@
expect(actual).to eq(expected.strip)
end

it "doesn't break line numbers" do
input = <<~ASCIIDOC
---
---
<1> callout
ASCIIDOC
expect { convert(input) }.to raise_error(
ConvertError, /<stdin>: line 3: no callout found for <1>/)
end

it "doesn't break line numbers in included files" do
input = <<~ASCIIDOC
include::resources/elastic_compat_preprocessor/missing_callout.adoc[]
ASCIIDOC
expect { convert(input) }.to raise_error(
ConvertError, /missing_callout.adoc: line 3: no callout found for <1>/)
end

it "un-blocks blocks containing only attributes" do
actual = convert <<~ASCIIDOC
:inheader: foo
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public class Example {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[P1]]
=== P1

[[P1_1]]
==== P1.1

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
words <1> <2>
--------------------------------------------------
<1> foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[P2]]
=== P2

[[P2_1]]
==== P2.1

["source","java",subs="attributes,callouts,macros"]
--------------------------------------------------
words <1> <2>
--------------------------------------------------
<1> foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
RSpec.shared_examples "doesn't break line numbers" do
it "doesn't break line numbers" do
input = <<~ASCIIDOC
---
---
<1> callout
ASCIIDOC
expect { convert(input) }.to raise_error(
ConvertError, /<stdin>: line 3: no callout found for <1>/)
end

it "doesn't break line numbers in included files" do
input = <<~ASCIIDOC
include::resources/does_not_break_line_numbers/missing_callout.adoc[]
ASCIIDOC
expect { convert(input) }.to raise_error(
ConvertError, /missing_callout.adoc: line 3: no callout found for <1>/)
end
end