Skip to content

Instantly share code, notes, and snippets.

@AyeGill
Created December 5, 2012 13:14
Show Gist options
  • Select an option

  • Save AyeGill/4215416 to your computer and use it in GitHub Desktop.

Select an option

Save AyeGill/4215416 to your computer and use it in GitHub Desktop.
Converts s-expressing into xml
;(:foo ((:bar (1234))) :baz "12345") -> <foo baz="12345"><bar>1234</bar></foo>
(defgeneric make-html-indent (expr indentation))
(defmethod make-html-indent (expr indentation)
(let ((output (make-array 0
:element-type 'character
:adjustable t
:fill-pointer 0)))
(dotimes (n indentation)
(format output " "))
(format output "~a" expr)
output))
(defmethod make-html-indent ((expr list) indentation)
(let ((name (first expr)) ;first element of input list should be name of tag
(body (second expr)) ;second element should be list of contents
(attribs (cddr expr)) ;remaining elements should be attributes(cddr returns the list minus the first two elements)
(output (make-array 0
:element-type 'character
:adjustable t
:fill-pointer 0)))
(dotimes (n indentation)
(format output " "))
(format output "<")
(format output "~(~a~)" name)
(unless (eql attribs ()) (format output " "))
(format output "~{~(~a~)=\"~a\"~#[~:; ~]~}" attribs)
(format output ">")
(format output "~a" #\Newline)
(dolist (tag body)
(format output (make-html-indent tag (+ 1 indentation)))
(format output "~a" #\Newline))
(dotimes (n indentation)
(format output " "))
(format output "</~(~a~)>" name)
output))
(defun make-html (expr)
(make-html-indent expr 0))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment