1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: generic assocs help http io io.styles io.files continuations
4 io.streams.string kernel math math.order math.parser namespaces
5 quotations assocs sequences strings words html.elements
6 xml.entities sbufs continuations destructors accessors ;
9 GENERIC: browser-link-href ( presented -- href )
11 M: object browser-link-href drop f ;
13 TUPLE: html-stream stream last-div ;
15 ! stream-nl after with-nesting or tabular-output is
16 ! ignored, so that HTML stream output looks like
18 : last-div? ( stream -- ? )
19 [ f ] change-last-div drop ;
21 : not-a-div ( stream -- stream )
24 : a-div ( stream -- straem )
27 : <html-stream> ( stream -- stream )
32 TUPLE: html-sub-stream < html-stream style parent ;
34 : new-html-sub-stream ( style stream class -- stream )
40 : end-sub-stream ( substream -- string style stream )
41 [ stream>> >string ] [ style>> ] [ parent>> ] tri ;
43 : object-link-tag ( style quot -- )
48 ] [ call ] if* ; inline
50 : hex-color, ( triplet -- )
52 [ 255 * >fixnum >hex 2 CHAR: 0 pad-left % ] each ;
54 : fg-css, ( color -- )
55 "color: #" % hex-color, "; " % ;
57 : bg-css, ( color -- )
58 "background-color: #" % hex-color, "; " % ;
60 : style-css, ( flag -- )
62 { italic bold-italic } member?
63 "font-style: " % "italic" "normal" ? % "; " %
64 { bold bold-italic } member?
65 "font-weight: " % "bold" "normal" ? % "; " % ;
67 : size-css, ( size -- )
68 "font-size: " % # "pt; " % ;
70 : font-css, ( font -- )
71 "font-family: " % % "; " % ;
73 : apply-style ( style key quot -- style gadget )
74 >r over at r> when* ; inline
76 : make-css ( style quot -- str )
79 : span-css-style ( style -- str )
81 foreground [ fg-css, ] apply-style
82 background [ bg-css, ] apply-style
83 font [ font-css, ] apply-style
84 font-style [ style-css, ] apply-style
85 font-size [ size-css, ] apply-style
88 : span-tag ( style quot -- )
89 over span-css-style dup empty? [
92 <span =style span> call </span>
95 : format-html-span ( string style stream -- )
97 [ [ drop write ] span-tag ] object-link-tag
98 ] with-output-stream* ;
100 TUPLE: html-span-stream < html-sub-stream ;
102 M: html-span-stream dispose
103 end-sub-stream not-a-div format-html-span ;
105 : border-css, ( border -- )
106 "border: 1px solid #" % hex-color, "; " % ;
108 : padding-css, ( padding -- ) "padding: " % # "px; " % ;
110 : pre-css, ( margin -- )
111 [ "white-space: pre; font-family: monospace; " % ] unless ;
113 : div-css-style ( style -- str )
115 page-color [ bg-css, ] apply-style
116 border-color [ border-css, ] apply-style
117 border-width [ padding-css, ] apply-style
118 wrap-margin over at pre-css,
121 : div-tag ( style quot -- )
122 swap div-css-style dup empty? [
125 <div =style div> call </div>
128 : format-html-div ( string style stream -- )
130 [ [ write ] div-tag ] object-link-tag
131 ] with-output-stream* ;
133 TUPLE: html-block-stream < html-sub-stream ;
135 M: html-block-stream dispose ( quot style stream -- )
136 end-sub-stream a-div format-html-div ;
138 : border-spacing-css, ( pair -- )
139 "padding: " % first2 max 2 /i # "px; " % ;
141 : table-style ( style -- str )
143 table-border [ border-css, ] apply-style
144 table-gap [ border-spacing-css, ] apply-style
147 : table-attrs ( style -- )
148 table-style " border-collapse: collapse;" append =style ;
150 : do-escaping ( string style -- string )
151 html swap at [ escape-string ] unless ;
156 M: html-stream stream-flush
157 stream>> stream-flush ;
159 M: html-stream stream-write1
160 >r 1string r> stream-write ;
162 M: html-stream stream-write
163 not-a-div >r escape-string r> stream>> stream-write ;
165 M: html-stream stream-format
166 >r html over at [ >r escape-string r> ] unless r>
169 M: html-stream stream-nl
170 dup last-div? [ drop ] [ [ <br/> ] with-output-stream* ] if ;
172 M: html-stream make-span-stream
173 html-span-stream new-html-sub-stream ;
175 M: html-stream make-block-stream
176 html-block-stream new-html-sub-stream ;
178 M: html-stream make-cell-stream
179 html-sub-stream new-html-sub-stream ;
181 M: html-stream stream-write-table
183 <table dup table-attrs table> swap [
185 <td "top" =valign swap table-style =style td>
186 stream>> >string write
190 ] with-output-stream* ;
192 M: html-stream dispose stream>> dispose ;
194 : with-html-stream ( quot -- )
195 output-stream get <html-stream> swap with-output-stream* ; inline