Partial HTML generation from textile using eclipse Mylyn standalone

The title is pretty much explicit. For SlideshowFX, I needed to generate HTML content from textile, in order to define slide’s content. Looking for a lib, I found eclipse Mylyn that can be used in a standalone way. The library is pretty powerful but I have to look a lot at the sources in order to get the things done how I wanted to (#LukeAtTheSource power). Generally to convert a markup string into a HTML one, you create a MarkupLanguage as well as a MarkupParser and do the following:

final MarkupLanguage language = new TextileLanguage();
final MarkupParser parser = new MarkupParser(language);
parser.parseToHTML("h1. My little String");

The thing is that this code will create a whole HTML document (with html, head, body and so on tags). Me needs were to just get the HTML code corresponding to my string, e.g.

<h1>My little String</h1>

So the code should be changed to use a DocumentBuilder, like the following one:

final StringWriter writer = new StringWriter();
final DocumentBuilder builder = new HtmlDocumentBuilder(writer);
final MarkupLanguage language = new TextileLanguage();
final MarkupParser parser = new MarkupParser(language, builder);

// false indicates to not produce a whole HTML document
parser.parse("h1. My little String", false);

writer.flush();
writer.close();

String htmlContent = writer.toString();

This is almost done except that Mylyn generates IDs (which is kind of normal right?) by using the content of the markup. In short, I wanted to avoid IDs’ generation but I could’t find a right and efficient way to do it. So I decided to ensure uniqueness of IDs by always getting the current timestamp. Maybe it’s not a wonderful solution, but for the purpose of this little tutorial it will be perfect. The main idea is to change the ID generation and for doing this, you have to override some classes, because the default ID generation strategy is stored as a static and final variable, and no setters are available. The following example demonstrates how to do it:

final StringWriter writer = new StringWriter();

// The generation strategy generates IDs using the current timestamp
final IdGenerationStrategy idGenerationStrategy = new IdGenerationStrategy() {
  @Override
  public String generateId(String s) {
    return System.currentTimeMillis() + "";
  }
};

final IdGenerator idGenerator = new IdGenerator();
idGenerator.setGenerationStrategy(idGenerationStrategy);

final TextileContentState contentState = new TextileContentState() {
  @Override
  public IdGenerator getIdGenerator() {
    return idGenerator;
  }
};

// Override the language to return the created contentState used for the ID generation
final MarkupLanguage language = new TextileLanguage() {
  @Override
  protected ContentState createState() {
    return contentState;
  }
};

final DocumentBuilder builder = new HtmlDocumentBuilder(writer);

final MarkupParser parser = new MarkupParser(language, builder);

parser.parse(markupString, false);

writer.flush();
writer.close();

final String htmlContent = writer.toString();

This is it. Enjoy.

Deal with workspaces and shortcuts in Eclipse

A brand new post here : http://thierry.wasylczenko.free.fr/?p=90

Enjoy 🙂

Eclipse Galileo

Hello everyone,

The latest version of Eclipse is out 🙂 Codename : Galileo. So be sure to grab it on : http://www.eclipse.org/
If You’re on Mac OS X : Leopard, You will prefer the Cocoa version of the IDE 🙂

Then, let me give You a little tip : if You’re on Mac OS X, and even if You’ve setted up the JDK 1.6 by default, Eclipse won’t recognize it, and will still use the JDK 1.5. So to change that, go to the Preferences of Eclipse and then to Java, Installed JREs. You should be there :

Image 1

So click on the Add button. In the new window, select MacOS X VM as JRE type. Click next and now enter something like this in the JRE Home text field : /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home
Right, You’ve done the most tough part of it 🙂
Enter an explicit name like JRE 1.6 for example and hit the Finish button. Now You just have to select the new JRE in the list.
Now under the Installed JREs node, select Your execution environment as shown bellow :

Image 2

Well, it will soon be ok 🙂 Just setup the compiler now. Go under Java, Compiler and select 1.6 in the list, as shown bellow :

Image 3

That’s all 🙂
Enjoy.