küçük refaktör

This commit is contained in:
riton
2025-03-26 00:00:29 +03:00
parent 030b33e850
commit 85b2910290

View File

@@ -2,25 +2,29 @@
(defparameter *indent* 0)
(defparameter *indent-increment* 2)
(defparameter *yeni-satır* t)
(defparameter *satır-başı* 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)
(defun tag-başı-yeni-satır? (tag)
(case tag
((:html :head :body :div :ol :ul :link) t)
((:title :p :b :i :code :li) nil)))
(defun tag-inline? (tag)
(defun tag-sonu-yeni-satır? (tag)
(case tag
((:html :head :body :div :ol :ul :li :p :title) nil)
((:html :head :body :div :ol :ul :li :p :title :pre) nil)
((:b :i :code) t)))
(defun yeni-satır-yaz (stream)
(write-char #\Newline stream)
(setf *satır-başı* t))
(defmethod node->html :before ((node node) &optional (stream *standard-output*))
(unless (not *yeni-satır*)
(unless (not *satır-başı*)
(loop :for i :from 0 :below *indent*
:do (write-char #\Space stream)))
(if (null (props node))
@@ -29,10 +33,9 @@
:for (k v) :on (props node) :by #'cddr
:do (format stream " ~a=\"~a\"" k v)
:finally (write-char #\> stream)))
(setf *yeni-satır* nil)
(when (tag-yeni-satır? (tag node))
(write-char #\Newline stream)
(setf *yeni-satır* t)))
(setf *satır-başı* nil)
(when (tag-başı-yeni-satır? (tag node))
(yeni-satır-yaz stream)))
(defmethod node->html ((node node) &optional (stream *standard-output*))
(if (null (children node))
@@ -42,13 +45,12 @@
:do (node->html child stream)))))
(defmethod node->html :after ((node node) &optional (stream *standard-output*))
(unless (not *yeni-satır*)
(unless (not *satır-başı*)
(loop :for i :from 0 :below *indent*
:do (write-char #\Space stream)))
(if (tag-inline? (tag node))
(format stream "</~a>" (tag node))
(progn (format stream "</~a>~%" (tag node))
(setf *yeni-satır* t))))
(format stream "</~a>" (tag node))
(when (tag-sonu-yeni-satır? (tag node))
(yeni-satır-yaz stream)))
(defun node->cons (node)