Saturday, February 12, 2005
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 '&', '<', and '>'." (interactive "*r") (save-excursion (save-restriction (narrow-to-region start end) (goto-char start) (while (search-forward "&" nil t) (replace-match "&" nil t)) (goto-char start) (while (search-forward "<" nil t) (replace-match "<" nil t)) (goto-char start) (while (search-forward ">" nil t) (replace-match ">" 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.
Post a Comment
Links to this post: