Skip to content
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

Asciidoctor: Change invocation rules for macros #799

Merged
merged 2 commits into from
Apr 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class ElasticCompatPreprocessor < Asciidoctor::Extensions::Preprocessor
CODE_BLOCK_RX = /^-----*$/
SNIPPET_RX = %r{^//\s*(AUTOSENSE|KIBANA|CONSOLE|SENSE:[^\n<]+)$}
LEGACY_MACROS = 'added|beta|coming|deprecated|experimental'
LEGACY_BLOCK_MACRO_RX = /^(#{LEGACY_MACROS})\[([^\]]*)\]/
LEGACY_INLINE_MACRO_RX = /(#{LEGACY_MACROS})\[([^\]]*)\]/
LEGACY_BLOCK_MACRO_RX = /^\s*(#{LEGACY_MACROS})\[(.*)\]\s*$/
LEGACY_INLINE_MACRO_RX = /(#{LEGACY_MACROS})\[(.*)\]/

def process(_document, reader)
reader.instance_variable_set :@in_attribute_only_block, false
Expand Down Expand Up @@ -175,7 +175,7 @@ def reader.process_line(line)
end

# First convert the "block" version of these macros. We convert them
# to block macros because they are at the start of the line....
# to block macros because they are alone on a line
line&.gsub!(LEGACY_BLOCK_MACRO_RX, '\1::[\2]')
# Then convert the "inline" version of these macros. We convert them
# to inline macros because they are *not* at the start of the line....
Expand Down
143 changes: 94 additions & 49 deletions resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,57 +30,102 @@

include_examples "doesn't break line numbers"

[
%w[added added note],
%w[coming changed note],
%w[deprecated deleted warning],
].each do |(name, revisionflag, tag)|
it "invokes the #{name} block macro when #{name}[version] starts a line" do
actual = convert <<~ASCIIDOC
== Example
#{name}[some_version]
ASCIIDOC
expected = <<~DOCBOOK
<chapter id="_example">
<title>Example</title>
<#{tag} revisionflag="#{revisionflag}" revision="some_version">
<simpara></simpara>
</#{tag}>
</chapter>
DOCBOOK
expect(actual).to eq(expected.strip)
end

it "invokes the #{name} inline macro when #{name}[version] is otherwise on the line" do
actual = convert <<~ASCIIDOC
== Example
words #{name}[some_version]
ASCIIDOC
expected = <<~DOCBOOK
<chapter id="_example">
<title>Example</title>
<simpara>words <phrase revisionflag="#{revisionflag}" revision="some_version"/>
</simpara>
</chapter>
DOCBOOK
expect(actual).to eq(expected.strip)
end

it "doesn't mind skipped #{name} block macros" do
actual = convert <<~ASCIIDOC
== Example
context 'change admonitions' do
shared_examples 'change admonition' do
include_context 'convert without logs'

shared_examples 'invokes the block macro' do
let(:expected) do
<<~DOCBOOK
<#{tag} revisionflag="#{revisionflag}" revision="some_version">
<simpara></simpara>
</#{tag}>
DOCBOOK
end
it 'invokes the block macro' do
expect(converted).to include(expected)
end
end
context 'when the admonition is alone on a line' do
let(:input) { "#{name}[some_version]" }
include_examples 'invokes the block macro'
end
context 'when the admonition has spaces before it' do
let(:input) { " #{name}[some_version]" }
include_examples 'invokes the block macro'
end
context 'when the admonition has spaces after it' do
let(:input) { "#{name}[some_version] " }
include_examples 'invokes the block macro'
end
context 'when the admonition has a `]` in it' do
let(:input) { "#{name}[some_version, link:link.html[Title]]" }
include_examples 'invokes the block macro'
let(:expected) do
<<~DOCBOOK
<#{tag} revisionflag="#{revisionflag}" revision="some_version">
<simpara><ulink url="link.html">Title</ulink></simpara>
</#{tag}>
DOCBOOK
end
end

ifeval::["true" == "false"]
#{name}[some_version]
#endif::[]
ASCIIDOC
expected = <<~DOCBOOK
<chapter id="_example">
<title>Example</title>
shared_examples 'invokes the inline macro' do
it 'invokes the inline macro' do
expect(converted).to include(
%(<phrase revisionflag="#{revisionflag}" revision="some_version"/>)
)
end
end
context "when the admonition is surrounded by other text" do
let(:input) { "words #{name}[some_version] words" }
include_examples 'invokes the inline macro'
end
context "when the admonition has text before it" do
let(:input) { "words #{name}[some_version]" }
include_examples 'invokes the inline macro'
end
context "when the admonition has text after it" do
let(:input) { "#{name}[some_version] words" }
include_examples 'invokes the inline macro'
end

</chapter>
DOCBOOK
expect(actual).to eq(expected.strip)
context 'when the admonition is skipped' do
let(:input) do
<<~ASCIIDOC
words before skip
ifeval::["true" == "false"]
#{name}[some_version]
endif::[]
words after skip
ASCIIDOC
end
it 'skips the admonition' do
expect(converted).not_to include('revisionflag')
end
it 'properly converts the rest of the text' do
expect(converted).to include('words before skip')
expect(converted).to include('words after skip')
end
end
end
context 'for added' do
include_context 'change admonition'
let(:name) { 'added' }
let(:revisionflag) { 'added' }
let(:tag) { 'note' }
end
context 'for coming' do
include_context 'change admonition'
let(:name) { 'coming' }
let(:revisionflag) { 'changed' }
let(:tag) { 'note' }
end
context 'for added' do
include_context 'change admonition'
let(:name) { 'deprecated' }
let(:revisionflag) { 'deleted' }
let(:tag) { 'warning' }
end
end

Expand Down