1 USING: accessors assocs combinators fry html.parser
2 html.parser.utils io io.streams.string kernel math namespaces
3 sequences strings unicode ;
4 IN: html.parser.printer
6 SYMBOL: indentation " " indentation set-global
10 #indentations get indentation get '[ _ write ] times ;
13 TUPLE: text-printer < html-printer ;
14 TUPLE: src-printer < html-printer ;
15 TUPLE: html-prettyprinter < html-printer ;
17 HOOK: print-text-tag html-printer ( tag -- )
18 HOOK: print-comment-tag html-printer ( tag -- )
19 HOOK: print-dtd-tag html-printer ( tag -- )
20 HOOK: print-opening-tag html-printer ( tag -- )
21 HOOK: print-closing-tag html-printer ( tag -- )
23 ERROR: unknown-tag-error tag ;
25 : print-tag ( tag -- )
27 { [ dup name>> text = ] [ print-text-tag ] }
28 { [ dup name>> comment = ] [ print-comment-tag ] }
29 { [ dup name>> dtd = ] [ print-dtd-tag ] }
30 { [ dup name>> string? ]
33 [ print-closing-tag ] [ print-opening-tag ] if
39 : print-tags ( vector -- )
40 0 #indentations [ [ print-tag ] each ] with-variable ;
42 : html-text. ( vector -- )
43 T{ text-printer } html-printer [ print-tags ] with-variable ;
45 : html-text ( vector -- string )
46 [ html-text. ] with-string-writer ;
48 : html-src. ( vector -- )
49 T{ src-printer } html-printer [ print-tags ] with-variable ;
51 : html-src ( vector -- string )
52 [ html-src. ] with-string-writer ;
54 M: text-printer print-opening-tag
56 { "br" [ nl indent ] }
57 ! { "ol" [ nl indent ] }
58 ! { "ul" [ nl indent ] }
59 { "li" [ " * " write ] }
60 { "blockquote" [ #indentations inc indent ] }
64 M: text-printer print-closing-tag
66 [ "blockquote" = [ #indentations dec ] when ]
68 { "p" "blockquote" "h1" "h2" "h3" "h4" "h5" }
69 member? [ nl indent nl indent ] when
72 { "ul" "ol" "li" "tr" } member? [ nl indent ] when
74 [ "td" = [ bl ] when ]
77 M: text-printer print-comment-tag drop ;
79 M: html-printer print-text-tag ( tag -- )
82 M: html-printer print-comment-tag ( tag -- )
83 "<!--" write text>> write "-->" write ;
85 M: html-printer print-dtd-tag ( tag -- )
86 "<!" write text>> write ">" write ;
88 : print-attributes ( hashtable -- )
89 [ [ bl write "=" write ] [ ?quote write ] bi* ] assoc-each ;
91 M: src-printer print-opening-tag ( tag -- )
93 [ name>> write ] [ attributes>> print-attributes ] bi
96 M: src-printer print-closing-tag ( tag -- )
97 "</" write name>> write ">" write ;
99 : prettyprint-html ( vector -- )
100 T{ html-prettyprinter } html-printer [ print-tags ] with-variable ;
102 M: html-prettyprinter print-opening-tag ( tag -- )
104 [ indent "<" write write ">\n" write ]
105 ! These tags usually don't have any closing tag associated with them.
106 [ { "br" "img" } member? [ #indentations inc ] unless ] bi ;
108 M: html-prettyprinter print-closing-tag ( tag -- )
109 ! These tags usually don't have any closing tag associated with them.
110 [ { "br" "img" } member? [ #indentations dec ] unless ]
111 [ indent "</" write name>> write ">\n" write ] bi ;
113 M: html-prettyprinter print-text-tag ( tag -- )
114 text>> [ blank? ] trim [ indent write "\n" write ] unless-empty ;