Using Textile with Grails

During the process of writing my first Grails application, Groovyquiz.com (coming soon), I needed a clean way to write the quiz text, and was generally not interested in writing everything as HTML.

I have always liked Textile, and quickly found a couple of options for using that format via two Java libraries: JTextile and textile4j. Now, I don’t know about you, but when I need a Java library of any nature, I tend to go with one that has most and preferably all of the following:

  • Jar file
  • JavaDocs
  • Example code
  • README file
  • Tests

When I don’t see those, I generally disregard the project and try to find something else. That was the case with JTextile… the download included 3 files: one java source file (JTextile.java), one text file for testing, and another java source file for running the test (Test.java). Where as textile4j had all of the features I listed above.

So, after deciding that I would use textile4j to Textilize my app, I followed these steps to get everything set up.

1. Copy textile4j jar to my

<grails-root>/lib

folder.

2. Created a

TextileCodec.groovy

file in

<grails-root>/grails-app/utils

folder with the following contents:

  1. import net.sf.textile4j.Textile
  2. class TextileCodec {
  3.   static encode = { str ->
  4.         return new Textile().process(str)
  5.   }
  6. }

3. Used it in my code! I needed it for a view:

  1. ${quiz.problem.encodeAsTextile()}

Simple as that!