Skip to main content

Plumber's Hack 1: Highlighting Sourcy

Posted by evanx on August 9, 2006 at 4:07 AM EDT
Plumber's Hack 1: Highlighting Sourcy You wanna highlight your code samples for your blog, using Netbeans? Here's some triviawarez to help. layout1_200.jpg -->

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.

colors.png 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.

http://weblogs.java.net/blog/evanx/archive/webstart.small.gif (Sourcy, Java5, 195k, unsandboxed)

You'll get the following screen.

sourcyShot.png

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.

package_settings.png 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.

Swing and Roundabouts 5: Quite Gooey will present the pack of simple foundation and helper classes used for building the above Web Start demo, and more. I've been having far too much fun lately implementing some Quite Gooey helpers with beans binding by convention, and validation and configuration by annotation, and so this is shaping up to be a helluva long article, packed full of Web Start demos :)

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.

Related Topics >>