aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2021-01-09 01:48:36 +0100
committerCharles Cabergs <me@cacharle.xyz>2021-01-09 01:48:36 +0100
commitfc9c7130b08ebc7ad4dbd0f16040251089c11f33 (patch)
tree378eced3746a30f2ad5a0acc312c796cab17d8bb
parent92375a78f43d5f62c3036b00a79f78200714e672 (diff)
downloadtemper-fc9c7130b08ebc7ad4dbd0f16040251089c11f33.tar.gz
temper-fc9c7130b08ebc7ad4dbd0f16040251089c11f33.tar.bz2
temper-fc9c7130b08ebc7ad4dbd0f16040251089c11f33.zip
Added injection of variable in scope
-rw-r--r--README.md2
-rw-r--r--temper.lisp46
-rw-r--r--test.html1
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 @@
+<h1><%= foo %> | <%= bar %></h1>
<ul>
<% (dotimes (n 10) %>
<li><%= (+ n 4 5) %></li>