1 USING: accessors assocs html.parser html.parser.utils combinators
2 continuations hashtables
3 hashtables.private io kernel math
4 namespaces prettyprint quotations sequences splitting
6 IN: html.parser.printer
11 TUPLE: text-printer < html-printer ;
12 TUPLE: src-printer < html-printer ;
13 TUPLE: html-prettyprinter < html-printer ;
15 HOOK: print-text-tag html-printer ( tag -- )
16 HOOK: print-comment-tag html-printer ( tag -- )
17 HOOK: print-dtd-tag html-printer ( tag -- )
18 HOOK: print-opening-tag html-printer ( tag -- )
19 HOOK: print-closing-tag html-printer ( tag -- )
21 ERROR: unknown-tag-error tag ;
23 : print-tag ( tag -- )
25 { [ dup name>> text = ] [ print-text-tag ] }
26 { [ dup name>> comment = ] [ print-comment-tag ] }
27 { [ dup name>> dtd = ] [ print-dtd-tag ] }
28 { [ dup [ name>> string? ] [ closing?>> ] bi and ]
29 [ print-closing-tag ] }
30 { [ dup name>> string? ]
31 [ print-opening-tag ] }
35 : print-tags ( vector -- ) [ print-tag ] each ;
37 : html-text. ( vector -- )
38 T{ text-printer } html-printer [ print-tags ] with-variable ;
40 : html-src. ( vector -- )
41 T{ src-printer } html-printer [ print-tags ] with-variable ;
43 M: html-printer print-text-tag ( tag -- ) text>> write ;
45 M: html-printer print-comment-tag ( tag -- )
46 "<!--" write text>> write "-->" write ;
48 M: html-printer print-dtd-tag ( tag -- )
49 "<!" write text>> write ">" write ;
51 : print-attributes ( hashtable -- )
52 [ [ bl write "=" write ] [ ?quote write ] bi* ] assoc-each ;
54 M: src-printer print-opening-tag ( tag -- )
57 [ attributes>> dup assoc-empty? [ drop ] [ print-attributes ] if ] bi
60 M: src-printer print-closing-tag ( tag -- )
69 : prettyprint-html ( vector -- )
71 T{ html-prettyprinter } printer set
72 V{ } clone tagstack set
79 tab-width get #indentations get * CHAR: \s <repetition> write ;
81 M: html-prettyprinter print-opening-tag ( tag -- )
86 M: html-prettyprinter print-closing-tag ( tag -- )