One of the great features that Asciidoctor offers us as a writers of manuals, tutorials and documentation in general is that allows us to follow the DRY pattern. If for example you are writing a tutorial, probably you will have one part of the tutorial which will be the documentation, and another part which will be source code with examples. Asciidoctor allows us to write the documentation and include the examples directly from source code project to document.
For example:
As you can see you can include directly from source code the example and Asciidoctor will render it inside the document. Moreover if you set the language, Asciidoctor will highlight the code too.
But as you can see there is one problem with this approach, you are including the whole file, with imports, javadoc, comments, ... and maybe you only want to show a method of that class. Now with Asciidoctor you have two options:
Using lines attribute:
Which have the problem that modifying original source code also modifies the code embedded inside tutorial.
Using tags:
Using tags:
Which is an invasive method that requires you modify the original source code:
But now we have got another option in case your language is Java. And it is using java-semantic-asciidoctorj extension. This extension allows us to embed parts of the code by its components. That is you can set which method to add, which class, if you want to add imports or not, ... The extension only works with asciidoctorj-1.5.0.preview1 and beyond, and uses the new SPI for extensions, so you only have to add the jar extension on classpath, and the extension is automatically registered.
An example of usage of this extension could be the next one. Let's imagine that we have a class with 5 methods and we want to include only a method called mymethod which receives an String as parameter and it has no return value. Also we want to add the imports, but nothing else.
We could do something like:
Note that now we are not invading our code with comment-tags, nor fixing the position with lines of the method inside our document.
The attributes you can set in include are:
Now we can use our classes within our documentation and by adding the required block.
This version of plugin works with Java 1.7 and before, not with Java 1.8 but it could work in some cases.
The extension is published on bintray, so to install you simply have to add bintray repository and the required dependency:
The project is hosted at: https://github.com/lordofthejars/asciidoctorj-extensions
We keep learning,
Alex.
The attributes you can set in include are:
- imports: adds the imports.
- fields: adds the fields of the main class.
- class: adds all the content of the main class.
- enum=<classname>: adds the enum with the given name. The enum should be defined inside the class we are adding.
- annotation=<classname>: same as enum but for annotations.
- class=<classname>: same as enum but for inner classes.
- contructor=<constructorName>(<parameterType1>,<parameterType2>, ....): adds the defined constructor. In this case we must set the constructor name and the type of each parameter. For example: MyClass(String).
- method=<returnType> <methodName>(<parameterType1>, <parameterType2>, ...): same as constructor but for methods, which implies adding the return type. Note that it is not required to add any modifier nor throws classes.
Now we can use our classes within our documentation and by adding the required block.
This version of plugin works with Java 1.7 and before, not with Java 1.8 but it could work in some cases.
The extension is published on bintray, so to install you simply have to add bintray repository and the required dependency:
The project is hosted at: https://github.com/lordofthejars/asciidoctorj-extensions
We keep learning,
Alex.
I feel shouting ya-hoo, And me still feeling hungry, Cowabunga!!, Cookie monster went and ate the new red two. Monster Went and Ate My Red 2 - Elvis Costello & ElmoMusic: https://www.youtube.com/watch?v=KxardpBReQc