-
Notifications
You must be signed in to change notification settings - Fork 1
/
parenscriptx.lisp
53 lines (43 loc) · 1.49 KB
/
parenscriptx.lisp
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
;;;; parenscriptx.lisp
(in-package #:parenscriptx)
(declaim (optimize (speed 3) (space 0) (debug 0)))
;;; "parenscriptx" goes here. Hacks and glory await!
(defun split-tag-parts (tree)
(loop with tag = (car tree)
for rest on (cdr tree) by #'cddr
while (keywordp (car rest))
if (cadr rest)
collect (ps::encode-js-identifier (string (car rest))) into attrs
and
collect (cadr rest)
into attrs
finally (return (values tag attrs rest))))
(defun html-element-p (keyword)
(notany #'upper-case-p (ps::encode-js-identifier (string keyword))))
(parenscript:defpsmacro htm (&body trees)
(if (> (length trees) 1)
`(progn ,@(mapcar #'psx-htm-item trees))
(psx-htm-item (car trees))))
(defun psx-htm-item (tree)
(if (and (consp tree) (keywordp (car tree)))
(multiple-value-bind (tag attrs body)
(split-tag-parts tree)
`(ps:chain
*react
(create-element
,(if (html-element-p tag)
(ps::encode-js-identifier (string tag))
(intern (string tag) *package*))
(ps:create ,@attrs)
,@(loop for item in body
collect `(htm ,item)))))
tree))
(ps:defpsmacro defreact (name &rest args)
`(ps:var ,name
(ps:chain *react (create-class (ps:create 'display-name ,(ps::encode-js-identifier (string name)) ,@args)))))
;;; The following two macros are for backwards-compatibility
;;; It used to be required that parenscript inside htm be
;;; enclosed in a { macro.
(parenscript:defpsmacro { (b) b)
(parenscript:defpsmacro cl-who:esc (item)
item)