factor/contrib/httpd/html.factor

213 lines
5.6 KiB
Factor
Raw Permalink Normal View History

! Copyright (C) 2004, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2006-01-23 20:05:39 -05:00
USING: cont-responder generic hashtables help http inspector io
kernel lists prototype-js math namespaces sequences strings
2006-01-23 20:05:39 -05:00
styles words xml ;
IN: html
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
2006-01-23 18:01:46 -05:00
: border-css, ( border -- )
"border: 1px solid #" % hex-color, "; " % ;
: padding-css, ( padding -- ) "padding: " % # "px; " % ;
: pre-css, ( -- )
"white-space: pre; font-family:monospace; " % ;
2006-01-23 18:01:46 -05:00
: div-css-style ( style -- str )
[
H{
2006-01-23 18:01:46 -05:00
{ page-color [ bg-css, ] }
{ border-color [ border-css, ] }
{ border-width [ padding-css, ] }
{ wrap-margin [ [ pre-css, ] unless ] }
} hash-apply
] "" make ;
: div-tag ( style quot -- )
2006-01-23 18:01:46 -05:00
swap 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
: do-escaping ( string style -- string )
html swap hash [ chars>entities ] unless ;
GENERIC: browser-link-href ( presented -- href )
2006-01-23 18:01:46 -05:00
M: object browser-link-href drop f ;
M: word browser-link-href
2006-01-23 20:05:39 -05:00
"/responder/browser/" swap [
2006-01-23 18:01:46 -05:00
dup word-vocabulary "vocab" set word-name "word" set
] make-hash build-url ;
M: link browser-link-href
link-name [ \ f ] unless* dup word? [
browser-link-href
] [
2006-01-23 20:05:39 -05:00
"/responder/help/" swap "topic" associate build-url
2005-09-24 15:21:17 -04:00
] if ;
2006-01-23 18:01:46 -05:00
: object-link-tag ( style quot -- )
presented pick hash browser-link-href
[ <a =href a> call </a> ] [ call ] if* ;
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
C: html-stream ( stream -- stream ) [ set-delegate ] keep ;
M: html-stream stream-write1 ( char stream -- )
>r ch>string r> stream-write ;
: delegate-write delegate stream-write ;
M: html-stream stream-write ( str stream -- )
>r chars>entities r> delegate-write ;
M: html-stream stream-format ( str style stream -- )
2005-05-03 04:40:13 -04:00
[
[
[
[
do-escaping stdio get delegate-write
] span-tag
2005-09-01 16:37:32 -04:00
] file-link-tag
2006-01-23 18:01:46 -05:00
] object-link-tag
] with-stream* ;
2004-11-28 21:56:58 -05:00
2006-01-23 23:03:15 -05:00
: with-html-stream ( quot -- )
stdio get <html-stream> swap with-stream* ;
: make-outliner-quot
[
<div "padding-left:10px;" =style div>
with-html-stream
</div>
] curry [ , \ show-final , ] [ ] make ;
2006-01-23 20:05:39 -05:00
: html-outliner ( caption contents -- )
"+ " get-random-id dup >r
rot make-outliner-quot updating-anchor call
<span r> =id span> </span> ;
2006-01-23 20:05:39 -05:00
: outliner-tag ( style quot -- )
outline pick hash [ html-outliner ] [ call ] if* ;
M: html-stream with-nested-stream ( quot style stream -- )
[
[
[
2006-01-23 20:05:39 -05:00
[
stdio get <nested-stream> swap with-stream*
] div-tag
] object-link-tag
] outliner-tag
] with-stream* ;
M: html-stream stream-terpri [ <br/> ] with-stream* ;
2004-07-16 02:26:21 -04:00
: default-css ( -- )
<style "text/css" =type style>
2006-01-23 18:01:46 -05:00
"A:link { text-decoration: none; color: black; }" print
"A:visited { text-decoration: none; color: black; }" print
"A:active { text-decoration: none; color: black; }" print
"A:hover, A:hover { text-decoration: none; color: black; }" print
</style> ;
2004-07-16 02:26:21 -04:00
2006-01-23 21:03:22 -05:00
: xhtml-preamble
"<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>" print
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" print ;
2004-07-16 02:26:21 -04:00
: html-document ( title quot -- )
2006-01-23 21:03:22 -05:00
xhtml-preamble
swap chars>entities dup
<html>
<head>
<title> write </title>
default-css
include-prototype-js
</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 ;