Plumber's Hack 1: Highlighting Sourcy
-->
Grid Bag Grease introduced a Gbc.java convenience class, and a Sourcy sample app that used it. Here is the formal introduction to that Quite Sourcy thing.
So I want my code samples in my blog to be highlighted. Because it's easier to read,
but more importantly, it just looks better! :) Netbeans' "Print to HTML"
produces an HTML file, complete with CSS styles. So i wrote this Quite Sourcy thing
to convert that into an HTML snippet to cut and paste into a blog entry.
It just trivially post-processes the output from Netbeans "Print to HTML" into a non-CSS format, using some "search and replace" commands. In particular, it changes span tags from referring to a CSS style eg. class="java-keywords", to using an embedded style attribute, as follows.
private String process(String line) { line = replace(line, "java-keywords", "color: #000099; font-weight: bold"); line = replace(line, "java-string-literal", "color: #99006b"); line = replace(line, "java-layer-method", "font-weight: bold"); line = replace(line, "java-numeric-literals", "color: #780000"); return line; } private String replace(String line, String className, String style) { String replacePattern = "class=\"" + className + "\""; String replaceWith = "style='" + style + "'"; line = stringHelper.replaceAll(line, replacePattern, replaceWith); return line; }
Here is the Quite Sourcy launcher.
(Sourcy, Java5, 195k, unsandboxed)
You'll get the following screen.
You can try to cut and paste the contents of the HTML output file produced using Netbeans 5
"Print to HTML" into the Source tab,
then press Process. The output will be written to the Output tab,
and from there you can cut and paste it into your blog.
The source code for Quite Sourcy is in
vellum.dev.java.net.
Soon, its dependent aptfoundation classes will move across to
aptfoundation.dev.java.net,
and Sourcy will move to
quitewriter.dev.java.net.
Hey, in case you missed it, you can check in with
Trip and Tick 3
on using cvs.exe to kick off a java.net/Netbeans project. If you don't already have
your own sandpit on java.net, then c'mon, it's playtime! And if you haven't played
around with Netbeans' Matisse GUI builder, then you are in for quite a treat :)
Credits: Icons by everaldo.com.
The next article is this series is Plumber's Hack 2: Quite Writer
about some softwarez i'm writing to help me write technical articles.
As you might expect, it does automatic syntax highlighting of code samples.
- Login or register to post comments
- Printer-friendly version
- evanx's blog
- 517 reads





