]> gitweb.factorcode.org Git - factor.git/blob - extra/html/parser/printer/printer.factor
extra: use bl when possible instead of ``" " write``.
[factor.git] / extra / html / parser / printer / printer.factor
1 USING: accessors assocs combinators html.parser
2 html.parser.utils io kernel math math.order namespaces sequences
3 strings unicode.categories ;
4 IN: html.parser.printer
5
6 TUPLE: html-printer ;
7 TUPLE: text-printer < html-printer ;
8 TUPLE: src-printer < html-printer ;
9 TUPLE: html-prettyprinter < html-printer ;
10
11 HOOK: print-text-tag html-printer ( tag -- )
12 HOOK: print-comment-tag html-printer ( tag -- )
13 HOOK: print-dtd-tag html-printer ( tag -- )
14 HOOK: print-opening-tag html-printer ( tag -- )
15 HOOK: print-closing-tag html-printer ( tag -- )
16
17 ERROR: unknown-tag-error tag ;
18
19 : print-tag ( tag -- )
20     {
21         { [ dup name>> text = ] [ print-text-tag ] }
22         { [ dup name>> comment = ] [ print-comment-tag ] }
23         { [ dup name>> dtd = ] [ print-dtd-tag ] }
24         { [ dup name>> string? ]
25             [
26                 dup closing?>>
27                 [ print-closing-tag ] [ print-opening-tag ] if
28             ]
29         }
30         [ unknown-tag-error ]
31     } cond ;
32
33 : print-tags ( vector -- ) [ print-tag ] each ;
34
35 : html-text. ( vector -- )
36     T{ text-printer } html-printer [ print-tags ] with-variable ;
37
38 : html-src. ( vector -- )
39     T{ src-printer } html-printer [ print-tags ] with-variable ;
40
41 M: text-printer print-opening-tag
42     name>> {
43         { "br" [ nl ] }
44         { "ol" [ nl ] }
45         { "ul" [ nl ] }
46         { "li" [ " * " write ] }
47         [ drop ]
48     } case ;
49
50 M: text-printer print-closing-tag
51     name>>
52     [
53         { "p" "blockquote" "h1" "h2" "h3" "h4" "h5" }
54         member? [ nl nl ] when
55     ]
56     [
57         { "ul" "ol" "li" "tr" } member? [ nl ] when
58     ]
59     [ "td" = [ bl ] when ] tri ;
60
61 M: text-printer print-comment-tag drop ;
62
63 M: html-printer print-text-tag ( tag -- )
64     text>> write ;
65
66 M: html-printer print-comment-tag ( tag -- )
67     "<!--" write text>> write "-->" write ;
68
69 M: html-printer print-dtd-tag ( tag -- )
70     "<!" write text>> write ">" write ;
71
72 : print-attributes ( hashtable -- )
73     [ [ bl write "=" write ] [ ?quote write ] bi* ] assoc-each ;
74
75 M: src-printer print-opening-tag ( tag -- )
76     "<" write
77     [ name>> write ]
78     [ attributes>> dup assoc-empty? [ drop ] [ print-attributes ] if ] bi
79     ">" write ;
80
81 M: src-printer print-closing-tag ( tag -- )
82     "</" write
83     name>> write
84     ">" write ;
85
86 SYMBOL: tab-width
87 SYMBOL: #indentations
88 SYMBOL: tagstack
89
90 : prettyprint-html ( vector -- )
91     [
92         T{ html-prettyprinter } html-printer set
93         V{ } clone tagstack set
94         2 tab-width set
95         0 #indentations set
96         print-tags
97     ] with-scope ;
98
99 : tabs ( -- vseq )
100     tab-width get #indentations get 0 max * CHAR: \s <repetition> ;
101
102 M: html-prettyprinter print-opening-tag ( tag -- )
103     name>>
104     [ tabs write "<" 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 ;
107
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     [ tabs write "</" write name>> write ">\n" write ] bi ;
112
113 M: html-prettyprinter print-text-tag ( tag -- )
114     text>> [ blank? ] trim [ tabs write write "\n" write ] unless-empty ;