factor/library/httpd/html.factor

145 lines
4.2 KiB
Factor
Raw Normal View History

! :folding=indent:collapseFolds=1:
2004-07-16 02:26:21 -04:00
! $Id$
!
! Copyright (C) 2004 Slava Pestov.
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
! this list of conditions and the following disclaimer in the documentation
! and/or other materials provided with the distribution.
!
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IN: html
USE: combinators
USE: format
USE: lists
USE: logic
2004-08-21 02:55:37 -04:00
USE: kernel
2004-07-16 02:26:21 -04:00
USE: namespaces
USE: stack
USE: stdio
USE: streams
USE: strings
USE: unparser
USE: url-encoding
: html-entities ( -- alist )
[
[ CHAR: < | "&lt;" ]
[ CHAR: > | "&gt;" ]
[ CHAR: & | "&amp;" ]
[ CHAR: ' | "&apos;" ]
[ CHAR: " | "&quot;" ]
2004-07-16 02:26:21 -04:00
] ;
: char>entity ( ch -- str )
dup >r html-entities assoc dup r> ? ;
2004-07-16 02:26:21 -04:00
: chars>entities ( str -- str )
#! Convert <, >, &, ' and " to HTML entities.
[ dup html-entities assoc dup rot ? ] str-map ;
: opening-tag ( tag attrs -- )
"<" % swap % [ " " % % ] when* ">" % ;
: closing-tag ( tag -- )
"</" % % ">" % ;
: html-tag ( str tag attrs -- str )
#! Wrap a string in an HTML tag.
<% dupd opening-tag swap % closing-tag %> ;
: link-attrs ( link -- attrs )
2004-08-12 02:13:43 -04:00
<% "href=\"/inspect/" % % "\"" % %> ;
2004-07-16 02:26:21 -04:00
: link-tag ( string link -- string )
2004-08-21 02:55:37 -04:00
url-encode "a" swap link-attrs html-tag ;
2004-07-16 02:26:21 -04:00
: bold-tag ( string -- string )
"b" f html-tag ;
: italics-tag ( string -- string )
"i" f html-tag ;
: underline-tag ( string -- string )
"u" f html-tag ;
: >hex-color ( triplet -- hex )
[ >hex 2 digits ] inject "#" swons cat ;
: fg-tag ( string color -- string )
"font" swap "color=\"" swap >hex-color "\"" cat3 html-tag ;
: size-tag ( string size -- string )
"font" swap "size=\"" swap "\"" cat3 html-tag ;
2004-08-21 02:55:37 -04:00
: html-attr-string ( string style -- string )
[
[ "fg" fg-tag ]
[ "bold" drop bold-tag ]
[ "italics" drop italics-tag ]
[ "underline" drop underline-tag ]
[ "size" size-tag ]
[ "link" link-tag ]
] assoc-each ;
2004-07-16 02:26:21 -04:00
2004-08-21 02:55:37 -04:00
: html-write-attr ( string style stream -- )
rot chars>entities rot html-attr-string swap fwrite ;
2004-07-16 02:26:21 -04:00
: <html-stream> ( stream -- stream )
#! Wraps the given stream in an HTML stream. An HTML stream
#! converts special characters to entities when being
#! written, and supports writing attributed strings with
#! the following attributes:
#!
#! link - an object path
#! fg - an rgb triplet in a list
#! bg - an rgb triplet in a list
#! bold
#! italic
#! underline
<extend-stream> [
[ chars>entities "stream" get fwrite ] "fwrite" set
[ chars>entities "stream" get fprint ] "fprint" set
2004-08-21 02:55:37 -04:00
[ "stream" get html-write-attr ] "fwrite-attr" set
2004-07-16 02:26:21 -04:00
] extend ;
: with-html-stream ( quot -- )
2004-08-10 23:48:08 -04:00
[
"stdio" get <html-stream> "stdio" set call
] with-scope ;
2004-07-16 02:26:21 -04:00
: html-head ( title -- )
"<html><head><title>" write
dup write
"</title></head><body><h1>" write write "</h1>" write ;
: html-tail ( -- ) "</body></html>" print ;
: html-document ( title quot -- )
swap chars>entities html-head call html-tail ;
: preformatted-html ( quot -- )
"<pre>" print call "</pre>" print ;
: simple-html-document ( title quot -- )
swap [
[ [ call ] with-html-stream ] preformatted-html
] html-document ;