A Groovy Idea


Last week I was struck by a groovy idea. Really, it was headed west on the Route 30 Interstate and it negotiated a lane change without signaling and it struck me broadside. After exchanging insurance info with the neglegent idea I came to realize that my problems here at wordpress could possibly be solved by the very technology I’ve been raving about lately. Groovy is a slick rapid prototype programming language that helps even lug heads like me manage a user interface for common problems and my common problem is the lack of source code formating here at WordPress.com. So what do you do when your a programmer and your web site host doesn’t include the tools for you to do your job? You create them! Needless to say I did a crappy job at programming a user interface that would accept programming source code and give the option of encoding it in HTML. It does what it does and what it does does help. (Aplogies for the haphazard use of the word “does” there.) Anyhow this is what I whipped together:

#!/bin/sh
//bin/true; exec groovy "$0" "$@"

import groovy.swing.SwingBuilder
import javax.swing.*
import java.awt.BorderLayout

def swing = new SwingBuilder()

def txtSrc = swing.textArea()
def txtResult = swing.textArea()
def htmlEncode = { String src ->
   return src.replaceAll("\\&", "&amp;").replaceAll("\\<", "&lt;").replaceAll("\\>", "&gt;")
      .replaceAll("\t","   ").replaceAll(" ", "&nbsp;").replaceAll("\n\n","\n<br />")
}
def convertTxt = {
   def converted = htmlEncode(txtSrc.text)
   SwingUtilities.invokeLater(new Thread({txtResult.text = converted}))
}

def frame = swing.frame(
            title:'HTMLEncoder',
            location:[100,100],
            size:[800,400],
            defaultCloseOperation:javax.swing.WindowConstants.EXIT_ON_CLOSE) {
   panel(layout:new BorderLayout()) {
                toolBar(constraints:BorderLayout.NORTH) {
         button(text:'Convert!', 
            actionPerformed:{new Thread({convertTxt()}).start()}
         )
      }
      splitPane(orientation:JSplitPane.VERTICAL_SPLIT, constraints:BorderLayout.CENTER) {
         scrollPane{ widget(txtSrc) }
         scrollPane{ widget(txtResult) }
      }
   }
}

frame.show()

As you can see there’s still some work to be done here as I get truncation when lines are over a certain length so you’re not seeing the complete source code yet. What I need now is a good word wrap algorithm that follows tab alignment on wrapping. I’ll work more on it when I get time but for now please bear with me as I get things looking all fancy like for you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s