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: Support added inline macro #548

Merged
merged 4 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
34 changes: 34 additions & 0 deletions resources/asciidoctor/lib/added/extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

include Asciidoctor

class Added < Extensions::Group
def activate registry
registry.block_macro AddedBlock
registry.inline_macro AddedInline
end
end

# Extension for marking when something was added.
#
# Usage
Expand All @@ -23,3 +30,30 @@ def process parent, target, attrs
create_pass_block parent, docbook, {}, subs: nil
end
end

# Extension for marking when something was added.
#
# Usage
#
# Foo added:[6.0.0-beta1]
#
class AddedInline < Extensions::InlineMacroProcessor
use_dsl
named :added
name_positional_attributes :version, :text
with_format :short

def process parent, target, attrs
if attrs[:text]
<<~DOCBOOK
<phrase revisionflag="added" revision="#{attrs[:version]}">
#{attrs[:text]}
</phrase>
DOCBOOK
else
<<~DOCBOOK
<phrase revisionflag="added" revision="#{attrs[:version]}"/>
DOCBOOK
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
# added[6.0.0-beta1]
# Into
# added::[6.0.0-beta1]
# Because `::` is required by asciidoctor but isn't by asciidoc.
# Because `::` is required by asciidoctor to invoke block macros but isn't
# required by asciidoc.
#
# Turns
# words words added[6.0.0-beta1]
# Into
# words words added:[6.0.0-beta1]
# Because `:` is required by asciidoctor to invoke inline macros but isn't
# required by asciidoc.
#
# Turns
# include-tagged::foo[tag]
Expand Down Expand Up @@ -132,7 +140,12 @@ def reader.process_line line
@code_block_start = line
end
end
line&.gsub!(/(added)\[([^\]]*)\]/, '\1::[\2]')
# First convert the "block" version of these macros. We convert them
# to block macros because they are at the start of the line....
line&.gsub!(/^(added)\[([^\]]*)\]/, '\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....
line&.gsub!(/(added)\[([^\]]*)\]/, '\1:[\2]')
end
end
reader
Expand Down
2 changes: 1 addition & 1 deletion resources/asciidoctor/lib/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require_relative 'elastic_compat_preprocessor/extension'
require_relative 'elastic_include_tagged/extension'

Extensions.register Added
Extensions.register do
# Enable storing the source locations so we can look at them. This is required
# for EditMe to get a nice location.
Expand All @@ -14,5 +15,4 @@
treeprocessor EditMe
treeprocessor ElasticCompatTreeProcessor
include_processor ElasticIncludeTagged
block_macro AddedBlock
end
56 changes: 50 additions & 6 deletions resources/asciidoctor/spec/added_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
require 'added/extension'

RSpec.describe AddedBlock do
RSpec.describe Added do
before(:each) do
Extensions.register do
block_macro AddedBlock
end
Extensions.register Added
end

after(:each) do
Extensions.unregister_all
end

it "creates a note" do
it "block version creates a note" do
actual = convert <<~ASCIIDOC
== Example
added::[some_version]
Expand All @@ -27,7 +25,7 @@
expect(actual).to eq(expected.strip)
end

it "is not invoked without the ::" do
it "block version is not invoked without the ::" do
actual = convert <<~ASCIIDOC
== Example
added[some_version]
Expand All @@ -40,4 +38,50 @@
DOCBOOK
expect(actual).to eq(expected.strip)
end

it "inline version creates a phrase" do
actual = convert <<~ASCIIDOC
== Example
words added:[some_version]
ASCIIDOC
expected = <<~DOCBOOK
<chapter id="_example">
<title>Example</title>
<simpara>words <phrase revisionflag="added" revision="some_version"/>
</simpara>
</chapter>
DOCBOOK
expect(actual).to eq(expected.strip)
end

it "inline version creates a phrase with extra text if provided" do
actual = convert <<~ASCIIDOC
== Example
words added:[some_version, more words]
ASCIIDOC
expected = <<~DOCBOOK
<chapter id="_example">
<title>Example</title>
<simpara>words <phrase revisionflag="added" revision="some_version">
more words
</phrase>
</simpara>
</chapter>
DOCBOOK
expect(actual).to eq(expected.strip)
end

it "inline version is not invoked without the ::" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the PR description... does this match the behavior of asciidoc too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woops! That should be without the : instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, in asciidoc the inline macro is invoked without the :, but in asciidoctor it isn't. We "fix" the input to add the : in the compat preprocessor. This is a "doesn't it work how I think it should" test more than anything.

actual = convert <<~ASCIIDOC
== Example
words added[some_version]
ASCIIDOC
expected = <<~DOCBOOK
<chapter id="_example">
<title>Example</title>
<simpara>words added[some_version]</simpara>
</chapter>
DOCBOOK
expect(actual).to eq(expected.strip)
end
end
19 changes: 17 additions & 2 deletions resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

RSpec.describe ElasticCompatPreprocessor do
before(:each) do
Extensions.register Added
Extensions.register do
preprocessor ElasticCompatPreprocessor
include_processor ElasticIncludeTagged
block_macro AddedBlock
end
end

Expand All @@ -18,7 +18,7 @@

include_examples "doesn't break line numbers"

it "invokes added[version]" do
it "invokes the added block macro when added[version] starts a line" do
actual = convert <<~ASCIIDOC
== Example
added[some_version]
Expand All @@ -34,6 +34,21 @@
expect(actual).to eq(expected.strip)
end

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

it "invokes include-tagged::" do
actual = convert <<~ASCIIDOC
== Example
Expand Down