Tuesday, August 07, 2007

Maintain Eclipse's color syntax highlighting when posting to a blog

One problem I had with posting code onto blogger is that I would like to maintain the color syntax of the code in my Eclipse editor. I want to convert following black and white version of my code :



<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>My Sample Struts Web Application </display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>


to the colorized version as shown below:



<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>My Sample Struts Web Application </display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>

I finally hit upon the solution by copying my code section to Microsoft Word and save it as XML format. Then I used the following xslt code to add color to my code syntax.


<?xml version="1.0" encoding="UTF-8" ?>
<html xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xsl:version="2.0">
<head>
<title>Color Code example</title>
</head>
<body>
<p>
<pre>
<xsl:for-each select="/*/*/*/w:p"><br/>
<xsl:for-each select="./w:r">
<span style="color:#{./w:rPr/w:color/@w:val};"><xsl:value-of select="w:t" /></font>
</xsl:for-each>
</xsl:for-each>

</pre>
</p>
</body>
</html>

I would then use Saxon to perform the transformation. It's as simple as running the following command:

java -Xmx512m -jar c:\tools\saxon\saxon8.jar -t code.xml wordxml2html.xslt > code.html

There's probably a solution out there to do this...but I haven't found it yet. For now, this is the best way that I've figured how to maintain code syntax color highlighting from Eclipse editor. There's still a lot manual steps along the way, but this will do until I find a better solution.

2 comments:

aaron said...

In the past I have used jEdit and the HTML plugin to convert code to colored HTML.

electronic signatures said...

Excellent! Thanks for this - I've been looking at this feature for ages. Followed your instructions and it works a treat!