Skip to content
Browse files

Add (!unescape ...) and (!escape ...) forms to locally toggle XML cha…

…racter escaping.
  • Loading branch information...
1 parent 50f1f32 commit 4ea4c7d0a9d9c89ee2452463ae32af04da9da645 @thorstadt thorstadt committed Sep 10, 2011
Showing with 21 additions and 11 deletions.
  1. +18 −11 xmlgen.el
  2. +3 −0 xmlgen.etest
View
29 xmlgen.el
@@ -84,17 +84,24 @@ elements content.")
(let ((el (car xml)))
(unless (symbolp el)
(error "Element must be a symbol (got '%S')." el))
- (setq el (symbol-name el))
- (concat "<" el (xmlgen-attr-to-string attrs)
- (if (> (length xml) 1)
- (concat ">" (mapconcat
- (lambda (s) (xmlgen s el (1+ level)))
- (if xmlgen-escape-elm-vals
- (mapcar 'xmlgen-string-escape (cdr xml))
- (cdr xml))
- "")
- "</" el ">")
- "/>"))))))))
+ (if (member el '(!unescape !escape))
+ (let ((xmlgen-escape-elm-vals (if (equal '!escape el) t nil)))
+ (mapconcat
+ (lambda (s) (xmlgen s in-elm (1+ level)))
+ (cdr xml)
+ ""))
+ (progn
+ (setq el (symbol-name el))
+ (concat "<" el (xmlgen-attr-to-string attrs)
+ (if (> (length xml) 1)
+ (concat ">" (mapconcat
+ (lambda (s) (xmlgen s el (1+ level)))
+ (if xmlgen-escape-elm-vals
+ (mapcar 'xmlgen-string-escape (cdr xml))
+ (cdr xml))
+ "")
+ "</" el ">")
+ "/>"))))))))))
(defun xmlgen-string-escape (string)
"Escape STRING for inclusion in some XML."
View
3 xmlgen.etest
@@ -23,6 +23,9 @@
(equal (xmlgen '(h1 "Title")) "<h1>Title</h1>")
(equal (xmlgen '(h1 :class "something" "hi"))
"<h1 class=\"something\">hi</h1>")
+ (equal (xmlgen '(div (p "Escaped: &") (!unescape (p "Unescaped: &") (!escape
+ (p "& escaped")))))
+ "<div><p>Escaped: &amp;</p><p>Unescaped: &</p><p>&amp; escaped</p></div>")
(equal (xmlgen '(p "hello" "again")) "<p>helloagain</p>")
("more complex"
(equal (xmlgen

0 comments on commit 4ea4c7d

Please sign in to comment.
Something went wrong with that request. Please try again.