factor/contrib/httpd/html.factor

197 lines
5.0 KiB
Factor
Raw Normal View History

! Copyright (C) 2004, 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
2004-07-16 02:26:21 -04:00
IN: html
USING: generic hashtables help http io kernel lists math
namespaces sequences strings styles words xml ;
2004-07-16 02:26:21 -04:00
: hex-color, ( triplet -- )
3 swap head [ 255 * >fixnum >hex 2 CHAR: 0 pad-left % ] each ;
2004-07-16 02:26:21 -04:00
: fg-css, ( color -- ) "color: #" % hex-color, "; " % ;
: bg-css, ( color -- ) "background-color: #" % hex-color, "; " % ;
2004-07-16 02:26:21 -04:00
: style-css, ( flag -- )
dup
{ italic bold-italic } member?
[ "font-style: italic; " % ] when
{ bold bold-italic } member?
[ "font-weight: bold; " % ] when ;
2004-07-16 02:26:21 -04:00
: size-css, ( size -- )
"font-size: " % # "pt; " % ;
2004-07-16 02:26:21 -04:00
: font-css, ( font -- )
"font-family: " % % "; " % ;
2004-08-22 16:04:55 -04:00
2005-11-29 23:49:59 -05:00
: hash-apply ( value-hash quot-hash -- )
#! Looks up the key of each pair in the first list in the
#! second list to produce a quotation. The quotation is
#! applied to the value of the pair. If there is no
#! corresponding quotation, the value is popped off the
#! stack.
swap [
2005-11-29 23:49:59 -05:00
swap rot hash dup [ call ] [ 2drop ] if
] hash-each-with ;
: span-css-style ( style -- str )
[
2005-11-29 23:49:59 -05:00
H{
{ foreground [ fg-css, ] }
{ background [ bg-css, ] }
2005-11-29 23:49:59 -05:00
{ font [ font-css, ] }
{ font-style [ style-css, ] }
{ font-size [ size-css, ] }
} hash-apply
2005-08-25 15:27:38 -04:00
] "" make ;
2004-07-16 02:26:21 -04:00
: span-tag ( style quot -- )
over span-css-style dup empty? [
drop call
] [
<span =style span> call </span>
2005-09-24 15:21:17 -04:00
] if ;
2004-08-30 20:24:19 -04:00
: div-css-style ( style -- str )
drop "" ;
! [
! H{
! { foreground [ fg-css, ] }
! { font [ font-css, ] }
! { font-style [ style-css, ] }
! { font-size [ size-css, ] }
! } hash-apply
! ] "" make ;
: div-tag ( style quot -- )
over div-css-style dup empty? [
drop call
] [
<div =style div> call </div>
] if ;
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 [
2005-05-18 16:26:22 -04:00
?head [ "/" ?head drop ] when
] when* "/" ?tail drop ;
2004-08-30 20:24:19 -04:00
: file-link-href ( path -- href )
2005-08-25 15:27:38 -04:00
[ "/" % resolve-file-link url-encode % ] "" make ;
2004-08-30 20:24:19 -04:00
: file-link-tag ( style quot -- )
2005-11-29 23:49:59 -05:00
over file swap hash [
<a file-link-href =href a> call </a>
] [
call
2005-09-24 15:21:17 -04:00
] if* ;
2004-08-30 20:24:19 -04:00
GENERIC: browser-link-href ( presented -- href )
M: word browser-link-href
dup word-name swap word-vocabulary [
"/responder/browser/?vocab=" %
url-encode %
"&word=" %
url-encode %
2005-08-25 15:27:38 -04:00
] "" make ;
M: link browser-link-href
link-name [ \ f ] unless* dup word? [
browser-link-href
] [
[ "/responder/help/" % url-encode % ] "" make
2005-09-24 15:21:17 -04:00
] if ;
M: object browser-link-href
drop f ;
: browser-link-tag ( style quot -- style )
presented pick hash browser-link-href
[ <a =href a> call </a> ] [ call ] if* ;
TUPLE: wrapper-stream scope ;
C: wrapper-stream ( stream -- stream )
2dup set-delegate [
>r stdio associate r> set-wrapper-stream-scope
] keep ;
: with-wrapper ( stream quot -- )
>r wrapper-stream-scope r> bind ; inline
TUPLE: nested-stream ;
C: nested-stream [ set-delegate ] keep ;
M: nested-stream stream-close drop ;
TUPLE: html-stream ;
2004-11-28 21:56:58 -05:00
M: html-stream stream-write1 ( char stream -- )
>r ch>string r> stream-write ;
M: html-stream stream-write ( char stream -- )
[ chars>entities write ] with-wrapper ;
M: html-stream stream-format ( str style stream -- )
2005-05-03 04:40:13 -04:00
[
[
[
2005-09-01 16:37:32 -04:00
[ drop chars>entities write ] span-tag
] file-link-tag
] browser-link-tag
2005-05-03 04:40:13 -04:00
] with-wrapper ;
2004-11-28 21:56:58 -05:00
: pre-tag ( stream style quot -- )
wrap-margin rot hash [
call
] [
over [ [ <pre> ] with-wrapper call ] keep
[ </pre> ] with-wrapper
] if ;
M: html-stream with-nested-stream ( quot style stream -- )
swap [
[ <nested-stream> swap with-stream ] pre-tag
] div-tag ;
M: html-stream stream-terpri [ <br/> ] with-wrapper ;
M: html-stream stream-terpri* [ <br/> ] with-wrapper ;
2004-11-28 21:56:58 -05:00
C: html-stream ( stream -- stream )
2004-07-16 02:26:21 -04:00
#! 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:
#!
#! foreground - an rgb triplet in a list
#! background - an rgb triplet in a list
#! font
#! font-style
#! font-size
#! file
#! word
#! vocab
[ >r <wrapper-stream> r> set-delegate ] keep ;
2004-07-16 02:26:21 -04:00
: with-html-stream ( quot -- )
[ stdio [ <html-stream> ] change 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 ;