Branch
Hash :
a21f47fa
Author :
Thomas de Grivel
Date :
2025-08-05T01:59:28
wip backup
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
;; 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)
(defgeneric str-write (stream parts))
(defmethod str-write ((out stream) (parts list))
(dolist (part parts)
(str-write out part)))
(defmethod str-write ((out stream) (parts null))
(declare (ignore out parts)))
(defmethod str-write ((out stream) (part character))
(write-char part out))
(defmethod str-write ((out stream) (part string))
(write-string part out))
(defmethod str-write ((out stream) (part symbol))
(write-string (string-downcase (symbol-name part)) out))
(defmethod str-write ((out stream) (part t))
(princ part out))
(defun str (&rest parts)
(with-output-to-string (out)
(str-write out parts)))
(defun parse-template-from-stream (input-stream)
(let ((template)
(state 'raw)
(token-stream (make-string-output-stream))
(token-position (the unsigned-byte 0)))
(loop
(let ((c (read-char input-stream nil)))
(unless c
(case state
((raw)
(push (str #\Newline "EKC3.verbose ") template)
(push (with-output-to-string (string-stream)
(prin1 (get-output-stream-string token-stream)
string-stream))
template))
((verbose)
(push (str #\Newline "EKC3.verbose ") template)
(push (get-output-stream-string token-stream)
template))
((silent)
(push (get-output-stream-string token-stream)
template)))
(return (str (reverse 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 (str #\Newline "EKC3.verbose ") template)
(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)))
(when (eq state 'verbose)
(push (str #\Newline "EKC3.verbose ") template))
(push 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<%= (* 2 (read-from-string %>3<% ) 4) %>End")
(parse-template-from-string "Start<%= (* 2 a) %>End<% 123 %>"))
(defun render-template-to-stream (bindings template output-stream)
(assert (eq 'template (car template)))
(let ((verbose nil))
(dolist (item (rest template))
(cond
(verbose
(write-string (with-output-to-string (string-stream)
(prin1 (eval `(let ,bindings
,item))
string-stream))
output-stream)
(setf verbose nil))
((stringp item)
(write-string item output-stream))
((eq 'verbose item)
(setf verbose t))
(t
(eval `(let ,bindings
,item)))))))
(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<% 123 %>")