Skip to content

Commit

Permalink
Asciidoctor: Support added inline macro (#548)
Browse files Browse the repository at this point in the history
Adds support for the added inline macro which would normally be
specified like this:

```
words added:[version]
```

But for compatibility with Elastic's asciidoc customizations we support
specifying it like this as well:

```
words added[version]
```

Note that we'll want to build on this to add support for `coming` and
`deprecated` as well.
  • Loading branch information
nik9000 authored Jan 18, 2019
1 parent 7d8b6d1 commit 044e33a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 11 deletions.
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
17 changes: 15 additions & 2 deletions resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb
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
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

0 comments on commit 044e33a

Please sign in to comment.