Commit | Line | Data |
---|---|---|
8e198a08 MB |
1 | (in-package :js) |
2 | ||
3 | (defun make-css-rule (selectors properties) | |
4 | (list (mapcar #'val-to-string | |
5 | (if (atom selectors) | |
6 | (list selectors) | |
7 | selectors)) | |
8 | properties)) | |
9 | ||
10 | (defun css-rule-selectors (css-rule) | |
11 | (first css-rule)) | |
12 | ||
13 | (defun css-rule-properties (css-rule) | |
14 | (second css-rule)) | |
15 | ||
16 | (defmacro css-rule (selectors &rest properties) | |
17 | `(make-css-rule ',selectors ',properties)) | |
18 | ||
19 | (defun propval-to-string (propval) | |
20 | (format nil "~A:~A" (val-to-string (first propval)) | |
21 | (val-to-string (second propval)))) | |
22 | ||
23 | (defun css-rule-to-string (css-rule) | |
24 | (format nil "~A {~%~{~A;~%~}}~%~%" | |
25 | (string-join (css-rule-selectors css-rule) ",") | |
26 | (loop for propval on (css-rule-properties css-rule) by #'cddr | |
27 | collect (concatenate 'string " " (propval-to-string propval))))) | |
28 | ||
29 | (defun css-to-string (rules) | |
30 | (string-join (mapcar #'css-rule-to-string rules) "; ")) | |
31 | ||
32 | (defmacro css (&rest rules) | |
33 | `((:style :type "text/css") | |
34 | (:princ #\Newline "<!--" #\Newline) | |
35 | (:princ ,@(mapcar #'(lambda (rule) `(css-rule-to-string (css-rule ,@rule))) rules)) | |
36 | (:princ "-->" #\Newline))) | |
37 | ||
38 | (defmacro css-inline (&rest propvals) | |
39 | `(concatenate 'string ,@(loop for propval on propvals by #'cddr | |
40 | collect (propval-to-string propval)))) | |
41 | ||
42 | (defmacro css-file (&rest rules) | |
43 | `(html | |
44 | (:princ | |
45 | ,@(mapcar #'(lambda (rule) `(css-rule-to-string (css-rule ,@rule))) rules)))) | |
46 | ||
47 | ;;; examples | |
48 | ||
49 | ;;; generate a CSS file | |
50 | #+nil | |
51 | (html-stream *standard-output* | |
52 | (css-file (* :border "1px solid black") | |
53 | (div.bl0rg :font-family "serif") | |
54 | (("a:active" "a:hoover") :color "black" :size "200%"))) | |
55 | ||
56 | ||
57 | ;;; generate an inline CSS spec in a HTML head element | |
58 | #+nil | |
59 | (html-stream *standard-output* | |
60 | (html | |
61 | (:html | |
62 | (:head | |
63 | (css (* :border "1px solid black") | |
64 | (div.bl0rg :font-family "serif") | |
65 | (("a:active" "a:hoover") :color "black" :size "200%")))))) | |
66 | ||
67 | ;;; generate a style attribute for a DIV element | |
68 | #+nil | |
69 | (html-stream *standard-output* | |
70 | (html (:html (:body ((:div :style (css-inline :border "1px solid black")) | |
71 | "foobar"))))) | |
72 |