Name: Anonymous 2012-09-23 16:57
(define (tagged-list? expr)
(and (pair? expr) (symbol? (car expr))))
(define (sexp->html expr)
(cond ((null? expr) "")
((string? expr) expr)
((tagged-list? expr)
(make-element (symbol->string (car expr)) "" (cdr expr)))
((pair? expr)
(string-append (sexp->html (car expr))
(sexp->html (cdr expr))))
(else (error "cannot be converted to html" expr))))
(define (make-element name attrs expr)
(cond ((null? expr) (string-append "<" name attrs "/>"))
((tagged-list? expr)
(make-element name
(string-append attrs
" " (symbol->string (car expr))
"=\"" (cadr expr) "\"")
(cddr expr)))
(else
(string-append "<" name attrs ">" (sexp->html expr) "</" name ">"))))Sample input:
(let ((title "Hello, world!")
(urls '("https://boards.4chan.org/g/"
"https://www.reddit.com/r/programming"
"https://news.ycombinator.com")))
(display
(sexp->html
`(html lang "en-us"
(head
(meta charset "utf-8")
(title ,title)
(style type "text/css"
"o { text-decoration: overline; }"
"spoiler { background-color: #000; }"
"spoiler:hover { background-color: transparent; }"))
(body
(h1 ,title)
(p "Welcome to " (spoiler "/prog/") "." (br)
(b (i (o (u "ENTERPRISE")))) " solutions!")
(h2 "Places you should go back to.")
(ul ,(map (lambda (url) `(li (a href ,url ,url)))
urls)))))))Output:
http://pastebin.com/v4cTnfTJ (spam filter won't let me post HTML)