Code block justification

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Code block justification

marc0der
Hi all!

I'm currently writing something that has a lot of code blocks where I reference sections of code using tags.
My problem occurs when I reference blocks of indented code. An example would be something like:

object MyObject {
    class MyClass {
        //tag::init[]
        fun myMethod(x: Int): Int = ....
        //end::init[]
    }
}

This would result the tagged code being indented by 8 characters:

        fun myMethod(x: Int): Int = ....

When defining code blocks, each with a different indentation level it feels odd in the final output.
I've looked in the documentation but can't find anything that addresses this specific issue. It would
be great if I could define a tag as follows:

        //tag::init[index=-8]
        fun myMethod(x: Int): Int = ....
        //end::init[]

that would in turn result in everything shifted left by 8 character columns:

fun myMethod(x: Int): Int = ....

Am I just being stupid and overlooking something? is there something I should be doing to achieve this effect?
Thanks for such a fantastic project and hope I can get this working!
Cheers,
Marco.
Reply | Threaded
Open this post in threaded view
|

Re: Code block justification

Alexander Schwartz
Hi Marco,

there is the indent option for the source block that allows you to normalize the indent. You can even specify the tab size. https://asciidoctor.org/docs/user-manual/#normalize-block-indentation

[source,ruby,indent=0]
----
include::file.rb[tags=init]
----

I assume this is what you have been looking for.

Cheers, Alexander

Alexander Schwartz (alexander.schwartz@gmx.net)
https://www.ahus1.de
Reply | Threaded
Open this post in threaded view
|

Re: Code block justification

marc0der
H Alexander,

Thanks for pointing me to the indent attribute, this is exactly what I needed.
I did experience one quirk though, so I thought I'd share it here for anybody else who might get stuck with this issue:
I set my source at a global level, so setting my indent attribute at the include level  as you have didn't work for me.
I ended up having to pass the indent attribute into the include directive. So, using your example it would be:

.Some title
----
include::file.rb[tags=init,indent=0]
----

This ended up working perfectly for me.
Thanks!
Marco.