]> gitweb.factorcode.org Git - factor.git/commitdiff
html.parser.printer: reasonable tree-structured formatting with prettyprint-html
authorBjörn Lindqvist <bjourne@gmail.com>
Mon, 22 Jul 2013 12:05:14 +0000 (14:05 +0200)
committerJohn Benediktsson <mrjbq7@gmail.com>
Mon, 5 Aug 2013 16:56:43 +0000 (09:56 -0700)
extra/html/parser/printer/printer.factor

index 4419eec70e9ea9045c2622552760350bee18854d..fbad8581df08c9e2df7d42b7cdc866b175708651 100644 (file)
@@ -1,8 +1,8 @@
 USING: accessors assocs html.parser html.parser.utils combinators
 continuations hashtables
-hashtables.private io kernel math
-namespaces prettyprint quotations sequences splitting
-strings ;
+hashtables.private io kernel make math
+namespaces prettyprint quotations sequences sequences.repeating splitting
+strings unicode.categories ;
 IN: html.parser.printer
 
 SYMBOL: printer
@@ -68,22 +68,23 @@ SYMBOL: tagstack
 
 : prettyprint-html ( vector -- )
     [
-        T{ html-prettyprinter } printer set
+        T{ html-prettyprinter } html-printer set
         V{ } clone tagstack set
         2 tab-width set
         0 #indentations set
         print-tags
     ] with-scope ;
 
-: print-tabs ( -- )
-    tab-width get #indentations get * CHAR: \s <repetition> write ; 
+: tabs ( -- str )
+    " " tab-width get #indentations get * repeat ;
 
 M: html-prettyprinter print-opening-tag ( tag -- )
-    print-tabs "<" write
-    name>> write
-    ">\n" write ;
+    name>>
+    [ tabs "<" append ">\n" surround write ]
+    [ { "br" "img" } member? [ #indentations inc ] unless ] bi ;
 
 M: html-prettyprinter print-closing-tag ( tag -- )
-    "</" write
-    name>> write
-    ">" write ;
+    #indentations dec name>> tabs "</" append ">\n" surround write ;
+
+M: html-prettyprinter print-text-tag ( tag -- )
+    text>> [ blank? ] trim [ tabs "\n" surround write ] unless-empty ;