2005-01-30 15:57:25 -05:00
|
|
|
! 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-08-31 21:06:13 -04:00
|
|
|
USING: generic http io kernel lists math namespaces parser
|
2005-08-21 20:50:14 -04:00
|
|
|
presentation sequences strings styles words ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: html-entities ( -- alist )
|
|
|
|
[
|
2005-01-13 19:49:47 -05:00
|
|
|
[[ CHAR: < "<" ]]
|
|
|
|
[[ CHAR: > ">" ]]
|
|
|
|
[[ CHAR: & "&" ]]
|
|
|
|
[[ CHAR: ' "'" ]]
|
|
|
|
[[ CHAR: " """ ]]
|
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
|
|
|
[
|
2005-05-14 17:18:45 -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
|
|
|
|
2005-07-11 22:47:38 -04:00
|
|
|
: hex-color, ( triplet -- )
|
|
|
|
[ >hex 2 CHAR: 0 pad-left % ] each ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-11-11 15:15:43 -05:00
|
|
|
: fg-css, ( color -- )
|
2005-07-11 22:47:38 -04:00
|
|
|
"color: #" % hex-color, "; " % ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-07-11 22:47:38 -04:00
|
|
|
: style-css, ( flag -- )
|
2005-07-16 22:16:18 -04:00
|
|
|
dup [ italic bold-italic ] member?
|
2005-07-11 22:47:38 -04:00
|
|
|
[ "font-style: italic; " % ] when
|
2005-07-16 22:16:18 -04:00
|
|
|
[ bold bold-italic ] member?
|
2005-07-11 22:47:38 -04:00
|
|
|
[ "font-weight: bold; " % ] when ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-11-11 15:15:43 -05:00
|
|
|
: underline-css, ( flag -- )
|
2005-07-11 22:47:38 -04:00
|
|
|
[ "text-decoration: underline; " % ] when ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-11-11 15:15:43 -05:00
|
|
|
: size-css, ( size -- )
|
2005-08-31 21:06:13 -04:00
|
|
|
"font-size: " % # "; " % ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-11-11 15:15:43 -05:00
|
|
|
: font-css, ( font -- )
|
2005-07-11 22:47:38 -04:00
|
|
|
"font-family: " % % "; " % ;
|
2004-08-22 16:04:55 -04:00
|
|
|
|
2004-09-02 19:38:05 -04:00
|
|
|
: css-style ( style -- )
|
2004-11-11 15:15:43 -05:00
|
|
|
[
|
|
|
|
[
|
2005-07-11 22:47:38 -04:00
|
|
|
[ foreground fg-css, ]
|
|
|
|
[ font font-css, ]
|
|
|
|
[ font-style style-css, ]
|
|
|
|
[ font-size size-css, ]
|
|
|
|
[ underline underline-css, ]
|
2004-11-11 15:15:43 -05:00
|
|
|
] assoc-apply
|
2005-08-25 15:27:38 -04:00
|
|
|
] "" make ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-09-02 19:38:05 -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
|
|
|
|
2004-09-02 19:38:05 -04:00
|
|
|
: file-link-tag ( style quot -- )
|
2005-07-21 23:36:40 -04:00
|
|
|
over file swap assoc [
|
2004-09-02 19:38:05 -04:00
|
|
|
<a href= file-link-href a> call </a>
|
|
|
|
] [
|
|
|
|
call
|
|
|
|
] ifte* ;
|
2004-08-30 20:24:19 -04:00
|
|
|
|
2005-07-11 22:47:38 -04:00
|
|
|
: browser-link-href ( word -- href )
|
|
|
|
dup word-name swap word-vocabulary
|
2005-07-27 20:13:11 -04:00
|
|
|
[
|
|
|
|
"/responder/browser/?vocab=" %
|
|
|
|
url-encode %
|
|
|
|
"&word=" %
|
|
|
|
url-encode %
|
2005-08-25 15:27:38 -04:00
|
|
|
] "" make ;
|
2005-02-14 16:44:15 -05:00
|
|
|
|
|
|
|
: browser-link-tag ( style quot -- style )
|
2005-07-11 22:47:38 -04:00
|
|
|
over presented swap assoc dup word? [
|
|
|
|
<a href= browser-link-href a> call </a>
|
2005-02-14 16:44:15 -05:00
|
|
|
] [
|
2005-07-11 22:47:38 -04:00
|
|
|
drop call
|
2005-02-14 16:44:15 -05:00
|
|
|
] ifte ;
|
|
|
|
|
2004-09-02 19:38:05 -04:00
|
|
|
: icon-tag ( string style quot -- )
|
2005-07-11 22:47:38 -04:00
|
|
|
over icon swap assoc dup [
|
2005-05-18 16:26:22 -04:00
|
|
|
<img src= "/responder/resource/" swap append img/>
|
2004-09-02 19:38:05 -04:00
|
|
|
#! Ignore the quotation, since no further style
|
|
|
|
#! can be applied
|
|
|
|
3drop
|
|
|
|
] [
|
|
|
|
drop call
|
|
|
|
] ifte ;
|
2004-08-22 16:04:55 -04:00
|
|
|
|
2005-03-08 22:54:59 -05:00
|
|
|
TUPLE: html-stream ;
|
2004-11-28 21:56:58 -05:00
|
|
|
|
2005-07-17 14:48:55 -04:00
|
|
|
M: html-stream stream-write1 ( char stream -- )
|
|
|
|
[
|
|
|
|
dup html-entities assoc [ write ] [ write1 ] ?ifte
|
|
|
|
] with-wrapper ;
|
|
|
|
|
2005-07-21 21:43:37 -04:00
|
|
|
M: html-stream stream-format ( str style stream -- )
|
2005-05-03 04:40:13 -04:00
|
|
|
[
|
2004-09-02 19:38:05 -04:00
|
|
|
[
|
|
|
|
[
|
2005-02-14 16:44:15 -05: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:
|
|
|
|
#!
|
2005-07-11 22:47:38 -04:00
|
|
|
#! 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
|
2005-05-02 00:18:34 -04:00
|
|
|
#! icon
|
|
|
|
#! file
|
|
|
|
#! word
|
|
|
|
#! vocab
|
2005-03-08 22:54:59 -05:00
|
|
|
[ >r <wrapper-stream> r> set-delegate ] keep ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: with-html-stream ( quot -- )
|
2004-12-04 15:10:46 -05:00
|
|
|
[ stdio [ <html-stream> ] change call ] with-scope ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: html-document ( title quot -- )
|
2004-09-02 19:38:05 -04:00
|
|
|
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 -- )
|
2004-09-02 19:38:05 -04:00
|
|
|
swap [ <pre> with-html-stream </pre> ] html-document ;
|