Created
April 20, 2011 15:54
-
-
Save dpk/931741 to your computer and use it in GitHub Desktop.
Plan: a very basic SXML-to-XML transformer. Only takes a small, simple subset of the full SXML.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(deffn sxml->xml (sxml) | |
(with (tag (car sxml) attrs nil children nil) | |
(if (= (caadr sxml) '@) ; if we have attributes | |
(do (if (is-a? (car (cdadr sxml)) 'list) | |
(set attrs (cdadr sxml)) ; association list attribute mode | |
(set attrs (pairs (cdadr sxml)))) ; pair list attribute mode -- plan only | |
(set children (cddr sxml))) | |
(set children (cdr sxml))) ; else, if we don't have any attributes | |
(string ~< tag | |
(map attrs (fn (attr) | |
(string ~\s (car attr) ~= ~" (xml-encode (cadr attrs)) ~"))) | |
(if children ; if we have children | |
(string ~> | |
(map children (fn (child) | |
(if (is-a? child 'list) ; if we're a sub-tag | |
(sxml->xml child) | |
(xml-encode child)))) "</" tag ~>) ; else, if we're just a text node | |
" />")))) ; else, we're an empty tag; use a self-closing end tag | |
(deffn xml-encode xs | |
(global-sub (string xs) | |
~& "&" | |
~< "<" | |
~> ">" | |
~" """)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
GitHub's Scheme highlighter is slightly wrong for Plan; whenever you see ~", it's an instance of Plan's char type as the literal character ", but Scheme (and thus GitHub) treats it at the start of a string. Please ignore the discrepancy.