From 8383d957a33f6d40b2fbda8ee28e381068cf679c Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Tue, 9 Apr 2019 10:22:46 -0400 Subject: [PATCH 1/2] Asciidoctor: Change invocation rules for macros Changes the invocation rules for legacy style admonition macros to only invoke the block version if it is alone on a line. This more closely mirrors the customizations that Elastic did to AsciiDoc. --- .../elastic_compat_preprocessor/extension.rb | 4 +- .../spec/elastic_compat_preprocessor_spec.rb | 132 +++++++++++------- 2 files changed, 85 insertions(+), 51 deletions(-) diff --git a/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb b/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb index 779b3dc489aeb..74020c0598a4e 100644 --- a/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb +++ b/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb @@ -115,7 +115,7 @@ 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_BLOCK_MACRO_RX = /^\s*(#{LEGACY_MACROS})\[([^\]]*)\]\s*$/ LEGACY_INLINE_MACRO_RX = /(#{LEGACY_MACROS})\[([^\]]*)\]/ def process(_document, reader) @@ -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.... diff --git a/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb b/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb index 0558315a094d9..617dd16ef2136 100644 --- a/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb +++ b/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb @@ -30,57 +30,91 @@ 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 - - Example - <#{tag} revisionflag="#{revisionflag}" revision="some_version"> - - - - 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 - - Example - words - - - 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"> + + + 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 - ifeval::["true" == "false"] - #{name}[some_version] - #endif::[] - ASCIIDOC - expected = <<~DOCBOOK - - Example + shared_examples 'invokes the inline macro' do + it 'invokes the inline macro' do + expect(converted).to include( + %() + ) + 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 - - 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 From d9485a854f00a9968c71378aeb18cbbbfc0b4039 Mon Sep 17 00:00:00 2001 From: Nik Everett Date: Wed, 10 Apr 2019 10:27:05 -0400 Subject: [PATCH 2/2] Hmmm --- .../lib/elastic_compat_preprocessor/extension.rb | 4 ++-- .../spec/elastic_compat_preprocessor_spec.rb | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb b/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb index 74020c0598a4e..9311c5ad96e33 100644 --- a/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb +++ b/resources/asciidoctor/lib/elastic_compat_preprocessor/extension.rb @@ -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 = /^\s*(#{LEGACY_MACROS})\[([^\]]*)\]\s*$/ - 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 diff --git a/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb b/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb index 617dd16ef2136..2755f8df0a16d 100644 --- a/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb +++ b/resources/asciidoctor/spec/elastic_compat_preprocessor_spec.rb @@ -58,6 +58,17 @@ 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"> + Title + + DOCBOOK + end + end shared_examples 'invokes the inline macro' do it 'invokes the inline macro' do