(in-package :araneida) ;;; XXX fix this, it's not correct (defun html-reserved-p (c) (member c '(#\< #\" #\> #\&))) (defun html-escape (html-string) (apply #'concatenate 'string (loop for c across html-string if (html-reserved-p c) collect (format nil "&#~A;" (char-code c)) else if (eql c #\Newline) collect "
" else collect (string c)))) (defun html-escape-tag (tag attrs content) (declare (ignore tag attrs)) (s. (mapcar #'html-escape content))) (setf (get 'escape 'html-converter) #'html-escape-tag) (setf (get 'null 'html-converter) #'princ-to-string) #+nil (defun html-attr (attr) (declare (optimize (speed 3) (debug 0))) (labels ((flatten (a) (if (consp a) (apply #'s. (mapcar #'flatten a)) a))) ;; surely somewhere there's a function that takes a list of ;; strings to concatenate? or maybe this isn't slow really (let ((r (apply #'s. (loop for a on attr by #'cddr append (list " " (symbol-name (car a)) "=\"" (s. (flatten (cadr a))) "\""))))) (the simple-string r)))) (macrolet ((html-attr-body () `(with-output-to-string (o) (loop for (att val . rest) on attr by #'cddr do (cond #+parenscript((and (symbolp att) (equal (symbol-name 'css) (symbol-name att))) (progn (princ " " o) (princ "style=\"" o) (princ (val-printer (js::css-inline-func val)) o) (princ "\"" o))) ((symbolp att) (progn (princ " " o) (princ (string-downcase (symbol-name att)) o) (princ "=\"" o) (princ (val-printer val) o) (princ "\"" o))) (t (error "attribute ~S is not a symbol in attribute list ~S" att attr))))))) (defun html-attr (attr) (macrolet ((val-printer (val) val)) (html-attr-body))) (defun html-attr-escaped (attr) (macrolet ((val-printer (val) `(html-escape ,val))) (html-attr-body)))) (defun empty-element-p (tag) (member (intern (symbol-name tag) #.*package*) '(img br hr link input meta))) ;; I wrote this to debug the new html functions. I might eventually work it into the ;; test suite. ;; -- Alan Shields [14 November 2005] #+nil(defun testfunc () (flet ((samplefun () "this!")) (flet ((htmlf (htmlme) (html htmlme))) (list (htmlf '(comment)) (htmlf '((comment))) (htmlf '((comment "detail"))) (htmlf '(comment "content")) (htmlf '(comment (p "here"))) (htmlf '(p)) (htmlf '((p))) (htmlf '((p :class "awful"))) (htmlf '(p "first " (span "second") ("third " "fourth" " fifth"))) (htmlf '(br)) (htmlf '((br))) (htmlf '((br :silly "tag"))) (htmlf '((span :css (:color "black" :size "200%" :family "the&family")) "Print this")) (htmlf '(html (head (title "A simple title") (css (h1 :color "red") (h2 :color "blue"))) (body (h1 "A simple header")))) (htmlf `(html (head (title "Another simple title") (js-script (defun foo (x) (alert (+ "Be a lert: " x))) (defun bar (x) (alert (+ "Bar - " x))))) (body (p "All foos and bars come to an end") (p ((a :onclick ,(js:js-inline (foo "e&nd"))) "Foo!")) (p ((a :onclick ,(js:js-inline (bar "e&nd"))) "Bar!"))))) (htmlf #'samplefun) (htmlf :br) (htmlf 1))))) (defmacro defhtmltag (tag (attributes-var content-var) &body body) "Define a custom HTML tag Useful for custom phrases, or even special constructs. Example: (defhtmltag coffee (attr content) (declare (ignore attr content)) \"c|_|\") So, saying (span \"Nice hot \" (coffee)) Would produce: Nice hot c|_| More in-depth use would be something such as: (even-odd-list (li \"one\") (li \"two\") (li \"three\")) Turning into: (ul ((li :class \"odd\") \"one\") ((li :class \"even\") \"two\") ((li :class \"odd\") \"odd\")) Your custom tag is expected to return either a string or an html construct like you'd pass to HTML-STREAM." (araneida::with-gensyms (throw-away) `(setf (get ',tag :html-converter) (lambda (,throw-away ,attributes-var ,content-var) (declare (ignore ,throw-away)) (funcall (lambda (,attributes-var ,content-var) ,@body) ,attributes-var ,content-var))))) (defun htmlp (html) "Returns t if html is a legal HTML list. NB: HTML and friends print out a superset of legal html lists. '(ul (li \"yes\") (li \"no\")) is legal 3 is not But HTML will print both of them" (and (consp html) (not (stringp (car html))))) (defmacro destructure-html ((tag-sym attrs-sym content-sym) html &body body) "Destructure an HTML construct. (destructure-html (tag attrs content) '((span :class \"strange\") \"A strange span!\") (format t \"<~A ~{~A~}>~{~A~}\" tag attrs content tag)) If the construct is invalid, it will cause an error" (once-only (html) `(if (araneida:htmlp ,html) (let ((,tag-sym (if (consp (car ,html)) (caar ,html) (car ,html))) (,attrs-sym (if (consp (car ,html)) (cdar ,html) nil)) (,content-sym (cdr ,html))) ,@body)))) ; I admit, this is a rather goofy way to write this. There's just so much code they have in common ; and this lets me modify them rather easily. (macrolet ((html-body () `(ret-block (cond ((htmlp things) (destructure-html (tag attrs content) things (let ((special-effect (get tag :html-converter))) (if special-effect (if (not (functionp special-effect)) (error "Tag ~A has :html-converter set, but NOT as a function." tag) (call-self stream (funcall special-effect tag attrs content) inline-elements)) (cond ((equal (symbol-name 'comment) (symbol-name tag)) (printing (format-out "~%"))) #+parenscript((equal (symbol-name 'css) (symbol-name tag)) (printing (format-out ""))) #+parenscript((equal (symbol-name 'js-script) (symbol-name tag)) (printing (format-out "