Skip to content

Instantly share code, notes, and snippets.

@danlei
Created March 20, 2011 02:36
Show Gist options
  • Save danlei/878006 to your computer and use it in GitHub Desktop.
Save danlei/878006 to your computer and use it in GitHub Desktop.
toy html generation
(defun transform-args (args)
(if (null args)
""
(destructuring-bind (name value . rest) args
(when (not (keywordp name))
(signal 'wrong-type-argument (list name)))
(concat " " (substring (symbol-name name) 1)
"=\"" (format "%s" value) "\""
(transform-args rest)))))
(defmacro deftag (tag)
`(lexical-let ((tagname (symbol-name ',tag)))
(defun ,tag (&rest rest)
(let* ((length (length rest))
(n (position t rest :key 'keywordp :from-end t))
(args (if n (butlast rest (- length n 2)) '()))
(content (if n (nthcdr (+ n 2) rest) rest)))
(concat "<" tagname (transform-args args) ">"
(mapconcat 'identity
(mapcar (apply-partially 'format "%s") content)
" ")
"</" tagname ">")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment