This blog is starting to look like an advertisment for Atlassian, but I just have to tell you about some stuff I wrote over the weekend (with some help with the finishing touches from Aslak and Carlos, the latter being in town all the way from Brazil).
You know when you are trying to write that long anticipated documentation for your latest open-source library and you want to include some code samples in your tutorials? Only problem is, the project is coming close to a release but you’re still refactoring like crazy. So how do you keep all those code-snippets up-to-date?
Confluence to the rescue! Your open-source library is the latest and coolest cutting-edge stuff you can think of so of course you are using the latest and coolest cutting-edge tool to write the documentation in. For those who don’t know about it: Confluence is basically a wiki with some really nice features like for example an awesome look-and-feel that makes me all warm and fuzzy inside.
So, one of the features in Confluence is that you can easily extend it with new macros. This is actually a result of that they are basing their wiki rendering on Radeox the same rendering engine used by SnipSnap.
So I basically just wrote a macro that extracts code snippets from any url (hint, think viewcvs) and renders them into a Confluence page.
You basically puts this into your code (easily done with an IntelliJ surround live template):
// START SNIPPET: boy
public class Boy {
public void kiss(Object kisser) {
System.out.println("I was kissed by " + kisser);
}
}
// END SNIPPET: boy
Then you add the following to your wiki page (I’ve snipped the long viewcvs url that points to my java code in cvs, because it wrecks the formatting of this page completely):
{snippet:lang=java|id=boy|url=[long viewcvs url deliberately left out]}
Don’t get fooled by the “lang=java” thing, the only thing that does is to render the content as a code block with java syntax highlighting. This actually works for any language, xml, text, html, whatever. Thanks to Martin Fowler who explained the idea of putting the snippet demarcators into the code, which avoids any crazy ideas of having to parse the Java code and makes the same thing work for virtually any text file. Simple, elegant and powerful, almost Wardish.
Have a look at the following page, there is not one single line of java code in that page (except for that snippet that for some reason doesn’t work properly, I’ll look into that later):
http://docs.codehaus.org/display/PICO/Two+minute+tutorial
By the way, I haven’t tested this but the macro probably works just as well in SnipSnap or any other Radeox based applications. It does not depend on any features in Confluence whatsoever.
The code temporarily lives in the NanoContainer CVS:
http://cvs.codehaus.org/viewcvs.cgi/snippet/?root=nanocontainer