From ecda6371c07f91dea0837d0a0a1f20e5cd12e029 Mon Sep 17 00:00:00 2001 From: riton Date: Sun, 16 Mar 2025 19:26:03 +0300 Subject: =?UTF-8?q?node=20s=C4=B1n=C4=B1f=C4=B1=20ve=20pretty=20print=20pr?= =?UTF-8?q?ototip?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- html-jen.lisp | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 html-jen.lisp diff --git a/html-jen.lisp b/html-jen.lisp new file mode 100644 index 0000000..3eb5e54 --- /dev/null +++ b/html-jen.lisp @@ -0,0 +1,72 @@ +(in-package :html-jen) + +(defun dosya-metni-oku (dosya) + (let* ((uzunluk 0) + (metin + (with-output-to-string (out) + (with-open-file (in dosya :external-format :utf-8) + (loop :with arabellek := (make-array 8192 :element-type 'character) + :for n := (read-sequence arabellek in) + :while (< 0 n) + :do (incf uzunluk n) + (write-sequence arabellek out :start 0 :end n)))))) + (values metin uzunluk))) + +(defclass node () + ((tag :initarg :tag :accessor tag :initform nil) + (children :initarg :children :accessor children :initform nil) + (props :initarg :props :accessor props :initform nil) + (value :initarg :value :accessor value :initform nil))) + +(defun node! (tag children props value) + (make-instance 'node :tag tag :children children :props props :value value)) + +(defparameter *indent* 0) +(defparameter *indent-increment* 2) +(defparameter *yeni-satır* t) + +(defgeneric node->html (node stream) + (:documentation "NODE u STREAM e yazar")) + +(defun tag-yeni-satır? (tag) + (case tag + ((:html :head :body) t) + ((:title :p) nil))) + +(defmethod node->html :before (node stream) + (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 stream) + (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 stream) + (unless (null *yeni-satır*) + (loop :for i :from 0 :below *indent* + :do (write-char #\Space stream))) + (format stream "~%" (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)) -- cgit v1.2.3