From fc9c7130b08ebc7ad4dbd0f16040251089c11f33 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Sat, 9 Jan 2021 01:48:36 +0100 Subject: Added injection of variable in scope --- README.md | 2 +- temper.lisp | 46 +++++++++++++++++++++++++++++----------------- test.html | 1 + 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4e1d866..b317020 100644 --- a/README.md +++ b/README.md @@ -13,4 +13,4 @@ Common Listp templating language. ## TODO * [x] value interpolation (e.g `<%= link %>`) -* [ ] insert variable in local scope +* [x] inject variable in local scope diff --git a/temper.lisp b/temper.lisp index 26eb9ec..a039f9c 100644 --- a/temper.lisp +++ b/temper.lisp @@ -42,27 +42,39 @@ `(setf buf (concatenate 'string buf - (format nil "~A" ,(read-from-string token-content)) - '(#\linefeed))))) + (format nil "~A" ,(read-from-string token-content)))))) ((eql token-type 'string) (format nil "~S" `(setf buf - (concatenate 'string - buf - ,(string-trim '(#\linefeed #\space) token-content) - '(#\linefeed)))))))) + (concatenate 'string buf ,(string-trim '(#\linefeed) token-content)))))))) tokens))) -(defun generate (tokens) - `(progn - (setq buf "") - (eval ,(read-from-string - (concatenate 'string - "(progn " - (tokens-to-code-string tokens) - ")"))))) + +(defun rest-keys (&rest args) + (if (null args) + '() + (destructuring-bind (key value &rest args) args + (cons (list key value) (apply #'rest-keys args))))) + + + +(defun generate (tokens &rest args) + `(let ,(apply #'rest-keys args) + (progn + (setq buf "") + (eval ,(read-from-string + (concatenate 'string + "(progn " + (tokens-to-code-string tokens) + ")")))))) + + +(setq tokens (lex (read-all))) +(setq code (generate tokens 'foo "bonjour" 'bar "aurevoir")) +; (format t "~S" code) +(princ (eval code)) +; (setq res (generate (lex (read-all)))) +; (format t "~A" (eval res)) -; (format t "~A" (tokens-to-code-string (lex (read-all)))) -(setq res (generate (lex (read-all)))) -(format t "~A" (eval res)) +; (format t "~S" (rest-keys :baz 10 :foo 4 :bar 5)) diff --git a/test.html b/test.html index 097ad56..fada913 100644 --- a/test.html +++ b/test.html @@ -1,3 +1,4 @@ +

<%= foo %> | <%= bar %>