factor/library/httpd/html.factor

157 lines
3.8 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 http io kernel lists math namespaces parser
presentation sequences strings styles words ;
2004-07-16 02:26:21 -04:00
: html-entities ( -- alist )
[
[[ CHAR: < "&lt;" ]]
[[ CHAR: > "&gt;" ]]
[[ CHAR: & "&amp;" ]]
[[ CHAR: ' "&apos;" ]]
[[ CHAR: " "&quot;" ]]
2004-07-16 02:26:21 -04:00
] ;
: chars>entities ( str -- str )
#! Convert <, >, &, ' and " to HTML entities.
2005-04-19 20:28:01 -04:00
[
[ dup html-entities assoc [ % ] [ , ] ?ifte ] each
2005-08-25 15:27:38 -04:00
] "" make ;
2004-07-16 02:26:21 -04:00
: hex-color, ( triplet -- )
[ >hex 2 CHAR: 0 pad-left % ] each ;
2004-07-16 02:26:21 -04:00
: fg-css, ( color -- )
"color: #" % hex-color, "; " % ;
2004-07-16 02:26:21 -04:00
: style-css, ( flag -- )
2005-07-16 22:16:18 -04:00
dup [ italic bold-italic ] member?
[ "font-style: italic; " % ] when
2005-07-16 22:16:18 -04:00
[ bold bold-italic ] member?
[ "font-weight: bold; " % ] when ;
2004-07-16 02:26:21 -04:00
: underline-css, ( flag -- )
[ "text-decoration: underline; " % ] when ;
2004-07-16 02:26:21 -04:00
: size-css, ( size -- )
"font-size: " % # "; " % ;
2004-07-16 02:26:21 -04:00
: font-css, ( font -- )
"font-family: " % % "; " % ;
2004-08-22 16:04:55 -04:00
: css-style ( style -- )
[
[
[ foreground fg-css, ]
[ font font-css, ]
[ font-style style-css, ]
[ font-size size-css, ]
[ underline underline-css, ]
] assoc-apply
2005-08-25 15:27:38 -04:00
] "" make ;
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 [
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 -- )
over file swap assoc [
<a href= file-link-href a> call </a>
] [
call
] ifte* ;
2004-08-30 20:24:19 -04:00
: browser-link-href ( word -- href )
dup word-name swap word-vocabulary
[
"/responder/browser/?vocab=" %
url-encode %
"&word=" %
url-encode %
2005-08-25 15:27:38 -04:00
] "" make ;
: browser-link-tag ( style quot -- style )
over presented swap assoc dup word? [
<a href= browser-link-href a> call </a>
] [
drop call
] ifte ;
: icon-tag ( string style quot -- )
over icon swap assoc dup [
2005-05-18 16:26:22 -04:00
<img src= "/responder/resource/" swap append img/>
#! Ignore the quotation, since no further style
#! can be applied
3drop
] [
drop call
] ifte ;
2004-08-22 16:04:55 -04:00
TUPLE: html-stream ;
2004-11-28 21:56:58 -05:00
M: html-stream stream-write1 ( char stream -- )
[
dup html-entities assoc [ write ] [ write1 ] ?ifte
] with-wrapper ;
M: html-stream stream-format ( str style stream -- )
2005-05-03 04:40:13 -04:00
[
[
[
[
[ drop chars>entities write ] span-tag
] file-link-tag
] icon-tag
] browser-link-tag
2005-05-03 04:40:13 -04:00
] 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
2004-07-16 02:26:21 -04:00
#! underline
#! icon
#! 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 ;