Hello,
first of all, congratulations for TBNL :-)
Here's a small macro (with a big ugly name) for TBNL, that allows to bind portions of a request URI to arguments passed to dispatch functions. (see attachment)
I'm using it to create RESTful [1] URIs and web APIs for a web application I'm playing with. For example, let's say I want to publish a news archive. The "usual" way for accessing the news for August 27th, 2005 would be an URI like:
http://www.news.tld/archive?year=2005&month=08&day=27
The proposed add-on allows to easily (well, actually *more* easily) manage URIs like this:
http://www.news.tld/archive/2005/08/27/
The task is performed by a macro called create-groups-bind-regex-dispatcher, which takes three arguments:
1. a CL-PPCRE regex (a string, an s-expression or a scanner) with one or more register groups. It will be matched against the request URI;
2. a list of variable names that will be bound to the register groups above iff the regex matches;
3. a dispatch function that accepts keyword arguments named like the variables above.
A code sample for the news archive: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (defun archive-page (&key year month day) "Archive page" (format nil "Year: ~A; Month: ~A; Day: ~A" year month day))
(setq *dispatch-table* (list (create-groups-bind-regex-dispatcher "^\/archive\/(\d{4})\/(\d{1,2})\/(\d{1,2})\/?$" (year month day) archive-page) #'default-dispatcher)) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Under the hood, the list of variables provided to create-groups-bind-regex-dispatcher is used with cl-ppcre:register-groups-bind --- so you can apply some voodoo to the matched variables: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (defun archive-page (&key year month day) "Archive page with integer keyword parameters" (format nil "(+ year month day) => ~D" (+ year month day)))
(setq *dispatch-table* (list (create-groups-bind-regex-dispatcher "^\/archive\/(\d{4})\/(\d{1,2})\/(\d{1,2})\/?$" ((#'parse-integer year month day)) archive-page) #'default-dispatcher)) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
That's all. I hope it will be useful for you as well...
Comments are welcome (please don't be too rude, it's one of my first attempts with CL :-)
Regards,
alceste
References: [1] http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm