Plaster

common-lisp
x
 
1
(setf *print-case* :downcase)
2
(defclass dom-element ()
3
  ((tag-name :initarg :tag-name :accessor tag-name :initform nil)
4
   (text     :initarg :text     :accessor text     :initform nil)
5
   (classes  :initarg :classes  :accessor classes  :initform nil)
6
   (children :initarg :children :accessor children :initform nil)
7
   (father   :initarg :father   :accessor father   :initform nil)))
8
9
(defun father-child (father child)
10
  (setf (father child) father)
11
  (push child (children father)))
12
13
(defparameter root (make-instance 'dom-element :tag-name 'html))
14
15
(defparameter body (make-instance 'dom-element))
16
17
(father-child root body)
18
19
(defparameter div (make-instance 'dom-element))
20
21
(father-child body div)
22
23
(setf (tag-name div) 'div)
24
(setf (tag-name body) 'body)
25
26
(setf (text div) "oh my cat is nice")
27
28
29
(let ((nesting-value 0))
30
  (defun render (element)
31
    (format t "~vt <~a" nesting-value (tag-name element))
32
    (if (classes element)
33
        (format t " class=\"~{~a ~}\"" (classes element)))
34
    (format t ">~%")
35
    (if (text element)
36
        (format t "~vt ~a~%" (+ nesting-value 3) (text element)))
37
    (when (children element)
38
      (dolist (current (children element))
39
        (incf nesting-value)
40
        (render current)))
41
    (format t "~vt </~a>~%" nesting-value (tag-name element))
42
    (decf nesting-value)))
43
44
(format t "<!DOCTYPE html>~%")
45
(render root)
46

Annotations