Created
May 4, 2011 21:51
-
-
Save edw/956113 to your computer and use it in GitHub Desktop.
SASS-like Thing in Scheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Let's you do this: | |
;; | |
;; (make-css | |
;; '(("body.loading" | |
;; (font-size "12px") | |
;; (color "#fff") | |
;; (" ul#sidenav" | |
;; (" li" | |
;; (blah "blah")))))) | |
;; | |
;; Returned is a string that looks like this: | |
;; | |
;; body.loading { font-size: 12px; color: #fff; } | |
;; body.loading ul#sidenav li { blah: blah; } | |
(define (make-css els) | |
(let outer ((els els) (ctx "") (out "")) | |
(if (null? els) out | |
(let* ((el (car els)) | |
(selector (car el))) | |
(let inner ((rules (cdr el)) (out out) (children-out "")) | |
(if (null? rules) | |
(if (zero? (string-length out)) | |
children-out | |
(string-append ctx selector " { " out "}\n" children-out)) | |
(let* ((rule (car rules)) (label (car rule))) | |
(if (string? label) | |
(inner (cdr rules) | |
out | |
(string-append children-out | |
(outer rules | |
(string-append ctx selector) | |
""))) | |
(let ((body (cdr rule))) | |
(inner (cdr rules) | |
(string-append out | |
(symbol->string label) | |
": " | |
(car body) | |
"; ") | |
children-out)))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment