! Copyright (C) 2004, 2006 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. USING: generic assocs help http io io.styles io.files io.streams.string kernel math math.parser namespaces quotations assocs sequences strings words html.elements xml.writer sbufs ; IN: html GENERIC: browser-link-href ( presented -- href ) M: object browser-link-href drop f ; TUPLE: html-stream ; : ( stream -- stream ) html-stream construct-delegate ; over set-delegate ; : ( style stream class -- stream ) >r (html-sub-stream) r> construct-delegate ; inline : end-sub-stream ( substream -- string style stream ) dup delegate >string over html-sub-stream-style rot html-sub-stream-stream ; : delegate-write ( string -- ) stdio get delegate stream-write ; : object-link-tag ( style quot -- ) presented pick at [ browser-link-href [ call ] [ call ] if* ] [ call ] if* ; inline : hex-color, ( triplet -- ) 3 head-slice [ 255 * >fixnum >hex 2 CHAR: 0 pad-left % ] each ; : 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: " % % "; " % ; : apply-style ( style key quot -- style gadget ) >r over at r> when* ; inline : make-css ( style quot -- str ) "" make nip ; inline : span-css-style ( style -- str ) [ 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 ; : span-tag ( style quot -- ) over span-css-style dup empty? [ drop call ] [ call ] if ; inline : format-html-span ( string style stream -- ) [ [ [ drop delegate-write ] span-tag ] object-link-tag ] with-stream* ; TUPLE: html-span-stream ; M: html-span-stream stream-close end-sub-stream format-html-span ; : border-css, ( border -- ) "border: 1px solid #" % hex-color, "; " % ; : padding-css, ( padding -- ) "padding: " % # "px; " % ; : pre-css, ( margin -- ) [ "white-space: pre; font-family: monospace; " % ] unless ; : div-css-style ( style -- str ) [ page-color [ bg-css, ] apply-style border-color [ border-css, ] apply-style border-width [ padding-css, ] apply-style wrap-margin [ pre-css, ] apply-style ] make-css ; : div-tag ( style quot -- ) swap div-css-style dup empty? [ drop call ] [
call
] if ; inline : format-html-div ( string style stream -- ) [ [ [ delegate-write ] div-tag ] object-link-tag ] with-stream* ; TUPLE: html-block-stream ; M: html-block-stream stream-close ( quot style stream -- ) end-sub-stream format-html-div ; : border-spacing-css, "padding: " % first2 max 2 /i # "px; " % ; : table-style ( style -- str ) [ table-border [ border-css, ] apply-style table-gap [ border-spacing-css, ] apply-style ] make-css ; : table-attrs ( style -- ) table-style " border-collapse: collapse;" append =style ; : do-escaping ( string style -- string ) html swap at [ chars>entities ] unless ; PRIVATE> ! Stream protocol M: html-stream stream-write1 ( char stream -- ) >r 1string r> stream-write ; M: html-stream stream-write ( str stream -- ) >r chars>entities r> delegate stream-write ; M: html-stream make-span-stream ( style stream -- stream' ) html-span-stream ; M: html-stream stream-format ( str style stream -- ) >r html over at [ >r chars>entities r> ] unless r> format-html-span ; M: html-stream make-block-stream ( style stream -- stream' ) html-block-stream ; M: html-stream stream-write-table ( grid style stream -- ) [ swap [ [ ] curry* each ] curry* each
>string write-html
] with-stream* ; M: html-stream make-cell-stream ( style stream -- stream' ) (html-sub-stream) ; M: html-stream stream-nl ( stream -- ) [
] with-stream* ; ! Utilities : with-html-stream ( quot -- ) stdio get swap with-stream* ; : xhtml-preamble "" write-html "" write-html ; : html-document ( body-quot head-quot -- ) #! head-quot is called to produce output to go #! in the html head portion of the document. #! body-quot is called to produce output to go #! in the html body portion of the document. xhtml-preamble call call ; : default-css ( -- ) ; : simple-html-document ( title quot -- ) swap [ write default-css ] html-document ; : vertical-layout ( list -- ) #! Given a list of HTML components, arrange them vertically. [ ] each
call
; : horizontal-layout ( list -- ) #! Given a list of HTML components, arrange them horizontally. [ ] each
call
; : button ( label -- ) #! Output an HTML submit button with the given label. ; : paragraph ( str -- ) #! Output the string as an html paragraph

write

; : simple-page ( title quot -- ) #! Call the quotation, with all output going to the #! body of an html page with the given title. swap write call ; : styled-page ( title stylesheet-quot quot -- ) #! Call the quotation, with all output going to the #! body of an html page with the given title. stylesheet-quot #! is called to generate the required stylesheet. rot write swap call call ;