1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators generic assocs help http io io.styles
4 io.files continuations io.streams.string kernel math math.order
5 math.parser namespaces make quotations assocs sequences strings
6 words html.elements xml.entities sbufs continuations destructors
10 GENERIC: browser-link-href ( presented -- href )
12 M: object browser-link-href drop f ;
14 TUPLE: html-stream stream last-div ;
16 ! stream-nl after with-nesting or tabular-output is
17 ! ignored, so that HTML stream output looks like
19 : last-div? ( stream -- ? )
20 [ f ] change-last-div drop ;
22 : not-a-div ( stream -- stream )
25 : a-div ( stream -- straem )
28 : <html-stream> ( stream -- stream )
33 TUPLE: html-sub-stream < html-stream style parent ;
35 : new-html-sub-stream ( style stream class -- stream )
41 : end-sub-stream ( substream -- string style stream )
42 [ stream>> >string ] [ style>> ] [ parent>> ] tri ;
44 : object-link-tag ( style quot -- )
49 ] [ call ] if* ; inline
51 : hex-color, ( color -- )
52 [ red>> ] [ green>> ] [ blue>> ] tri
53 [ 255 * >fixnum >hex 2 CHAR: 0 pad-left % ] tri@ ;
55 : fg-css, ( color -- )
56 "color: #" % hex-color, "; " % ;
58 : bg-css, ( color -- )
59 "background-color: #" % hex-color, "; " % ;
61 : style-css, ( flag -- )
63 { italic bold-italic } member?
64 "font-style: " % "italic" "normal" ? % "; " %
65 { bold bold-italic } member?
66 "font-weight: " % "bold" "normal" ? % "; " % ;
68 : size-css, ( size -- )
69 "font-size: " % # "pt; " % ;
71 : font-css, ( font -- )
72 "font-family: " % % "; " % ;
74 : apply-style ( style key quot -- style gadget )
75 >r over at r> when* ; inline
77 : make-css ( style quot -- str )
80 : span-css-style ( style -- str )
82 foreground [ fg-css, ] apply-style
83 background [ bg-css, ] apply-style
84 font [ font-css, ] apply-style
85 font-style [ style-css, ] apply-style
86 font-size [ size-css, ] apply-style
89 : span-tag ( style quot -- )
93 <span =style span> call </span>
96 : format-html-span ( string style stream -- )
98 [ [ drop write ] span-tag ] object-link-tag
99 ] with-output-stream* ;
101 TUPLE: html-span-stream < html-sub-stream ;
103 M: html-span-stream dispose
104 end-sub-stream not-a-div format-html-span ;
106 : border-css, ( border -- )
107 "border: 1px solid #" % hex-color, "; " % ;
109 : padding-css, ( padding -- ) "padding: " % # "px; " % ;
111 : pre-css, ( margin -- )
112 [ "white-space: pre; font-family: monospace; " % ] unless ;
114 : div-css-style ( style -- str )
116 page-color [ bg-css, ] apply-style
117 border-color [ border-css, ] apply-style
118 border-width [ padding-css, ] apply-style
119 wrap-margin over at pre-css,
122 : div-tag ( style quot -- )
126 <div =style div> call </div>
129 : format-html-div ( string style stream -- )
131 [ [ write ] div-tag ] object-link-tag
132 ] with-output-stream* ;
134 TUPLE: html-block-stream < html-sub-stream ;
136 M: html-block-stream dispose ( quot style stream -- )
137 end-sub-stream a-div format-html-div ;
139 : border-spacing-css, ( pair -- )
140 "padding: " % first2 max 2 /i # "px; " % ;
142 : table-style ( style -- str )
144 table-border [ border-css, ] apply-style
145 table-gap [ border-spacing-css, ] apply-style
148 : table-attrs ( style -- )
149 table-style " border-collapse: collapse;" append =style ;
151 : do-escaping ( string style -- string )
152 html swap at [ escape-string ] unless ;
157 M: html-stream stream-flush
158 stream>> stream-flush ;
160 M: html-stream stream-write1
161 >r 1string r> stream-write ;
163 M: html-stream stream-write
164 not-a-div >r escape-string r> stream>> stream-write ;
166 M: html-stream stream-format
167 >r html over at [ >r escape-string r> ] unless r>
170 M: html-stream stream-nl
171 dup last-div? [ drop ] [ [ <br/> ] with-output-stream* ] if ;
173 M: html-stream make-span-stream
174 html-span-stream new-html-sub-stream ;
176 M: html-stream make-block-stream
177 html-block-stream new-html-sub-stream ;
179 M: html-stream make-cell-stream
180 html-sub-stream new-html-sub-stream ;
182 M: html-stream stream-write-table
184 <table dup table-attrs table> swap [
186 <td "top" =valign swap table-style =style td>
187 stream>> >string write
191 ] with-output-stream* ;
193 M: html-stream dispose stream>> dispose ;
195 : with-html-stream ( quot -- )
196 output-stream get <html-stream> swap with-output-stream* ; inline