| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | ! Copyright (C) 2004, 2008 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  | USING: combinators generic assocs help http io io.styles | 
					
						
							|  |  |  | io.files continuations io.streams.string kernel math math.order | 
					
						
							|  |  |  | math.parser namespaces make quotations assocs sequences strings | 
					
						
							|  |  |  | words html.elements xml.entities sbufs continuations destructors | 
					
						
							| 
									
										
										
										
											2008-09-29 20:43:59 -04:00
										 |  |  | accessors arrays urls.encoding ;
 | 
					
						
							| 
									
										
										
										
											2008-05-23 18:33:31 -04:00
										 |  |  | IN: html.streams | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | GENERIC: browser-link-href ( presented -- href )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: object browser-link-href drop f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | TUPLE: html-stream stream last-div ;
 | 
					
						
							| 
									
										
										
										
											2008-01-11 01:01:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | ! stream-nl after with-nesting or tabular-output is | 
					
						
							|  |  |  | ! ignored, so that HTML stream output looks like | 
					
						
							|  |  |  | ! UI pane output | 
					
						
							|  |  |  | : last-div? ( stream -- ? )
 | 
					
						
							|  |  |  |     [ f ] change-last-div drop ;
 | 
					
						
							| 
									
										
										
										
											2008-01-11 01:01:23 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : not-a-div ( stream -- stream )
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  |     f >>last-div ; inline
 | 
					
						
							| 
									
										
										
										
											2008-01-11 01:01:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:09:02 -04:00
										 |  |  | : a-div ( stream -- stream )
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  |     t >>last-div ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-23 23:01:26 -04:00
										 |  |  | : <html-stream> ( stream -- html-stream )
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  |     f html-stream boa ;
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | TUPLE: html-sub-stream < html-stream style parent ;
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | : new-html-sub-stream ( style stream class -- stream )
 | 
					
						
							|  |  |  |     new
 | 
					
						
							|  |  |  |         512 <sbuf> >>stream | 
					
						
							|  |  |  |         swap >>parent | 
					
						
							|  |  |  |         swap >>style ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : end-sub-stream ( substream -- string style stream )
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  |     [ stream>> >string ] [ style>> ] [ parent>> ] tri ;
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : object-link-tag ( style quot -- )
 | 
					
						
							| 
									
										
										
										
											2007-10-15 16:44:40 -04:00
										 |  |  |     presented pick at [ | 
					
						
							|  |  |  |         browser-link-href [ | 
					
						
							| 
									
										
										
										
											2008-09-29 20:43:59 -04:00
										 |  |  |             <a url-encode =href a> call </a> | 
					
						
							| 
									
										
										
										
											2007-10-15 16:44:40 -04:00
										 |  |  |         ] [ call ] if*
 | 
					
						
							|  |  |  |     ] [ call ] if* ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:09:02 -04:00
										 |  |  | : href-link-tag ( style quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-09-29 20:43:59 -04:00
										 |  |  |     href pick at [ | 
					
						
							|  |  |  |         <a url-encode =href a> call </a> | 
					
						
							|  |  |  |     ] [ call ] if* ; inline
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:09:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-04 12:01:55 -04:00
										 |  |  | : hex-color, ( color -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-04 16:03:54 -04:00
										 |  |  |     [ red>> ] [ green>> ] [ blue>> ] tri
 | 
					
						
							|  |  |  |     [ 255 * >fixnum >hex 2 CHAR: 0 pad-left % ] tri@ ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : fg-css, ( color -- )
 | 
					
						
							|  |  |  |     "color: #" % hex-color, "; " % ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : bg-css, ( color -- )
 | 
					
						
							|  |  |  |     "background-color: #" % hex-color, "; " % ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : style-css, ( flag -- )
 | 
					
						
							|  |  |  |     dup
 | 
					
						
							|  |  |  |     { italic bold-italic } member?
 | 
					
						
							|  |  |  |     "font-style: " % "italic" "normal" ? % "; " % | 
					
						
							|  |  |  |     { bold bold-italic } member?
 | 
					
						
							|  |  |  |     "font-weight: " % "bold" "normal" ? % "; " % ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : size-css, ( size -- )
 | 
					
						
							|  |  |  |     "font-size: " % # "pt; " % ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : font-css, ( font -- )
 | 
					
						
							|  |  |  |     "font-family: " % % "; " % ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | : apply-style ( style key quot -- style gadget )
 | 
					
						
							| 
									
										
										
										
											2008-12-03 09:46:16 -05:00
										 |  |  |     [ over at ] dip when* ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : make-css ( style quot -- str )
 | 
					
						
							|  |  |  |     "" make nip ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : span-css-style ( style -- str )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  |         foreground [ fg-css,    ] apply-style | 
					
						
							|  |  |  |         background [ bg-css,    ] apply-style | 
					
						
							|  |  |  |         font       [ font-css,  ] apply-style | 
					
						
							|  |  |  |         font-style [ style-css, ] apply-style | 
					
						
							|  |  |  |         font-size  [ size-css,  ] apply-style | 
					
						
							|  |  |  |     ] make-css ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : span-tag ( style quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-09-06 20:13:59 -04:00
										 |  |  |     over span-css-style [ | 
					
						
							|  |  |  |         call
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         <span =style span> call </span> | 
					
						
							| 
									
										
										
										
											2008-09-06 20:13:59 -04:00
										 |  |  |     ] if-empty ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : format-html-span ( string style stream -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  |     stream>> [ | 
					
						
							| 
									
										
										
										
											2008-09-29 05:09:02 -04:00
										 |  |  |         [ [ [ drop write ] span-tag ] href-link-tag ] object-link-tag | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  |     ] with-output-stream* ;
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | TUPLE: html-span-stream < html-sub-stream ;
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-31 01:52:06 -05:00
										 |  |  | M: html-span-stream dispose | 
					
						
							| 
									
										
										
										
											2008-01-11 01:01:23 -05:00
										 |  |  |     end-sub-stream not-a-div format-html-span ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : border-css, ( border -- )
 | 
					
						
							|  |  |  |     "border: 1px solid #" % hex-color, "; " % ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : padding-css, ( padding -- ) "padding: " % # "px; " % ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | : pre-css, ( margin -- )
 | 
					
						
							|  |  |  |     [ "white-space: pre; font-family: monospace; " % ] unless ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : div-css-style ( style -- str )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  |         page-color   [ bg-css,      ] apply-style | 
					
						
							|  |  |  |         border-color [ border-css,  ] apply-style | 
					
						
							|  |  |  |         border-width [ padding-css, ] apply-style | 
					
						
							| 
									
										
										
										
											2008-01-11 01:01:23 -05:00
										 |  |  |         wrap-margin over at pre-css, | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  |     ] make-css ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : div-tag ( style quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-09-06 20:13:59 -04:00
										 |  |  |     swap div-css-style [ | 
					
						
							|  |  |  |         call
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         <div =style div> call </div> | 
					
						
							| 
									
										
										
										
											2008-09-06 20:13:59 -04:00
										 |  |  |     ] if-empty ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | : format-html-div ( string style stream -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  |     stream>> [ | 
					
						
							|  |  |  |         [ [ write ] div-tag ] object-link-tag | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  |     ] with-output-stream* ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | TUPLE: html-block-stream < html-sub-stream ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-01-31 01:52:06 -05:00
										 |  |  | M: html-block-stream dispose ( quot style stream -- )
 | 
					
						
							| 
									
										
										
										
											2008-01-11 01:01:23 -05:00
										 |  |  |     end-sub-stream a-div format-html-div ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | : border-spacing-css, ( pair -- )
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  |     "padding: " % first2 max 2 /i # "px; " % ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | : table-style ( style -- str )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         table-border [ border-css,         ] apply-style | 
					
						
							|  |  |  |         table-gap    [ border-spacing-css, ] apply-style | 
					
						
							|  |  |  |     ] make-css ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | : table-attrs ( style -- )
 | 
					
						
							|  |  |  |     table-style " border-collapse: collapse;" append =style ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | : do-escaping ( string style -- string )
 | 
					
						
							| 
									
										
										
										
											2007-12-29 01:33:21 -05:00
										 |  |  |     html swap at [ escape-string ] unless ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | PRIVATE>
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  | ! Stream protocol | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | M: html-stream stream-flush | 
					
						
							|  |  |  |     stream>> stream-flush ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | M: html-stream stream-write1 | 
					
						
							| 
									
										
										
										
											2008-12-03 09:46:16 -05:00
										 |  |  |     [ 1string ] dip stream-write ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | M: html-stream stream-write | 
					
						
							| 
									
										
										
										
											2008-12-03 09:46:16 -05:00
										 |  |  |     not-a-div [ escape-string ] dip stream>> stream-write ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | M: html-stream stream-format | 
					
						
							| 
									
										
										
										
											2008-12-03 09:46:16 -05:00
										 |  |  |     [ html over at [ [ escape-string ] dip ] unless ] dip
 | 
					
						
							| 
									
										
										
										
											2007-09-25 20:30:33 -04:00
										 |  |  |     format-html-span ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | M: html-stream stream-nl | 
					
						
							|  |  |  |     dup last-div? [ drop ] [ [ <br/> ] with-output-stream* ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: html-stream make-span-stream | 
					
						
							|  |  |  |     html-span-stream new-html-sub-stream ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: html-stream make-block-stream | 
					
						
							|  |  |  |     html-block-stream new-html-sub-stream ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | M: html-stream make-cell-stream | 
					
						
							|  |  |  |     html-sub-stream new-html-sub-stream ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: html-stream stream-write-table | 
					
						
							|  |  |  |     a-div stream>> [ | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |         <table dup table-attrs table> swap [ | 
					
						
							|  |  |  |             <tr> [ | 
					
						
							|  |  |  |                 <td "top" =valign swap table-style =style td> | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  |                     stream>> >string write
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |                 </td> | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  |             ] with each </tr> | 
					
						
							|  |  |  |         ] with each </table> | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  |     ] with-output-stream* ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-23 23:20:27 -04:00
										 |  |  | M: html-stream dispose stream>> dispose ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:09:02 -04:00
										 |  |  | : with-html-writer ( quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-05-05 03:19:25 -04:00
										 |  |  |     output-stream get <html-stream> swap with-output-stream* ; inline
 |