Skip to content

Commit

Permalink
move the examples into separate files so they can be tested
Browse files Browse the repository at this point in the history
  • Loading branch information
djencks committed Jan 9, 2021
1 parent e6b5ad3 commit 84c20d8
Show file tree
Hide file tree
Showing 43 changed files with 1,503 additions and 314 deletions.
@@ -0,0 +1,12 @@
<details>
<summary class="title">Show JSON</summary>
<div class="content">
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-json" data-lang="json">{
"foo": "bar"
}</code></pre>
</div>
</div>
</div>
</details>
@@ -0,0 +1,13 @@
#!/bin/sh

#tag::runner[]
echo \
'.Show JSON
[collapsible,json]
----
{
"foo": "bar"
}
----' \
| asciidoctor -s -r ./block-collapsible-extension.rb -
#end::runner[]
29 changes: 29 additions & 0 deletions docs/modules/extensions/examples/block-collapsible-extension.rb
@@ -0,0 +1,29 @@
require 'asciidoctor'
require 'asciidoctor/extensions'

class CollapsibleBlock < Asciidoctor::Extensions::BlockProcessor
enable_dsl

named :collapsible
contexts :listing
positional_attributes 'language'

def process parent, reader, attrs
lang = attrs.delete 'language'
attrs['title'] ||= 'Show Listing'
example = create_example_block parent, [], attrs, content_model: :compound
example.set_option 'collapsible'
listing = create_listing_block example, reader.readlines, nil
if lang
listing.style = 'source'
listing.set_attr 'language', lang
listing.commit_subs
end
example << listing
example
end
end

Asciidoctor::Extensions.register do
block CollapsibleBlock
end
@@ -0,0 +1,6 @@
<div class="openblock gist">
<div class="title">My Gist</div>
<div class="content">
<script src="https://gist.github.com/123456.js"></script>
</div>
</div>
@@ -0,0 +1,8 @@
#!/bin/sh

#tag::runner[]
echo \
'.My Gist
gist::123456[]' \
| asciidoctor -s -r ./block-macro-gist-extension.rb -
#end::runner[]
26 changes: 26 additions & 0 deletions docs/modules/extensions/examples/block-macro-gist-extension.rb
@@ -0,0 +1,26 @@
require 'asciidoctor'
require 'asciidoctor/extensions'

class GistBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
enable_dsl

named :gist

def process parent, target, attrs
title_html = (attrs.has_key? 'title') ?
%(<div class="title">#{attrs['title']}</div>\n) : nil

html = %(<div class="openblock gist">
#{title_html}<div class="content">
<script src="https://gist.github.com/#{target}.js"></script>
</div>
</div>)

create_pass_block parent, html, attrs, subs: nil
end
end

# Self-registering
Asciidoctor::Extensions.register do
block_macro GistBlockMacro if document.basebackend? 'html'
end
@@ -0,0 +1,6 @@
<div class="paragraph">
<p>THE TIME IS NOW! GET A MOVE ON!</p>
</div>
<div class="paragraph">
<p>I MEAN IT!!!!</p>
</div>
12 changes: 12 additions & 0 deletions docs/modules/extensions/examples/block-shout-extension-runner.sh
@@ -0,0 +1,12 @@
#!/bin/sh

#tag::runner[]
echo \
'[shout]
The time is now. Get a move on.
[shout,4]
I mean it.
' \
| asciidoctor -s -r ./block-shout-extension.rb -
#end::runner[]
23 changes: 23 additions & 0 deletions docs/modules/extensions/examples/block-shout-extension.rb
@@ -0,0 +1,23 @@
require 'asciidoctor'
require 'asciidoctor/extensions'

class ShoutBlock < Asciidoctor::Extensions::BlockProcessor
PeriodRx = /\.(?= |$)/

enable_dsl

named :shout
contexts :paragraph
positional_attributes 'vol'
content_model :simple

def process parent, reader, attrs
volume = ((attrs.delete 'vol') || 1).to_i
create_paragraph parent, (reader.lines.map {|l| l.upcase.gsub PeriodRx, '!' * volume }), attrs
end
end

# self-registering
Asciidoctor::Extensions.register do
block ShoutBlock
end

Large diffs are not rendered by default.

@@ -0,0 +1,11 @@
#!/bin/sh

#tag::runner[]
echo \
'= Silly Page
Who would look at this content?
' \
| asciidoctor -r ./docinfo-google-analytics-extension.rb -a google-analytics-account=UA-ABCXYZ123 -
#end::runner[]
@@ -0,0 +1,23 @@
require 'asciidoctor'
require 'asciidoctor/extensions'

class GoogleAnalyticsDocinfoProcessor < Asciidoctor::Extensions::DocinfoProcessor
enable_dsl
at_location :footer
def process document
return unless (ga_account_id = document.attr 'google-analytics-account')
%(<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create','#{ga_account_id}','auto');
ga('send','pageview');
</script>)
end
end

# Self-registering
Asciidoctor::Extensions.register do
docinfo_processor GoogleAnalyticsDocinfoProcessor
end
21 changes: 20 additions & 1 deletion docs/modules/extensions/examples/extensions-test.sh
Expand Up @@ -6,5 +6,24 @@

./nestable-extension-object-runner.sh | diff --brief - nestable-extension-object-out.html

./rfc-link-extension-inline-runner.sh | diff --brief - rfc-link-extension-inline-out.html
./block-collapsible-extension-runner.sh | diff --brief - block-collapsible-extension-out.html

./block-macro-gist-extension-runner.sh | diff --brief - block-macro-gist-extension-out.html

./block-shout-extension-runner.sh | diff --brief - block-shout-extension-out.html

#output not reproducible
./docinfo-google-analytics-extension-runner.sh >/dev/null

./include-uri-extension-runner.sh | diff --brief - include-uri-extension-out.html

./inline-man-extension-runner.sh | diff --brief - inline-man-extension-out.html

./inline-rfc-link-extension-runner.sh | diff --brief - inline-rfc-link-extension-out.html

#output not reproducible due to date
./post-footer-extension-runner.sh >/dev/null # | diff - post-footer-extension-out.html

./pre-front-matter-extension-runner.sh | diff --brief - pre-front-matter-extension-out.html

./tree-shell-session-extension-runner.sh | diff --brief - tree-shell-session-extension-out.html
35 changes: 35 additions & 0 deletions docs/modules/extensions/examples/include-uri-extension-out.html
@@ -0,0 +1,35 @@
<div class="listingblock">
<div class="title">Gemfile</div>
<div class="content">
<pre class="highlight"><code class="language-ruby" data-lang="ruby">source 'https://rubygems.org'

# Look in asciidoctor.gemspec for runtime and development dependencies
gemspec

group :development do
gem 'asciimath', ENV['ASCIIMATH_VERSION'] if ENV.key? 'ASCIIMATH_VERSION'
gem 'pygments.rb', ENV['PYGMENTS_VERSION'] if ENV.key? 'PYGMENTS_VERSION'
gem 'rouge', ENV['ROUGE_VERSION'] if ENV.key? 'ROUGE_VERSION'
gem 'haml', '~&gt; 4.0' if RUBY_ENGINE == 'truffleruby'
end

group :docs do
gem 'yard'
gem 'yard-tomdoc'
end

# enable this group to use Guard for continuous testing
# after removing comments, run `bundle install` then `guard`
#group :guardtest do
# gem 'guard'
# gem 'guard-test'
# gem 'libnotify'
# gem 'listen', :github =&gt; 'guard/listen'
#end

group :ci do
gem 'simplecov', '~&gt; 0.16.0'
gem 'json', '~&gt; 2.2.0' if RUBY_ENGINE == 'truffleruby'
end</code></pre>
</div>
</div>
13 changes: 13 additions & 0 deletions docs/modules/extensions/examples/include-uri-extension-runner.sh
@@ -0,0 +1,13 @@
#!/bin/sh

#tag::runner[]
echo \
'
.Gemfile
[source,ruby]
----
include::https://raw.githubusercontent.com/asciidoctor/asciidoctor/master/Gemfile[]
----
' \
| asciidoctor -s -r ./include-uri-extension.rb -
#end::runner[]
20 changes: 20 additions & 0 deletions docs/modules/extensions/examples/include-uri-extension.rb
@@ -0,0 +1,20 @@
require 'asciidoctor'
require 'asciidoctor/extensions'
require 'open-uri'

class UriIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
def handles? target
(target.start_with? 'http://') or (target.start_with? 'https://')
end

def process doc, reader, target, attributes
content = (open target).readlines
reader.push_include content, target, target, 1, attributes
reader
end
end

# Self-registering
Asciidoctor::Extensions.register do
include_processor UriIncludeProcessor
end
@@ -0,0 +1,3 @@
<div class="paragraph">
<p>See <a href="gittutorial.html(7)">gittutorial</a> to get started.</p>
</div>
@@ -0,0 +1,8 @@
#!/bin/sh

#tag::runner[]
echo \
'See man:gittutorial[7] to get started.
' \
| asciidoctor -s -r ./inline-man-extension.rb -
#end::runner[]
27 changes: 27 additions & 0 deletions docs/modules/extensions/examples/inline-man-extension.rb
@@ -0,0 +1,27 @@
require 'asciidoctor'
require 'asciidoctor/extensions'

class ManInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
enable_dsl

named :man
name_positional_attributes 'volnum'

def process parent, target, attrs
text = manname = target
suffix = ''
target = %(#{manname}.html)
suffix = if (volnum = attrs['volnum'])
"(#{volnum})"
else
nil
end
parent.document.register :links, target
create_anchor parent, text, type: :link, target: %(#{target}#{suffix})
end
end

# Self-registering
Asciidoctor::Extensions.register do
inline_macro ManInlineMacro
end
Expand Up @@ -6,5 +6,5 @@ echo \
Check out for example RFC 1882 at Christmas, RFC1927 when I forget the space.
Not to mention RFC 2549, a personal favorite.' \
| asciidoctor -s -r ./rfc-link-extension-inline.rb -
| asciidoctor -s -r ./inline-rfc-link-extension.rb -
#end::runner[]

0 comments on commit 84c20d8

Please sign in to comment.