factor/libs/httpd/html.factor

207 lines
5.5 KiB
Factor
Raw Permalink Normal View History

! Copyright (C) 2004, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: callback-responder generic hashtables help http tools
2006-06-15 01:21:16 -04:00
io kernel math namespaces prototype-js sequences strings styles
words xml ;
IN: html
2004-07-16 02:26:21 -04:00
: hex-color, ( triplet -- )
3 head-slice
[ 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?
2006-06-26 03:07:57 -04:00
"font-style: " % "italic" "normal" ? % "; " %
{ bold bold-italic } member?
2006-06-26 03:07:57 -04:00
"font-weight: " % "bold" "normal" ? % "; " % ;
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, ( -- )
2006-12-19 21:12:50 -05:00
"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 ;
: 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 ;
2006-12-19 21:12:50 -05:00
M: pathname browser-link-href pathname-string url-encode ;
2006-06-15 01:21:16 -04:00
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 ;
: with-html-style ( quot style stream -- )
[ [ swap span-tag ] object-link-tag ] with-stream* ; inline
M: html-stream with-stream-style ( quot style stream -- )
[ drop call ] -rot with-html-style ;
M: html-stream stream-format ( str style stream -- )
[ do-escaping stdio get delegate-write ] -rot
with-html-style ;
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
[
2006-06-26 03:07:57 -04:00
<div "padding-left: 20px; " =style div>
with-html-stream
</div>
2006-06-15 01:21:16 -04:00
] curry ;
2006-01-23 20:05:39 -05:00
: html-outliner ( caption contents -- )
"+ " get-random-id dup >r
rot make-outliner-quot updating-anchor call
2006-06-26 03:07:57 -04:00
<span r> =id "display: none; " =style 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* ;
2006-06-26 03:07:57 -04:00
: border-spacing-css,
"padding: " % first2 max 2 /i # "px; " % ;
: table-style ( style -- str )
[
H{
{ table-border [ border-css, ] }
{ table-gap [ border-spacing-css, ] }
} hash-apply
] "" make ;
: table-attrs ( style -- )
table-style " border-collapse: collapse;" append =style ;
2006-06-15 01:21:16 -04:00
M: html-stream with-stream-table ( grid quot style stream -- )
[
2006-06-26 03:07:57 -04:00
<table dup table-attrs table> rot [
2006-06-15 01:21:16 -04:00
<tr> [
2006-06-26 03:07:57 -04:00
<td "top" =valign over table-style =style td>
pick H{ } swap with-nesting
</td>
2006-06-15 01:21:16 -04:00
] each </tr>
] each 2drop </table>
] with-stream* ;
M: html-stream stream-terpri [ <br/> ] with-stream* ;
2004-07-16 02:26:21 -04:00
: default-css ( -- )
2006-10-19 17:28:58 -04:00
<link
"stylesheet" =rel "text/css" =type
"/responder/resources/stylesheet.css" =href
link/> ;
2004-07-16 02:26:21 -04:00
2006-01-23 21:03:22 -05:00
: xhtml-preamble
2006-11-14 16:59:20 -05:00
"<?xml version=\"1.0\"?>" write-html
2006-10-19 17:28:58 -04:00
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">" write-html ;
2006-01-23 21:03:22 -05:00
2004-07-16 02:26:21 -04:00
: html-document ( title quot -- )
2006-01-23 21:03:22 -05:00
xhtml-preamble
2006-05-25 00:06:50 -04:00
swap chars>entities
2006-10-19 17:28:58 -04:00
<html " xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\"" write-html html>
<head>
<title> write </title>
default-css
include-prototype-js
</head>
<body>
call
</body>
</html> ;
2004-07-16 02:26:21 -04:00
: simple-html-document ( title quot -- )
swap [ <pre> with-html-stream </pre> ] html-document ;