Skip to content

Add anchor with hash of example #1717

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

Merged
merged 1 commit into from
Jan 30, 2020
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
1 change: 1 addition & 0 deletions integtest/spec/helper/console_alternative_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module ConsoleExamples
)
);</pre>
</div>
<a id="8a7e0a79b1743d5fd94d79a7106ee930"></a>
<div class="pre_wrapper lang-console default #{has_classes}">
HTML
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require_relative '../delegating_converter'

module AlternativeLanguageLookup
##
# A Converter that adds an anchor above each listing with its digest.
class Converter < DelegatingConverter
def convert_listing(node)
return yield unless (digest = node.attr 'digest')

%(<a id="#{digest}"></a>\n) + yield
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
require 'digest/murmurhash'
require_relative '../log_util'
require_relative '../scaffold'
require_relative 'converter'
require_relative 'listing'
require_relative 'lookup'
require_relative 'report'
require_relative 'summary'

##
# Extension to add "alternative" languages to language examples.
module AlternativeLanguageLookup
def self.activate(registry)
registry.treeprocessor AlternativeLanguageLookup
DelegatingConverter.setup(registry.document) { |d| Converter.new d }
end

##
# TreeProcessor extension find alternative languages for snippets.
class AlternativeLanguageLookup < TreeProcessorScaffold
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def initialize(block)
def process
return unless alternatives

block.attributes['digest'] = digest
found_langs = []

alternatives.each do |lookup|
Expand Down
4 changes: 2 additions & 2 deletions resources/asciidoctor/lib/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
preprocessor CrampedInclude
preprocessor ElasticCompatPreprocessor
treeprocessor ElasticCompatTreeProcessor
# The tree processors after this must come after ElasticComptTreeProcessor
# Everything after this must come after ElasticCompatTreeProcessor
# or they won't see the right tree.
treeprocessor AlternativeLanguageLookup::AlternativeLanguageLookup
include_processor ElasticIncludeTagged
end
Asciidoctor::Extensions.register AlternativeLanguageLookup
20 changes: 16 additions & 4 deletions resources/asciidoctor/spec/alternative_language_lookup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

RSpec.describe AlternativeLanguageLookup::AlternativeLanguageLookup do
before(:each) do
Asciidoctor::Extensions.register do
treeprocessor AlternativeLanguageLookup::AlternativeLanguageLookup
end
Asciidoctor::Extensions.register AlternativeLanguageLookup
end

after(:each) do
Expand Down Expand Up @@ -125,7 +123,20 @@
include_context 'convert without logs'
let(:input) { one_snippet }
let(:snippet_contents) { 'GET /no_alternatives' }
include_examples "doesn't modify the output"
it 'only adds the digest' do
expect(converted).to eq <<~HTML.strip
<div id="preamble">
<div class="sectionbody">
<a id="3fcdfa6097c68c04f3e175dcf3934af6"></a>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-console" data-lang="console">#{snippet_contents}</code></pre>
</div>
</div>
</div>
</div>
HTML
end
context 'the alternatives report' do
it 'contains an entry for the snippet' do
expect(report).to eq(
Expand Down Expand Up @@ -413,6 +424,7 @@
<pre class="highlight"><code class="language-csharp" data-lang="csharp">Console.WriteLine("there are callouts"); <b class="conum">(1)</b> <b class="conum">(2)</b></code></pre>
</div>
</div>
<a id="9e01493a85c06a5100ff712f6b3eead4"></a>
<div class="listingblock default has-csharp">
<div class="content">
<pre class="highlight"><code class="language-console" data-lang="console">GET /there_are_callouts <b class="conum">(1)</b> <b class="conum">(2)</b></code></pre>
Expand Down