Books of Note

Practical Common
LispThe best intro to start your journey. Excellent coverage of CLOS.

ANSI Common
LispAnother great starting point with a different focus.

Paradigms of Artificial Intelligence
ProgrammingA superb set of Lisp examples. Not just for the AI crowd.

Saturday, February 12, 2005

Escaping HTML 

Okay, so after whacking out a couple of functions to escape double-quotes and backslashes in CL strings, I felt like I was on a roll. Over the past few weeks, I have been reading John Wiseman's humorous experiences with getting ampersands (&) to pass through an HTML/XML toolchain. Needless to say, we have all been there. In fact, I have bugged Zach Beane a few times about how Planet Lisp wasn't quite handling one of my code samples correctly, only to retreat sheepishly when I had it pointed out to me that I wasn't escaping a less-than (<), greater-than (>), or ampersand correctly.

So, in honor of John Wiseman and frustrated bloggers everywhere, I give you escape-html-region, a simple Emacs Lisp function to escape the basic HTML/XML problem-children:

(defun escape-html-region (start end)
  "Escape '&<>' characters in the region using '&amp;', '&lt;', and '&gt;'."
  (interactive "*r")
      (narrow-to-region start end)
      (goto-char start)
      (while (search-forward "&" nil t)
 (replace-match "&amp;" nil t))
      (goto-char start)
      (while (search-forward "<" nil t)
 (replace-match "&lt;" nil t))
      (goto-char start)
      (while (search-forward ">" nil t)
 (replace-match "&gt;" nil t)))))

Note that I actually ran this on itself to write this blog entry. ;-)

There are, of course, a plethora of other options that will do this sort of thing for you, including converting all the Emacs font-lock colors to HTML as part of the process. A couple of the options include:

Bill Clementson covered some of this in his blog a while ago, and even discussed BKNR's htmlize.el that will generate links to CLHS for CL functions and keywords, but I can't seem to get the BKNR link working right now.

In any case, escape-html-region just escapes the characters and leaves out all the pretty colorizing.


perl -i -wple 's/([<>&])/"&".($1 eq "&" ? "amp" : $1 eq ">" ? "gt" : "lt" ).";"/ge'

Post a Comment

Links to this post:

Create a Link

This page is powered by Blogger. Isn't yours?