 
 M: src-printer print-opening-tag ( tag -- )
     "<" write
-    [ name>> write ]
-    [ attributes>> dup assoc-empty? [ drop ] [ print-attributes ] if ] bi
+    [ name>> write ] [ attributes>> print-attributes ] bi
     ">" write ;
 
 M: src-printer print-closing-tag ( tag -- )
-    "</" write
-    name>> write
-    ">" write ;
-
-SYMBOL: tab-width
-SYMBOL: #indentations
+    "</" write name>> write ">" write ;
 
 : prettyprint-html ( vector -- )
-    H{
-        { html-printer T{ html-prettyprinter } }
-        { tab-width 2 }
-        { #indentations 0 }
-    } [ print-tags ] with-variables ;
-
-: tabs ( -- vseq )
-    tab-width get #indentations get 0 max * CHAR: \s <repetition> ;
+    T{ html-prettyprinter } html-printer [ print-tags ] with-variable ;
 
 M: html-prettyprinter print-opening-tag ( tag -- )
     name>>
-    [ tabs write "<" write write ">\n" write ]
+    [ indent "<" write write ">\n" write ]
     ! These tags usually don't have any closing tag associated with them.
     [ { "br" "img" } member? [ #indentations inc ] unless ] bi ;
 
 M: html-prettyprinter print-closing-tag ( tag -- )
     ! These tags usually don't have any closing tag associated with them.
     [ { "br" "img" } member? [ #indentations dec ] unless ]
-    [ tabs write "</" write name>> write ">\n" write ] bi ;
+    [ indent "</" write name>> write ">\n" write ] bi ;
 
 M: html-prettyprinter print-text-tag ( tag -- )
-    text>> [ blank? ] trim [ tabs write write "\n" write ] unless-empty ;
+    text>> [ blank? ] trim [ indent write "\n" write ] unless-empty ;


M: text-printer print-opening-tag
    name>> {
        { "br" [ nl ] }
        { "ol" [ nl ] }
        { "ul" [ nl ] }
        { "li" [ " * " write ] }
        [ drop ]
    } case ;

M: text-printer print-closing-tag
    name>>
    [
        { "p" "blockquote" "h1" "h2" "h3" "h4" "h5" }
        member? [ nl nl ] when
    ]
    [
        { "ul" "ol" "li" "tr" } member? [ nl ] when
    ]
    [ "td" = [ bl ] when ] tri ;

M: text-printer print-comment-tag drop ;


USING: accessors assocs combinators fry html.parser
html.parser.utils io io.streams.string kernel math namespaces
sequences strings unicode ;
IN: html.parser.printer

SYMBOL: indentation "  " indentation set-global
SYMBOL: #indentations

: indent ( -- )
    #indentations get indentation get '[ _ write ] times ;

TUPLE: html-printer ;
TUPLE: text-printer < html-printer ;
