Hash :
104a68e3
Author :
Thomas de Grivel
Date :
2025-07-27T11:43:53
it works
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
;; cl-embed - ERB-style template engine for Common Lisp
;; Copyright 2025 kmx.io <contact@kmx.io>
;;
;; Permission to use, copy, modify, and distribute this software for
;; any purpose with or without fee is hereby granted, provided that the
;; above copyright notice and this permission notice appear in all
;; copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
;; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
;; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
;; CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
;; LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
;; NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
(in-package :embed)
(defun str (&rest parts)
(apply #'concatenate 'string parts))
(defun parse-template-from-stream (input-stream)
(let ((template '("(str "))
(state 'raw)
(token-stream (make-string-output-stream))
(token-position (the unsigned-byte 0)))
(loop
(let ((c (read-char input-stream nil)))
(unless c
(if (eq 'raw state)
(push (with-output-to-string (string-stream)
(prin1 (get-output-stream-string token-stream)
string-stream))
template)
(push (list state
(get-output-stream-string token-stream))
template))
(return
(read-from-string
(apply #'str
(reverse (cons ")" template))))))
(case state
((raw)
(case token-position
((0)
(if (eq c #\<)
(setf token-position 1)
(write-char c token-stream)))
((1)
(if (eq c #\%)
(setf token-position 2)
(progn
(setf token-position 0)
(write-char #\< token-stream))))
((2)
(push (with-output-to-string (string-stream)
(prin1 (get-output-stream-string token-stream)
string-stream))
template)
(if (eq c #\=)
(setf state 'verbose
token-position 0
token-stream (make-string-output-stream))
(progn
(push (get-output-stream-string token-stream)
template)
(setf state 'silent
token-position 0
token-stream (make-string-output-stream))
(write-char c token-stream))))))
((silent verbose)
(case token-position
((0)
(if (eq c #\%)
(setf token-position 1)
(write-char c token-stream)))
((1)
(if (eq c #\>)
(let ((token (get-output-stream-string token-stream)))
(push (case state
((silent)
(str "(progn " token " \"\")"))
((verbose)
token))
template)
(setf state 'raw
token-position 0
token-stream (make-string-output-stream)))
(progn
(write-char c token-stream)
(setf token-position 0)))))))))))
(defun parse-template-from-file (input-pathname)
(with-open-file (input
input-pathname
:direction :input
:element-type 'character
:external-format :utf-8)
(parse-template-from-stream input)))
(defun parse-template-from-string (input-string)
(with-input-from-string (input
input-string)
(parse-template-from-stream input)))
#+nil
(progn
(parse-template-from-string "Hello world !")
(parse-template-from-string "Start<%= (* 1024 1024) %>End"))
(defun render-template-to-stream (bindings template output-stream)
(assert (eq 'str (car template)))
(dolist (item (rest template))
(if (stringp item)
(write-string item output-stream)
(write-string (with-output-to-string (string-stream)
(prin1 (eval `(let ,bindings
,item))
string-stream))
output-stream))))
(defun render-template-to-file (bindings template output-pathname)
(with-open-file (output
output-pathname
:direction :output
:element-type 'character
:external-format :utf-8)
(render-template-to-stream bindings template output)))
(defun render-template-to-string (bindings template)
(with-output-to-string (output)
(render-template-to-stream bindings template output)))
(defun template (bindings input-string)
(render-template-to-string
bindings
(parse-template-from-string input-string)))
#+nil
(template '((a 21)) "Start<%= (* 2 a) %>End")