Advertisement
dom-cmpx

HTML.lisp

Sep 19th, 2022 (edited)
836
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.99 KB | None | 0 0
  1. ;;;; clisp html.lisp > index.html
  2.  
  3. ;; sexp-format (name ((atribute value) ...) tag-body)
  4. ;; sgml-format <name atribute="value" ...>tag-body</name>
  5. (defvar *HTML*
  6.   '(html ((lang en)) (head () (meta ((charset utf-8)))
  7.               (meta
  8.                ((name viewport)
  9.             (content width=device-width)))
  10.               (title () dom))
  11.     (body ((style "color: #BBB; background: #555;"))
  12.      (p () "&lt;Hello,&#x20;world!&gt;")
  13.      (a ((href "https://pastebin.com/mSKvS8qR") (target "_blank")) src))))
  14.  
  15. (defun fmt-atributes (atributes)
  16.      (cond ((null atributes)
  17.                (format nil ""))
  18.           ((= (length atributes) 1)
  19.                (apply #'format nil "~a=\"~a\"" (car atributes)))
  20.           (t (format nil "~a ~a" (apply #'format nil "~a=\"~a\""
  21.                (car atributes)) (fmt-atributes (cdr atributes))))))
  22.  
  23. (defun fmt-contents (list)
  24.      (let ((tmp ""))
  25.           (loop for element in list do
  26.                (if (null element)
  27.                     '()
  28.                     (if (listp element)
  29.                          (setq tmp (format nil "~a~a" tmp (sexpr-to-xml element)))
  30.                          (setq tmp (format nil "~a~a" tmp element)))))
  31.           tmp))
  32.  
  33. (defun sxp-xml (name atributes &rest contents)
  34.      (let ((*print-case* :downcase))
  35.           (if (null contents)
  36.                (format nil "<~s ~a />"
  37.                     name (fmt-atributes atributes))
  38.                (format nil "<~s ~a>~a</~s>"
  39.                     name (fmt-atributes atributes)
  40.                     (fmt-contents contents) name))))
  41.  
  42. (defun sexpr-to-xml (a) (apply #'sxp-xml a))
  43.  
  44. (format t "<!DOCTYPE html>~A<!-- clisp html.lisp -->" (sexpr-to-xml *HTML*))
  45.  
  46. ;;; prints
  47. ; <!DOCTYPE html><html lang="en"><head ><meta charset="utf-8" /><meta name="viewport" content="width=device-width" /><title >dom</title></head><body style="color: #BBB; background: #555;"><p >&lt;Hello,&#x20;world!&gt;</p><a href="https://pastebin.com/mSKvS8qR" target="_blank">src</a></body></html><!-- clisp html.lisp -->
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement