Skip to content

Instantly share code, notes, and snippets.

@fidothe
Last active April 6, 2022 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fidothe/4f88134e5d4d8097fab2397691695d6f to your computer and use it in GitHub Desktop.
Save fidothe/4f88134e5d4d8097fab2397691695d6f to your computer and use it in GitHub Desktop.
Trying to generate a syntax-highlighted listing block from a macro (using AsciiDoctor)

Example

Here’s the macro in use:

rml::blah[]

And here’s the equivalent in-place listing block:

require 'eg'

puts 'blah'
class MyListingBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
use_dsl
named :rml
def process(parent, target, attrs)
listing = create_listing_block(parent, "require 'eg'\n\nputs '#{target}'\n", attrs)
listing.style = 'source'
listing.set_attr 'language', 'ruby'
listing.commit_subs
$stderr.puts listing.content
listing
end
end
Asciidoctor::Extensions.register do
block_macro MyListingBlockMacro
end
#!/bin/sh
asciidoctor -r ./listing-block.rb eg.adoc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment