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 ;
14 INSTANCE: html-writer output-stream
18 : new-html-writer ( class -- html-writer )
19 new V{ } clone >>data ; inline
21 TUPLE: html-sub-stream < html-writer style parent ;
23 : new-html-sub-stream ( style stream class -- stream )
28 : end-sub-stream ( substream -- string style stream )
29 [ data>> ] [ style>> ] [ parent>> ] tri ;
31 : object-link-tag ( xml style -- xml )
32 presented swap at [ url-of [ simple-link ] when* ] when* ;
34 : href-link-tag ( xml style -- xml )
35 href swap at [ simple-link ] when* ;
37 : hex-color, ( color -- )
38 [ red>> ] [ green>> ] [ blue>> ] tri
39 [ 255 * >integer >hex 2 CHAR: 0 pad-head % ] tri@ ;
41 : fg-css, ( color -- )
42 "color: #" % hex-color, "; " % ;
44 : bg-css, ( color -- )
45 "background-color: #" % hex-color, "; " % ;
47 : style-css, ( flag -- )
49 { italic bold-italic } member?
50 "font-style: " % "italic" "normal" ? % "; " %
51 { bold bold-italic } member?
52 "font-weight: " % "bold" "normal" ? % "; " % ;
54 : size-css, ( size -- )
55 "font-size: " % # "pt; " % ;
57 : font-css, ( font -- )
58 "font-family: " % % "; " % ;
60 MACRO: make-css ( pairs -- str )
61 [ '[ _ swap at [ _ execute ] when* ] ] { } assoc>map
62 '[ [ _ cleave ] "" make ] ;
64 : span-css-style ( style -- str )
66 { foreground fg-css, }
67 { background bg-css, }
68 { font-name font-css, }
69 { font-style style-css, }
70 { font-size size-css, }
73 : span-tag ( xml style -- xml )
75 [ swap [XML <span style=<->><-></span> XML] ] unless-empty ; inline
77 : emit-html ( stream quot -- )
78 dip data>> push ; inline
80 : image-path ( path -- images-path )
81 "vocab:definitions/icons/" ?head [ "/icons/" prepend ] when ;
83 : img-tag ( xml style -- xml )
84 image swap at [ nip image-path simple-image ] when* ;
86 : format-html-span ( string style stream -- )
96 TUPLE: html-span-stream < html-sub-stream ;
98 M: html-span-stream dispose
99 end-sub-stream format-html-span ;
101 : border-css, ( border -- )
102 "border: 1px solid #" % hex-color, "; " % ;
104 : padding-css, ( padding -- )
105 first2 "padding: " % # "px " % # "px; " % ;
107 CONSTANT: pre-css "white-space: pre; font-family: monospace;"
109 : div-css-style ( style -- str )
112 { page-color bg-css, }
113 { border-color border-css, }
114 { inset padding-css, }
116 ] [ wrap-margin swap at [ pre-css append ] unless ] bi
117 " display: inline-block;" append ;
119 : div-tag ( xml style -- xml' )
121 [ swap [XML <div style=<->><-></div> XML] ] unless-empty ;
123 : format-html-div ( string style stream -- )
124 [ [ div-tag ] [ object-link-tag ] bi ] emit-html ;
126 TUPLE: html-block-stream < html-sub-stream ;
128 M: html-block-stream dispose
129 end-sub-stream format-html-div ;
131 : border-spacing-css, ( pair -- )
132 "padding: " % first2 max 2 /i # "px; " % ;
134 : table-style ( style -- str )
136 { table-border border-css, }
137 { table-gap border-spacing-css, }
139 " border-collapse: collapse;" append ;
144 M: html-writer stream-flush drop ;
146 M: html-writer stream-write1
147 [ 1string ] emit-html ;
149 M: html-writer stream-write
152 M: html-writer stream-format
155 M: html-writer stream-nl
156 [ [XML <br/> XML] ] emit-html ;
158 M: html-writer make-span-stream
159 html-span-stream new-html-sub-stream ;
161 M: html-writer make-block-stream
162 html-block-stream new-html-sub-stream ;
164 M: html-writer make-cell-stream
165 html-sub-stream new-html-sub-stream ;
167 M: html-writer stream-write-table
170 [ data>> [XML <td valign="top" style=<->><-></td> XML] ] with map
171 [XML <tr><-></tr> XML]
173 [XML <table style="display: inline-table;"><-></table> XML]
176 M: html-writer dispose drop ;
178 : <html-writer> ( -- html-writer )
179 html-writer new-html-writer ;
181 : with-html-writer ( quot -- xml )
182 <html-writer> [ swap with-output-stream* ] keep data>> ; inline