 
 M: text-printer print-opening-tag
     name>> {
-        { "br" [ nl ] }
-        { "ol" [ nl ] }
-        { "ul" [ nl ] }
+        { "br" [ nl indent ] }
+        ! { "ol" [ nl indent ] }
+        ! { "ul" [ nl indent ] }
         { "li" [ " * " write ] }
+        { "blockquote" [ #indentations inc indent ] }
         [ 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 ;
+    name>> {
+        [ "blockquote" = [ #indentations dec ] when ]
+        [
+            { "p" "blockquote" "h1" "h2" "h3" "h4" "h5" }
+            member? [ nl indent nl indent ] when
+        ]
+        [
+            { "ul" "ol" "li" "tr" } member? [ nl indent ] when
+        ]
+        [ "td" = [ bl ] when ]
+    } cleave ;
 
 M: text-printer print-comment-tag drop ;
 


M: src-printer print-opening-tag ( tag -- )
    "<" write
    [ name>> write ]
    [ attributes>> dup assoc-empty? [ drop ] [ print-attributes ] if ] bi
    ">" write ;

M: src-printer print-closing-tag ( tag -- )
    "</" write
    name>> write
    ">" write ;

SYMBOL: tab-width
SYMBOL: #indentations

: 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> ;

M: html-prettyprinter print-opening-tag ( tag -- )
    name>>
    [ tabs write "<" 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 ;

M: html-prettyprinter print-text-tag ( tag -- )
    text>> [ blank? ] trim [ tabs write write "\n" write ] unless-empty ;

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 ;
