Awesome Asciidoc: Include Only Certain Lines from Included Files
In a previous post we learned how to include partial content from included files. We needed to enclose the content we want to include between start and end tags and reference those tags in our documentation markup. But Andres Almiray already mentioned in a tweet we can use line numbers as well:
@marcovermeulen @rfletcherew @mrhaki you can also use -1 in the include range, like this https://raw.githubusercontent.com/griffon/griffon/master/docs/griffon-guide/src/asciidoc/appendix-sample-applications.adoc
— Andres Almiray (@aalmiray) June 6, 2014
Let's see this in action in our Asciidoc markup. We can use the include
macro and specify the lines we want to include with the lines
attribute. We can include multiple lines by specifying a range (1..10
), or separate different line sections with a ;
. To indicate we want to include lines from a starting line up until the end of the file we can use -1
to indicate the end of the file.
[source,groovy]
----
include::{sourcedir}/Sample.groovy[lines=1;7..-1]
----
Here is the source of the file we want to include:
package com.mrhaki.groovy
/**
* Sample Groovy class with
* two properties.
*/
@groovy.transform.ToString
class User {
String name
String email
}
And when we generate an HTML version of our markup we see that only part of the original source file is included in the output:
Written with Asciidoctor 0.1.4.