factor/library/httpd/html.factor

153 lines
3.9 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
2005-04-19 20:28:01 -04:00
USING: generic kernel lists namespaces presentation sequences
stdio streams strings unparser url-encoding ;
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
] ;
: 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.
2005-04-19 20:28:01 -04:00
[
[
dup html-entities assoc [ % ] [ , ] ?ifte
] seq-each
] make-string ;
2004-07-16 02:26:21 -04:00
2004-08-22 16:04:55 -04:00
: >hex-color ( triplet -- hex )
2005-04-19 20:28:01 -04:00
[ CHAR: # , [ >hex 2 CHAR: 0 pad % ] each ] make-string ;
2004-07-16 02:26:21 -04:00
: fg-css, ( color -- )
"color: " , >hex-color , "; " , ;
2004-07-16 02:26:21 -04:00
: bold-css, ( flag -- )
[ "font-weight: bold; " , ] when ;
2004-07-16 02:26:21 -04:00
: italics-css, ( flag -- )
[ "font-style: italic; " , ] 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: " , unparse , "; " , ;
2004-07-16 02:26:21 -04:00
: font-css, ( font -- )
"font-family: " , , "; " , ;
2004-08-22 16:04:55 -04:00
: css-style ( style -- )
[
[
[ "fg" fg-css, ]
[ "bold" bold-css, ]
[ "italics" italics-css, ]
[ "underline" underline-css, ]
[ "size" size-css, ]
[ "font" font-css, ]
] assoc-apply
] make-string ;
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 [
?string-head [ "/" ?string-head drop ] when
] when* "/" ?string-tail drop ;
2004-08-30 20:24:19 -04:00
: file-link-href ( path -- href )
[ "/" , resolve-file-link url-encode , ] make-string ;
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
: browser-link-href ( style -- href )
dup "browser-link-word" swap assoc url-encode
swap "browser-link-vocab" swap assoc url-encode
"responder" get url-encode
[ "/responder/" , , "/?vocab=" , , "&word=" , , ] make-string ;
: browser-link-tag ( style quot -- style )
over "browser-link-word" swap assoc [
<a href= over browser-link-href 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
TUPLE: html-stream ;
2004-11-28 21:56:58 -05:00
2005-02-14 22:15:02 -05:00
M: html-stream stream-write-attr ( str style stream -- )
wrapper-stream-scope [
[
[
[
[ drop chars>entities write ] span-tag
] file-link-tag
] icon-tag
] browser-link-tag
] bind ;
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:
#!
#! 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
[ >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 ;