Skip to content

Commit

Permalink
Improve rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
skyizwhite committed Jun 7, 2024
1 parent 87bc1e1 commit 8f14d58
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/element.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,27 @@
(with-accessors ((type element-type)
(props element-props)
(children element-children)) element
(let ((type-str (string-downcase type)))
(let ((type-str (string-downcase type))
(props-str (render-props props)))
(if children
(format stream
(if (and (null (rest children))
(typep (first children) 'string))
"~@<<~a~a>~2I~:_~<~a~^~:@_~:>~0I~_</~a>~:>"
"~@<<~a~a>~2I~:@_~<~@{~a~^~:@_~}~:>~0I~:@_</~a>~:>")
(if (or (rest children)
(typep (first children) 'element))
"~@<<~a~a>~2I~:@_~<~@{~a~^~:@_~}~:>~0I~:@_</~a>~:>"
"~@<<~a~a>~2I~:_~<~a~^~:@_~:>~0I~_</~a>~:>")
type-str
(props->string props)
(mapcar (lambda (child)
(if (and (not (non-escaping-tag-p type))
(stringp child))
(escape-html-text-content child)
child))
children)
props-str
(escape-children type children)
type-str)
(format stream
(if (self-closing-tag-p type)
"<~a~a>"
"<~a~a></~a>")
type-str
(props->string props)
props-str
type-str)))))

(defun props->string (props)
(defun render-props (props)
(with-output-to-string (stream)
(loop
:for (key value) :on props :by #'cddr
Expand All @@ -109,6 +105,14 @@
key-str
(escape-html-attribute value)))))))

(defun escape-children (type children)
(mapcar (lambda (child)
(if (and (not (non-escaping-tag-p type))
(stringp child))
(escape-html-text-content child)
child))
children))

(defmethod print-object ((element html-tag) stream)
(format stream "<!DOCTYPE html>~%")
(call-next-method))
Expand Down

0 comments on commit 8f14d58

Please sign in to comment.