A Groovy Notebook
From time to time I stumble upon a web application which lets you write notebooks like the ones you know from Mathematica. They let you mix formatted text which explains your code with code snippets. The output of those snippets is displayed and the code builds up snippet by snippet.
This time it was Jypiter, formerly known as IPython Notebook. It evolved from a notebook just for Python to a cool solution suitable for all kind of languages. Unfortunately, Groovy isn’t yet in the list of supported languages.
Since Python is not my mother tongue and I couldn’t find good documentation on how to support an additional language, I thought about other ways to create a notebook for Groovy.
Inspired by the talk “Make your Asciidoctor Groovy” by Stephan Classen (which I unfortunately haven’t attended), I decided to create an asciidoctorj plugin in Groovy which lets you execute Groovy code inline.
There are already great examples of plugins written in Groovy, so I had a good starting point:
- Stephan Classen: asciidoctorj-screenshot
- mrhaki: Awesome Asciidoctor: Write Extensions Using Groovy (or Java)
- Andreas Almiray: Gradle Glam: custom asciidoctor extensions
So I took one of them as starting point and came up with my own project. The core of the extension is shown here:
It is activated whenever asciidoctor hits a
[groovy] block. It then takes the content of the block, executes it and renders the original script together with the output. Infortunately, I couldn’t find a way to return two different blocks in my extension - one which renders the source with highlighting and one which renders the pre-formatted output - so I had to do an ugly workaround which directly renders HTML. Do the simplest think which works :-)