Skip to content

Instantly share code, notes, and snippets.

@dpk
Created Apr 20, 2011
Embed
What would you like to do?
Plan: a very basic SXML-to-XML transformer. Only takes a small, simple subset of the full SXML.
(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)
~& "&amp;"
~< "&lt;"
~> "&gt;"
~" "&quot;"))
@dpk

This comment has been minimized.

Copy link
Owner Author

@dpk dpk commented Apr 20, 2011

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment