1 USING: accessors assocs combinators html.parser
2 html.parser.utils io io.streams.string kernel math namespaces
3 regexp 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 SYMBOLS: preformatted? script? style? ;
56 M: text-printer print-opening-tag
58 { "br" [ nl indent ] }
59 { "ol" [ nl indent ] }
60 { "ul" [ nl indent ] }
61 { "li" [ " * " write ] }
62 { "blockquote" [ #indentations inc indent ] }
63 { "pre" [ preformatted? on ] }
64 { "script" [ script? on ] }
65 { "style" [ style? on ] }
69 M: text-printer print-closing-tag
71 [ "blockquote" = [ #indentations dec ] when ]
73 { "p" "blockquote" "h1" "h2" "h3" "h4" "h5" }
74 member? [ nl indent nl indent ] when
77 { "ul" "ol" "li" "tr" } member? [ nl indent ] when
79 [ { "th" "td" } member? [ bl ] when ]
80 [ "pre" = [ preformatted? off ] when ]
81 [ "script" = [ script? off ] when ]
82 [ "style" = [ style? off ] when ]
85 M: text-printer print-comment-tag drop ;
87 M: text-printer print-dtd-tag drop ;
89 : collapse-spaces ( text -- text' )
90 preformatted? get [ R/ \s+/ " " re-replace ] unless ;
92 M: text-printer print-text-tag
93 script? get style? get or
94 [ drop ] [ text>> collapse-spaces write ] if ;
96 M: html-printer print-text-tag
99 M: html-printer print-comment-tag
100 "<!--" write text>> write "-->" write ;
102 M: html-printer print-dtd-tag
103 "<!" write text>> write ">" write ;
105 : print-attributes ( hashtable -- )
106 [ [ bl write "=" write ] [ ?quote write ] bi* ] assoc-each ;
108 M: src-printer print-opening-tag
110 [ name>> write ] [ attributes>> print-attributes ] bi
113 M: src-printer print-closing-tag
114 "</" write name>> write ">" write ;
116 : prettyprint-html ( vector -- )
117 T{ html-prettyprinter } html-printer [ print-tags ] with-variable ;
119 M: html-prettyprinter print-opening-tag
121 [ indent "<" write write ">\n" write ]
122 ! These tags usually don't have any closing tag associated with them.
123 [ { "br" "img" } member? [ #indentations inc ] unless ] bi ;
125 M: html-prettyprinter print-closing-tag
126 ! These tags usually don't have any closing tag associated with them.
127 [ { "br" "img" } member? [ #indentations dec ] unless ]
128 [ indent "</" write name>> write ">\n" write ] bi ;
130 M: html-prettyprinter print-text-tag
131 text>> [ blank? ] trim [ indent write "\n" write ] unless-empty ;