Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created December 8, 2012 01:38
Show Gist options
  • Select an option

  • Save podhmo/4238084 to your computer and use it in GitHub Desktop.

Select an option

Save podhmo/4238084 to your computer and use it in GitHub Desktop.
(defun logicless-template:emit (string)
(let ((tokens
(loop for x in (split-string string "{{")
nconc
(cond ((string-match "}}" x)
(let ((left (substring x 0 (match-beginning 0)))
(right (substring x (match-end 0))))
(list (format "%s" (eval (read left)))
right)))
(t (list x))))))
(apply 'concat tokens)))
(defun logicless-template:eval-only (string)
(let ((tokens
(loop for x in (split-string string "{{{")
collect
(cond ((string-match "}}}\n" x)
(let ((left (substring x 0 (match-beginning 0)))
(right (substring x (match-end 0))))
(eval (read left))
right))
(t x)))))
(apply 'concat tokens)))
(defun logicless-template:comment (string)
(let ((tokens
(loop for x in (split-string string "#|")
collect
(cond ((string-match "|#\n" x)
(let ((left (substring x 0 (match-beginning 0)))
(right (substring x (match-end 0))))
(eval (read left))
right))
(t x)))))
(apply 'concat tokens)))
(defun logicless-template (string)
(logicless-template:emit
(logicless-template:eval-only
(logicless-template:comment string))))
(let ((aaa 100))
(logicless-template "--{{aaa}}--"))
(let ((aaa 100))
(logicless-template "--{{(+ aaa aaa aaa)}}--"))
(let ((aaa 100))
(logicless-template "--{{(mapcar (lambda (x) (* aaa x)) '(1 2 3))}}--"))
(let ((aaa 100))
(logicless-template "--{{
(cond ((= aaa 1000) (+ aaa aaa))
(t \"fail\"))
}}--"))
(let ((aaa 100))
(logicless-template "\
{{{
(setq bbb (* aaa aaa))
}}}
#|
this is comment
|#
--{{
(cond ((= aaa 100) (+ bbb aaa))
(t \"fail\"))
}}--"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment