factor/library/httpd/html.factor

180 lines
4.9 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 ;
2004-08-22 16:04:55 -04:00
: >hex-color ( triplet -- hex )
2004-08-22 19:06:51 -04:00
[ >hex 2 digits ] map "#" swons cat ;
2004-07-16 02:26:21 -04:00
2004-08-22 16:04:55 -04:00
: fg-css% ( color -- )
"color: " % >hex-color % "; " % ;
2004-07-16 02:26:21 -04:00
2004-08-22 16:04:55 -04:00
: bold-css% ( flag -- )
[ "font-weight: bold; " % ] when ;
2004-07-16 02:26:21 -04:00
2004-08-22 16:04:55 -04:00
: italics-css% ( flag -- )
[ "font-style: italic; " % ] when ;
2004-07-16 02:26:21 -04:00
2004-08-22 16:04:55 -04:00
: underline-css% ( flag -- )
[ "text-decoration: underline; " % ] when ;
2004-07-16 02:26:21 -04:00
2004-08-22 16:04:55 -04:00
: size-css% ( size -- )
"font-size: " % unparse % "; " % ;
2004-07-16 02:26:21 -04:00
2004-08-22 16:04:55 -04:00
: font-css% ( font -- )
"font-family: " % % "; " % ;
: css-style ( style -- )
<% [
2004-08-22 16:04:55 -04:00
[ "fg" fg-css% ]
[ "bold" bold-css% ]
[ "italics" italics-css% ]
[ "underline" underline-css% ]
[ "size" size-css% ]
[ "font" font-css% ]
] assoc-apply %> ;
2004-07-16 02:26:21 -04:00
: span-tag ( style quot -- )
over css-style dup "" = [
drop call
] [
<span style= span> call </span>
] ifte ;
2004-08-30 20:24:19 -04:00
: resolve-file-link ( path -- link )
#! The file responder needs relative links not absolute
#! links.
"doc-root" get [
?str-head [ "/" ?str-head drop ] when
] when* "/" ?str-tail drop ;
: file-link-href ( path -- href )
2004-10-04 21:51:57 -04:00
<% "/" % resolve-file-link url-encode % %> ;
2004-08-30 20:24:19 -04:00
: file-link-tag ( style quot -- )
over "file-link" swap assoc [
<a href= file-link-href a> call </a>
] [
call
] ifte* ;
2004-08-30 20:24:19 -04:00
: object-link-href ( path -- href )
2004-09-23 16:50:30 -04:00
#! Perhaps this should not be hard-coded.
"/responder/inspect/" swap cat2 ;
: object-link-tag ( style quot -- )
over "object-link" swap assoc [
<a href= object-link-href url-encode a> call </a>
] [
call
] ifte* ;
: icon-tag ( string style quot -- )
over "icon" swap assoc dup [
2004-09-23 16:50:30 -04:00
<img src= "/responder/resource/" swap cat2 img/>
#! Ignore the quotation, since no further style
#! can be applied
3drop
] [
drop call
] ifte ;
2004-08-22 16:04:55 -04:00
: html-write-attr ( string style -- )
[
[
[
[ drop chars>entities write ] span-tag
] file-link-tag
] object-link-tag
] icon-tag ;
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
2004-08-22 16:04:55 -04:00
#! italics
2004-07-16 02:26:21 -04:00
#! underline
2004-08-22 16:04:55 -04:00
#! size
#! link - an object path
2004-07-16 02:26:21 -04:00
<extend-stream> [
[ chars>entities write ] "fwrite" set
[ chars>entities print ] "fprint" set
[ html-write-attr ] "fwrite-attr" set
2004-07-16 02:26:21 -04:00
] extend ;
: with-html-stream ( quot -- )
[ "stdio" get <html-stream> "stdio" set call ] with-scope ;
2004-07-16 02:26:21 -04:00
: html-document ( title quot -- )
swap chars>entities dup
<html>
<head>
<title> write </title>
</head>
<body>
<h1> write </h1>
call
</body>
</html> ;
2004-07-16 02:26:21 -04:00
: simple-html-document ( title quot -- )
swap [ <pre> with-html-stream </pre> ] html-document ;