54 lines
1.8 KiB
Common Lisp
54 lines
1.8 KiB
Common Lisp
(in-package :html-jen)
|
||
|
||
(defparameter *indent* 0)
|
||
(defparameter *indent-increment* 2)
|
||
(defparameter *yeni-satır* t)
|
||
|
||
(defgeneric node->html (node &optional stream)
|
||
(:method (node &optional (stream *standard-output*))
|
||
(write-string node stream))
|
||
(:documentation "NODE u STREAM e yazar"))
|
||
|
||
(defun tag-yeni-satır? (tag)
|
||
(case tag
|
||
((:html :head :body) t)
|
||
((:title :p :b :i :code) nil)))
|
||
|
||
(defmethod node->html :before ((node node) &optional (stream *standard-output*))
|
||
(loop :for i :from 0 :below *indent*
|
||
:do (write-char #\Space stream))
|
||
(setf *yeni-satır* nil)
|
||
(if (null (props node))
|
||
(format stream "<~a>" (tag node))
|
||
(loop :initially (format stream "<~a" (tag node))
|
||
:for (k v) :on (props node) :by #'cddr
|
||
:do (format stream " ~a=\"~a\"" k v)
|
||
:finally (write-char #\> stream)))
|
||
(when (tag-yeni-satır? (tag node))
|
||
(write-char #\Newline stream)
|
||
(setf *yeni-satır* t)))
|
||
|
||
(defmethod node->html ((node node) &optional (stream *standard-output*))
|
||
(if (null (children node))
|
||
(format stream "~a" (value node))
|
||
(let ((*indent* (+ *indent* *indent-increment*)))
|
||
(loop :for child :in (children node)
|
||
:do (node->html child stream)))))
|
||
|
||
(defmethod node->html :after ((node node) &optional (stream *standard-output*))
|
||
(unless (null *yeni-satır*)
|
||
(loop :for i :from 0 :below *indent*
|
||
:do (write-char #\Space stream)))
|
||
(format stream "</~a>~%" (tag node))
|
||
(setf *yeni-satır* t))
|
||
|
||
|
||
(defparameter bir-node
|
||
(node! :html (list (node! :head
|
||
(list (node! :title nil nil "başlık"))
|
||
nil nil)
|
||
(node! :body
|
||
(list (node! :p nil '(:color "red") "içerik"))
|
||
nil nil))
|
||
nil nil))
|