This is a read-only archive!

RSS feed (Common Lisp)

I made an RSS 2.0 feed for my origami gallery the other day. Thanks to CL-WHO this is trivial. Here's the complete code:

;; RSS
(defun rss ()
  (cl-who:with-html-output-to-string (s nil :prologue "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>")
    (:rss :version "2.0" :|xmlns:atom| "http://www.w3.org/2005/Atom"
          (:channel (:title "An Origami Gallery")
                    (:link "http://origamigallery.net")
                    (:|atom:link| :href "http://origamigallery.net/feed" :rel "self" :type "application/rss+xml")
                    (:description "A photo gallery.  Of origami models.")
                    (loop for model in (all-models)
                          do (htm (:item (:title (str (fullname model)))
                                         (:link (str (absolute-url model)))
                                         (:guid  (str (absolute-url model)))
                                         (:description (str (clean-remarks (remarks model)))))))))))

There are other Common Lisp libraries to make RSS feeds, which may have made this even easier, but I like CL-WHO.

Interesting about this code is how short it is. CL-WHO is an example of embedding another language in Common Lisp (e.g. a domain specific language), in this case, XML or HTML.

XML in particular happens to map very nicely onto s-expressions, allowing nested tags and attribute/value pairs and whatnot. Except that s-exps are far less verbose than XML, far nicer to type, and far easier to read in my opinion. One quirk here is that CL-WHO uses keyword symbols for XML tag names (CL keyword symbols begin with a colon), and XML uses colons for namespaces. To make a keyword symbol with a colon in the middle in CL, you have to surround it in pipes. No big deal though.

This kind of thing isn't entirely possible or quite as elegant in most other languages. (Ruby does pretty good though.) In PHP, for example, you can mix up PHP and HTML content, but the PHP interpreter doesn't parse the HTML or do anything interesting with it. It just treats it as a bunch of strings; embedding PHP in HTML is just a shortcut for string concatenation.

You could write a library in PHP to generate XML code (and I'm sure such libraries exist) but they're either going to have some kind of functional / OO interface, which will make it very verbose and probably clunky to use in comparison to CL, or else it's going to be something like Smarty which actually isn't PHP at all but rather a separate language with its own parser and interpreter or compiler that itself happens to be written in PHP. What you could not easily do is write PHP code that turns other PHP code into an XML document.

But that's largely what CL-WHO does. Because CL-WHO parses the s-exps that represent my XML data, if I forget a closing paren, Lisp will yell at me, so my XML is guaranteed to be well-formed. CL-WHO doesn't verify that I'm using proper RSS tags in the proper places or anything but it could easily be extended to do that. See PCL for an HTML interpreter/compiler which can verify that you only use valid HTML tags, for example.

CL-WHO also lets me change whether my XML attribute values are in single or double quotes, or whether I want to allow empty tags (as in old HTML) or require them all to be closed, and other such things. CL-WHO can nicely indent my XML, or generate it unindented.

And since this is all just Common Lisp, you see that I can embed a loop right in the middle of the XML s-exps, or do conditionals, or I could write a function that generates some or all these s-exps for me programatically. Or any number of other things. I have all of Common Lisp at my fingertips to create these s-exps.

See also "The Nature of Lisp" which is a nice long discussion of how Lisp is a better XML than XML. (And I do mean looong. I haven't read the whole article myself, but enough to know it's pretty good.)

February 18, 2008 @ 10:26 AM PST
Cateogory: Programming

3 Comments

Dirk Gently
Quoth Dirk Gently on February 18, 2008 @ 12:03 PM PST

Common Lisp looks really cool. I learning bash at the moment but I really like it. It seems to me to be a pretty straight forward language and Common Lisp looks that way too. When I get a chance I've been thinking about trying it.

O BTW Moo Nice site, I like the dark. OoooOOoohhhhhh!

Ivar Refsdal
Quoth Ivar Refsdal on February 19, 2008 @ 2:31 AM PST

"To make a keyword symbol with a colon in the middle in CL, you have to surround it in pipes." Or quote the colon? I rented Common Lisp The Language (cltl) yesterday and I think you can do this if I remember correctly.

You might want to check out this too. Pretty nice.

Slava's (defmacro.org) articles are great..! You should check out the functional programming for the rest of us. And give Weblocks a go when you feel ready! :-)

anon
Quoth anon on March 29, 2009 @ 4:20 AM PDT

Possibly xml-emitter is better-suited for RSS feeds. (And yeah, I deed read past the first paragraph. :D)