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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
;; -*- lexical-binding: t -*-
;;; main interface
(defvar *css-stream* nil)
(defvar *indent-css* nil
"Indicates if the properties of a selector should be indented or not.
There are three possible values:
* nil - The default value, and indicates that no indentation should be
applied
* the symbol 'tab - Indicates that the properties should be indented
using the Tab character
* an integer greater than 0 - Indicates how many Space characters
should be used to indent the properties")
(defmacro css (&rest rules)
`(mapconcat (lambda (x) (format "%s" x))
',(mapcan #'process-css-rule rules)
""))
(defun inline-css (&rest properties)
(mapconcat (lambda (x) (format "%s" x))
(process-css-properties properties t :newlines nil)
""))
(defun css-id-name (symbol)
(cl-format nil "#~(~a~)" symbol))
(defmacro make-css-var (var-name var-val)
`(progn
(setq ,var-name ,var-val)
(setf (get ',var-name 'css-var) t)))
(defmacro make-css-func (func-name &rest forms)
`(progn
(defun ,func-name ,@forms)
(setf (get ',func-name 'css-func) t)))
(make-css-func comment (comment-string) (list (concat "/*" comment-string) "*/"))
;;; implementation
(defun selector-to-string (selector)
(condition-case nil
(if (listp selector)
(destructuring-bind (specifier element)
selector
(ecase specifier
(:hover (format "%s:hover" (selector-to-string element)))
(:id (css-id-name element))))
(cond ((and (symbolp selector) (not (symbol-package selector))) (css-id-name selector))
((eql :and selector) ",")
(t (to-string selector))))
(t (error (format "%s isn't a valid CSS selector." selector)))))
(defun css-selectors-to-string (selectors)
(reduce (lambda (s1 s2)
(concat s1 " " s2))
(mapcar #'selector-to-string selectors)))
(defvar +newline+ "\n")
(defun css-func-p (val)
(if (symbolp val)
(get val 'css-func)
nil))
(defun css-var-p (val)
(if (symbolp val)
(get val 'css-var)
nil))
(defun string-cut (str n)
(or (ignore-errors (substring str 0 n)) str))
(defun css-comment-p (val)
"Return T if `val' is the start of a CSS comment, otherwise return NIL."
(string= (string-cut val 2) "/*"))
(defun expand-tree (tree)
(let ((result '()))
(labels ((scan (item)
(if (listp item)
(if (css-func-p (car item))
;; this calls the function
(scan (eval `(,(car item) ,@(cdr item))))
(mapcar #'scan item))
(if (css-var-p item)
(scan (symbol-value item))
(push item result)))))
(scan tree))
(nreverse result)))
(defun* process-css-properties (properties eval-vals &key (newlines t))
(loop for (name val) on
(expand-tree properties)
by #'cddr appending
(list
(if newlines +newline+ "")
(concat
;; Indent the property as specified in the variable `*indent-css*'
(cond ((null *indent-css*) "")
((equal *indent-css* 'tab)
(string ?\t))
((plusp *indent-css*)
(make-string *indent-css* ?\s))
;; XXX: If the value of `*indent-css*' is invalid, this
;; `cond' does the same thing as if `*indent-css*' had the
;; value `nil'. Should it raise an error?
)
(to-string name)
;; Only add the ':' character if this isn't a comment
(unless (css-comment-p name)
":"))
(if eval-vals (to-string val)
(to-string val))
;; The ';' character should only be added if this isn't a
;; comment
(if (css-comment-p name)
""
";"))))
(defun* process-css-rule (rule &key (parent-selectors nil))
(let ((selectors (if parent-selectors
(concatenate 'list parent-selectors (car rule))
(car rule)))
(properties (cadr rule))
(children-rules (cddr rule)))
(append (list +newline+ (css-selectors-to-string selectors) " {")
(process-css-properties properties nil)
(list +newline+ "}" +newline+)
(mapcan (lambda (child-rules)
(process-css-rule child-rules :parent-selectors selectors))
children-rules))))
(defun to-string (x)
(cond ((stringp x) x)
((keywordp x) (downcase (substring (symbol-name x) 1)))
((symbolp x) (downcase (symbol-name x)))
((listp x) (mapconcat (lambda (val)
(if (stringp val)
(format "'%s'" val)
(to-string val)))
x ", "))
(t (princ-to-string x))))
(not
(and
(member
nil
(list
(string=
(css (("#foo") (:height "50px")))
"
#foo {
height:50px;
}
")
;; defining a css-variable manually
;; using that css-variable
(string=
(progn
(setq my-css-var '(:margin "50px 30px"))
(setf (get 'my-css-var 'css-var) t)
(css (("#foo") (:height "50px" my-css-var))))
"
#foo {
height:50px;
margin:50px 30px;
}
")
;; now defining a css-variable with the make-css-var macro
;;using that variable
(string=
(progn
(make-css-var my-favorite-border-var '(:border "1px solid red"))
(css (("#foo") (:height "50px" my-css-var my-favorite-border-var))))
"
#foo {
height:50px;
margin:50px 30px;
border:1px solid red;
}
")
;; now with cascading
(string=
(css (("#foo") (:length "50px" my-css-var my-favorite-border-var)
(("li") (:width "50px" :float "left" my-css-var my-favorite-border-var))))
"
#foo {
length:50px;
margin:50px 30px;
border:1px solid red;
}
#foo li {
width:50px;
float:left;
margin:50px 30px;
border:1px solid red;
}
")
;;now with functions
(string=
(progn
(make-css-func foo-func2 (avar) (list avar avar))
(css (("#foo") ((foo-func2 "should-be-repeated")
:height "50px" my-css-var my-favorite-border-var)
(("li") (:width "50px" :float "left" my-css-var my-favorite-border-var)))))
"
#foo {
should-be-repeated:should-be-repeated;
height:50px;
margin:50px 30px;
border:1px solid red;
}
#foo li {
width:50px;
float:left;
margin:50px 30px;
border:1px solid red;
}
")
;; now with comments
(string=
(css (("#foo") ((comment "a comment" )
(foo-func2 "should-be-repeated")
:height "50px" my-css-var my-favorite-border-var)
(("li") (:width "50px" my-css-var my-favorite-border-var))))
"
#foo {
/*a comment*/
should-be-repeated:should-be-repeated;
height:50px;
margin:50px 30px;
border:1px solid red;
}
#foo li {
width:50px;
margin:50px 30px;
border:1px solid red;
}
")
;; To change the indentation of the properties, use the variable *indent-css*
;; Use tabs to indent
(string=
(progn
(setf *indent-css* 'tab)
(css (("#foo") ((comment "a comment")
(foo-func2 "should-be-repeated")
:height "50px" my-css-var my-favorite-border-var)
(("li") (:width "50px" my-css-var my-favorite-border-var)))))
"
#foo {
/*a comment*/
should-be-repeated:should-be-repeated;
height:50px;
margin:50px 30px;
border:1px solid red;
}
#foo li {
width:50px;
margin:50px 30px;
border:1px solid red;
}
")
;; Use a number of spaces
(string=
(progn
(setf *indent-css* 2)
(css (("#foo") ((comment "a comment" )
(foo-func2 "should-be-repeated")
:height "50px" my-css-var my-favorite-border-var)
(("li") (:width "50px" my-css-var my-favorite-border-var)))))
"
#foo {
/*a comment*/
should-be-repeated:should-be-repeated;
height:50px;
margin:50px 30px;
border:1px solid red;
}
#foo li {
width:50px;
margin:50px 30px;
border:1px solid red;
}
")
;; To get the default behaviour (no indentation), set the value of *indent-css* to nil.
(string=
(progn
(setf *indent-css* nil)
(css (("#foo") ((comment "a comment")
(foo-func2 "should-be-repeated")
:height "50px" my-css-var my-favorite-border-var)
(("li") (:width "50px" my-css-var my-favorite-border-var)))))
"
#foo {
/*a comment*/
should-be-repeated:should-be-repeated;
height:50px;
margin:50px 30px;
border:1px solid red;
}
#foo li {
width:50px;
margin:50px 30px;
border:1px solid red;
}
")
))
t))
|