Anonymous avatar Anonymous committed 1e2020f

Basic indentation during rendering HTML to a string

Comments (0)

Files changed (1)

 (defvar *current-tag* nil
   "The current tag under construction (if any)")
+(defvar *tag-printing-level* 0
+  "Used for pretty printing HTML, with proper indentation for nested tags")
 (defvar *html-out* *standard-output* 
   "Output stream for rendering html")
   (dolist (item some-list)
     (render-as-html item)))
+(defun tag-indentation-string ()
+  (make-array (* 2 *tag-printing-level*) :element-type 'character :initial-element #\Space))
 (defmethod render-tag-start ( (*current-tag* htmltag) )
   (let ((name (tag-name *current-tag* ) ))
-    (hout "<~a" (string-downcase name) )
+    ;; here's one place we manage indentation
+    (hout "~%~a<~a" 
+	  (tag-indentation-string)
+	  (string-downcase name))
     (render-tag-attributes *current-tag*)
-    (format *html-out* ">~%")))
+    (unless (tag-has-end-tag-p *current-tag*)
+      (format *html-out* "/"))
+    (format *html-out* ">")))
 (defmethod render-tag-attributes ( (*current-tag* htmltag) )
   (hout "~:{ ~a='~a'~}" 
 (defmethod render-tag-end ( (*current-tag* htmltag) )
   (let ((name (tag-name *current-tag* ) ))
-    (hout "</~a>~%" (string-downcase name) )))
+    ;; and here's another place we manage indentation
+    (hout "~%~a</~a>" 
+	  (tag-indentation-string)
+	  (string-downcase name) )))
 (defmethod render-tag-content ( (*current-tag* htmltag) )
   (render-tag-start *current-tag* )
-  (render-tag-body *current-tag* )
+  (let ((*tag-printing-level* (1+ *tag-printing-level*)))
+    (render-tag-body *current-tag* ))
   (if (tag-has-end-tag-p *current-tag*)
       (render-tag-end *current-tag* )))
 		      ((:style style) nil)
 		      ((:style-sheets style-sheets) nil)
 		      ((:script-libraries script-libraries) nil))
+  "Define a new tag renderable as HTML."
-   ;; (type (or null string) script ready style)
    (type (or null list) style-sheets script-libraries))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.