Thursday, 29 May 2008

YUI and Parenscript

Parenscript is a nice little mini-language that lets you compile Lisp to JavaScript. Sort of like Google does with GWT and the language to end all languages: Java.

Parenscript code looks like this:


(ps:ps
(defun say-hi ()
(alert "Hi!"))
(say-hi))


The macro ps:ps compiles the Lisp code to a JS string which looks like this:

function sayHi() {
alert('Hi!');
};
sayHi();

Notice the conversion from say-hi to sayHi. I love the Common Lisp naming conventions. They make so much sense and I miss them everywhere else. Obviously the writers of Parenscript agreed with me so they convert each symbol they see into a JS symbol which keeps things looking mostly neat.

The major conventions are (I don't know that there are more):

  • *foo* becomes FOO

  • foo-bar becomes fooBar

  • foo.*bar becomes foo.Bar



Anyhoo, YUI is a largish but nice JavaScript library that one can use for creating quite interactive web applications.

The problem is that YUI has names like YAHOO.util.Event.onDOMReady which is a handful to type even in JavaScript. If you try converting this symbol via ParenScript you will be in for a surprise (try it!) Anyway, there are a couple of ways to write this in ParenScript and keep the casing intact without losing your hair:


  • *yahoo*.util.*event.on-d-o-m-ready

  • |:YAHOO.util.:Event.:onDOMReady|



This works because the bars prevent the reader from downcasing or upcasing the symbol and ParenScript promises that it will leave symbols that begin with a colon alone.

Even so, this is still a PITA to type. What I do is I have a wrapper script that gets compiled once with short names for all of these. So for the above example, using Hunchentoot:

(defin-easy-handler (my-yui :uri "/ps/my-yui.ps") ()
(ps:ps
(defun yui-on-dom-ready (fn)
(|:YAHOO.util.:Event.:onDOMReady| fn))
(defvar yui-some-other-funky-name |:YAHOO.util.:FunkyChickens|)))


Yay.

No comments: