Create extension to embed source code

Posted by bodiam on
URL: https://discuss.asciidoctor.org/Create-extension-to-embed-source-code-tp3645.html

Hi all,

I'm trying to write a small extension, but even after reading quite some documentation, I still have no idea what the difference is between createInline and createBlock, and I'm not sure if I'm doing things right here.

The 'problem' I have is that I have a lot of code like this:

[source, scala]
.Sample1.scala
----
include::{rootdir}/src/main/scala/samples/flatmap/Sample1.scala[tags=helloMethod]
----

That's 5 lines every time, plus there's duplication. So, I'd like to get of rid of that, and I was hoping I could do the same thing with creating a macro or extension like this:

source::flatmap/Sample1.scala[tags=helloMethod]

And be done. But I have no idea how write the code for this. I currently have the following code:

    // Here we can add the code for extensions we write.
    extensions {

        // Implementation for inline macro to replace
        // source:flatmap/Sample1[tag=helloMethod] with a link to the issue.
        inlinemacro (name: "source") { parent, target, attributes ->

            options = [
                "type": ":link",
                "target": "http://issue-server/browse/${target}".toString()
            ]

            // Create the link to the issue.
            createBlock(parent, "anchor", target, attributes, options).render()
        }
    }

but that doesn't do anything yet, it's based on:
http://blog.jdriven.com/2015/03/awesome-asciidoctor-use-inline-extension-dsl-with-gradle/
and
https://github.com/asciidoctor/asciidoctorj-groovy-dsl

Can anyone help me out, or point me in the right direction? Thanks!

Erik