diff --git a/template.lisp b/template.lisp
index e2b9914..9103bad 100644
--- a/template.lisp
+++ b/template.lisp
@@ -49,32 +49,32 @@
(defun template-var-p (var)
(and (symbolp var)
(eq (symbol-package var)
- (find-package *template-vars-package*))))
+ (find-package *template-vars-package*))))
(defmacro template-let (bindings &body body)
"By default bind to an existing variable."
(let ((bindings (mapcar (lambda (b)
- (if (consp b)
- b
- (list b b)))
- bindings)))
+ (if (consp b)
+ b
+ (list b b)))
+ bindings)))
`(let ,(mapcar (lambda (b) `(,(template-var-key (car b)) ,(cadr b)))
- bindings)
+ bindings)
(declare (special ,@(mapcar (lambda (b) (template-var-key (car b)))
- bindings)))
+ bindings)))
(symbol-macrolet ,(mapcar (lambda (b)
- `(,(car b) ,(template-var-key (car b))))
- bindings)
- ,@body))))
+ `(,(car b) ,(template-var-key (car b))))
+ bindings)
+ ,@body))))
(defmacro define-template-var (name &optional (value nil value-p) doc)
(let ((var (template-var-key name)))
(if value-p
- `(defparameter ,var ,value ,@(when doc `(,doc)))
- (if doc
- `(progn (defvar ,var)
- (setf (documentation ',var t) ,doc))
- `(defvar ,var)))))
+ `(defparameter ,var ,value ,@(when doc `(,doc)))
+ (if doc
+ `(progn (defvar ,var)
+ (setf (documentation ',var t) ,doc))
+ `(defvar ,var)))))
;; Template reader
@@ -84,36 +84,36 @@
(defvar *writing*)
(flet ((read-plain (stream &optional char)
- (declare (ignore char))
- (let ((plain (with-output-to-string (out)
- (loop for c = (peek-char nil stream nil #\« t)
- until (char= #\« c)
- do (write-char (read-char stream) out)))))
- (when (and *nested*
- (char= #\« (peek-char nil stream nil #\Z t)))
- (read-char stream t nil t)
- (unless (char= #\Space (peek-char nil stream t nil t))
- (unread-char #\« stream)))
- (unless (= 0 (length plain))
- (if *writing*
- plain
- `(write-string ,plain *template-output*)))))
+ (declare (ignore char))
+ (let ((plain (with-output-to-string (out)
+ (loop for c = (peek-char nil stream nil #\« t)
+ until (char= #\« c)
+ do (write-char (read-char stream) out)))))
+ (when (and *nested*
+ (char= #\« (peek-char nil stream nil #\Z t)))
+ (read-char stream t nil t)
+ (unless (char= #\Space (peek-char nil stream t nil t))
+ (unread-char #\« stream)))
+ (unless (= 0 (length plain))
+ (if *writing*
+ plain
+ `(write-string ,plain *template-output*)))))
(read-var (stream char)
- (let ((var (let ((*package* *template-vars-package*))
- (read stream))))
- (assert (eq *template-vars-package* (symbol-package var)))
- (ecase char
- ( #\$ (car (push (the symbol var) *template-vars*))))))
+ (let ((var (let ((*package* *template-vars-package*))
+ (read stream))))
+ (assert (eq *template-vars-package* (symbol-package var)))
+ (ecase char
+ ( #\$ (car (push (the symbol var) *template-vars*))))))
(dispatch-template-eval (stream c n)
- (declare (ignore c n))
- (assert (not *nested*) nil "Template parse error: Nested « ")
- (let ((*nested* t)) (read stream t nil t)))
+ (declare (ignore c n))
+ (assert (not *nested*) nil "Template parse error: Nested « ")
+ (let ((*nested* t)) (read stream t nil t)))
(dispatch-template-eval-print (stream c n)
- (declare (ignore c n))
- (assert (not *writing*) () "Template parse error: Nested «=")
- (let ((*nested* t)
- (*writing* t))
- `(write-string (str ,(read stream t nil t)) *template-output*))))
+ (declare (ignore c n))
+ (assert (not *writing*) () "Template parse error: Nested «=")
+ (let ((*nested* t)
+ (*writing* t))
+ `(write-string (str ,(read stream t nil t)) *template-output*))))
(let ((rt (copy-readtable nil)))
(make-dispatch-macro-character #\« t rt)
(set-dispatch-macro-character #\« #\Space #'dispatch-template-eval rt)
@@ -123,12 +123,12 @@
(defun read-template (stream)
(let ((*readtable* (copy-readtable rt))
- (*nested* nil)
- (*writing* nil)
- (*template-vars* nil))
- (loop while (peek-char nil stream nil)
- for item = (read-plain stream) then (read stream)
- when item collect item)))))
+ (*nested* nil)
+ (*writing* nil)
+ (*template-vars* nil))
+ (loop while (peek-char nil stream nil)
+ for item = (read-plain stream) then (read stream)
+ when item collect item)))))
(defun read-template-from-string (string)
(with-input-from-string (s string)
@@ -136,8 +136,8 @@
(defun read-template-from-file (path)
(with-open-file (stream path
- :element-type 'character
- :external-format :utf-8)
+ :element-type 'character
+ :external-format :utf-8)
(read-template stream)))
;; Compilation
@@ -145,40 +145,40 @@
(defmacro do-tree ((var tree) &body body)
(let ((walk (gensym "WALK-")))
`(labels ((,walk (,var)
- (if (consp ,var)
- (progn (,walk (car ,var))
- (,walk (cdr ,var)))
- (progn ,@body))))
+ (if (consp ,var)
+ (progn (,walk (car ,var))
+ (,walk (cdr ,var)))
+ (progn ,@body))))
(,walk ,tree))))
(defun collect-vars (sexp)
(let (vars)
(do-tree (x sexp)
(when (and (symbolp x) (eq *template-vars-package* (symbol-package x)))
- (pushnew x vars :test #'eq)))
+ (pushnew x vars :test #'eq)))
(nreverse vars)))
(defun template-printer-code (template)
(let ((vars))
(labels ((walk (x)
- (if (consp x)
- (cons (walk (car x)) (walk (cdr x)))
- (if (template-var-p x)
- (let ((g (assoc x vars)))
- (if g
- (cdr g)
- (cdar (push (cons x (gensym (symbol-name x)))
- vars))))
- x))))
+ (if (consp x)
+ (cons (walk (car x)) (walk (cdr x)))
+ (if (template-var-p x)
+ (let ((g (assoc x vars)))
+ (if g
+ (cdr g)
+ (cdar (push (cons x (gensym (symbol-name x)))
+ vars))))
+ x))))
(let ((body (walk template)))
- `(lambda ()
- ,@(if vars
- `((let ,(mapcar (lambda (var)
- `(,(cdr var) (when (boundp ',(car var))
- (symbol-value ',(car var)))))
- vars)
- ,@body))
- body))))))
+ `(lambda ()
+ ,@(if vars
+ `((let ,(mapcar (lambda (var)
+ `(,(cdr var) (when (boundp ',(car var))
+ (symbol-value ',(car var)))))
+ vars)
+ ,@body))
+ body))))))
#+nil
(template-printer-code (read-template-from-string "plop«= $abc »"))
@@ -200,22 +200,22 @@
(defun compile-template-from-file (path)
(let ((found #1=(gethash path *template-cache*)))
(if (and found
- (= (car found) #2=(file-write-date path)))
- (cdr found)
- (let ((template (compile-template (read-template-from-file path))))
- (format t "~&;; Compiling template ~S~%" path)
- (if found
- (setf (car found) #2#
- (cdr found) template)
- (setf #1# (cons #2# template)))
- template))))
+ (= (car found) #2=(file-write-date path)))
+ (cdr found)
+ (let ((template (compile-template (read-template-from-file path))))
+ (format t "~&;; Compiling template ~S~%" path)
+ (if found
+ (setf (car found) #2#
+ (cdr found) template)
+ (setf #1# (cons #2# template)))
+ template))))
;; Print template
(defun print-template (template)
(funcall (etypecase template
- (pathname (compile-template-from-file template))
- (string (compile-template (read-template-from-string template))))))
+ (pathname (compile-template-from-file template))
+ (string (compile-template (read-template-from-string template))))))
(define-compiler-macro print-template (&whole form template)
(if (stringp template)