1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators destructors fry html io
4 io.backend io.pathnames io.styles kernel macros make math
5 math.order math.parser namespaces sequences strings words
6 splitting xml xml.syntax ;
9 GENERIC: url-of ( object -- url )
11 M: object url-of drop f ;
13 TUPLE: html-writer data ;
17 : new-html-writer ( class -- html-writer )
18 new V{ } clone >>data ; inline
20 TUPLE: html-sub-stream < html-writer style parent ;
22 : new-html-sub-stream ( style stream class -- stream )
27 : end-sub-stream ( substream -- string style stream )
28 [ data>> ] [ style>> ] [ parent>> ] tri ;
30 : object-link-tag ( xml style -- xml )
31 presented swap at [ url-of [ simple-link ] when* ] when* ;
33 : href-link-tag ( xml style -- xml )
34 href swap at [ simple-link ] when* ;
36 : hex-color, ( color -- )
37 [ red>> ] [ green>> ] [ blue>> ] tri
38 [ 255 * >integer >hex 2 CHAR: 0 pad-head % ] tri@ ;
40 : fg-css, ( color -- )
41 "color: #" % hex-color, "; " % ;
43 : bg-css, ( color -- )
44 "background-color: #" % hex-color, "; " % ;
46 : style-css, ( flag -- )
48 { italic bold-italic } member?
49 "font-style: " % "italic" "normal" ? % "; " %
50 { bold bold-italic } member?
51 "font-weight: " % "bold" "normal" ? % "; " % ;
53 : size-css, ( size -- )
54 "font-size: " % # "pt; " % ;
56 : font-css, ( font -- )
57 "font-family: " % % "; " % ;
59 MACRO: make-css ( pairs -- str )
60 [ '[ _ swap at [ _ execute ] when* ] ] { } assoc>map
61 '[ [ _ cleave ] "" make ] ;
63 : span-css-style ( style -- str )
65 { foreground fg-css, }
66 { background bg-css, }
67 { font-name font-css, }
68 { font-style style-css, }
69 { font-size size-css, }
72 : span-tag ( xml style -- xml )
74 [ swap [XML <span style=<->><-></span> XML] ] unless-empty ; inline
76 : emit-html ( quot stream -- )
77 dip data>> push ; inline
79 : image-path ( path -- images-path )
80 "vocab:definitions/icons/" ?head [ "/icons/" prepend ] when ;
82 : img-tag ( xml style -- xml )
83 image swap at [ nip image-path simple-image ] when* ;
85 : format-html-span ( string style stream -- )
95 TUPLE: html-span-stream < html-sub-stream ;
97 M: html-span-stream dispose
98 end-sub-stream format-html-span ;
100 : border-css, ( border -- )
101 "border: 1px solid #" % hex-color, "; " % ;
103 : padding-css, ( padding -- )
104 first2 "padding: " % # "px " % # "px; " % ;
106 CONSTANT: pre-css "white-space: pre; font-family: monospace;"
108 : div-css-style ( style -- str )
111 { page-color bg-css, }
112 { border-color border-css, }
113 { inset padding-css, }
115 ] [ wrap-margin swap at [ pre-css append ] unless ] bi
116 " display: inline-block;" append ;
118 : div-tag ( xml style -- xml' )
120 [ swap [XML <div style=<->><-></div> XML] ] unless-empty ;
122 : format-html-div ( string style stream -- )
123 [ [ div-tag ] [ object-link-tag ] bi ] emit-html ;
125 TUPLE: html-block-stream < html-sub-stream ;
127 M: html-block-stream dispose ( quot style stream -- )
128 end-sub-stream format-html-div ;
130 : border-spacing-css, ( pair -- )
131 "padding: " % first2 max 2 /i # "px; " % ;
133 : table-style ( style -- str )
135 { table-border border-css, }
136 { table-gap border-spacing-css, }
138 " border-collapse: collapse;" append ;
143 M: html-writer stream-flush drop ;
145 M: html-writer stream-write1
146 [ 1string ] emit-html ;
148 M: html-writer stream-write
151 M: html-writer stream-format
154 M: html-writer stream-nl
155 [ [XML <br/> XML] ] emit-html ;
157 M: html-writer make-span-stream
158 html-span-stream new-html-sub-stream ;
160 M: html-writer make-block-stream
161 html-block-stream new-html-sub-stream ;
163 M: html-writer make-cell-stream
164 html-sub-stream new-html-sub-stream ;
166 M: html-writer stream-write-table
169 [ data>> [XML <td valign="top" style=<->><-></td> XML] ] with map
170 [XML <tr><-></tr> XML]
172 [XML <table style="display: inline-table;"><-></table> XML]
175 M: html-writer dispose drop ;
177 : <html-writer> ( -- html-writer )
178 html-writer new-html-writer ;
180 : with-html-writer ( quot -- xml )
181 <html-writer> [ swap with-output-stream* ] keep data>> ; inline