From 044e33ad982bd7ccc9b1f4e52eb15c02c47b88eb Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Fri, 18 Jan 2019 12:00:31 -0500 Subject: [PATCH] Asciidoctor: Support added inline macro (#548) 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. --- resources/asciidoctor/lib/added/extension.rb | 34 +++++++++++ .../elastic_compat_preprocessor/extension.rb | 17 +++++- resources/asciidoctor/lib/extensions.rb | 2 +- resources/asciidoctor/spec/added_spec.rb | 56 +++++++++++++++++-- .../spec/elastic_compat_preprocessor_spec.rb | 19 ++++++- 5 files changed, 117 insertions(+), 11 deletions(-) diff --git a/resources/asciidoctor/lib/added/extension.rb b/resources/asciidoctor/lib/added/extension.rb index d0fdec501c9dd..2f890a2505306 100644 --- a/resources/asciidoctor/lib/added/extension.rb +++ b/resources/asciidoctor/lib/added/extension.rb @@ -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 @@ -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 + + #{attrs[:text]} + + DOCBOOK + else + <<~DOCBOOK + + DOCBOOK + end + end +end diff --git a/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb b/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb index 59419916b586d..3b454402aeb97 100644 --- a/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb +++ b/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb @@ -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] @@ -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 diff --git a/resources/asciidoctor/lib/extensions.rb b/resources/asciidoctor/lib/extensions.rb index b58b665be1ab7..ca85b05a4fe5b 100644 --- a/resources/asciidoctor/lib/extensions.rb +++ b/resources/asciidoctor/lib/extensions.rb @@ -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. @@ -14,5 +15,4 @@ treeprocessor EditMe treeprocessor ElasticCompatTreeProcessor include_processor ElasticIncludeTagged - block_macro AddedBlock end diff --git a/resources/asciidoctor/spec/added_spec.rb b/resources/asciidoctor/spec/added_spec.rb index c2d47b1deae59..9a642820d7bcc 100644 --- a/resources/asciidoctor/spec/added_spec.rb +++ b/resources/asciidoctor/spec/added_spec.rb @@ -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] @@ -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] @@ -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 + + Example + words + + + 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 + + Example + words + more words + + + + 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 + + Example + words added[some_version] + + DOCBOOK + expect(actual).to eq(expected.strip) + end end diff --git a/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb b/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb index a901749680684..8581eee006d78 100644 --- a/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb +++ b/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb @@ -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 @@ -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] @@ -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 + + Example + words + + + DOCBOOK + expect(actual).to eq(expected.strip) + end + it "invokes include-tagged::" do actual = convert <<~ASCIIDOC == Example