2006-01-21 02:37:39 -05:00
|
|
|
! Copyright (C) 2004, 2006 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2004-07-16 02:26:21 -04:00
|
|
|
IN: html
|
2006-01-20 01:26:50 -05:00
|
|
|
USING: generic hashtables help http io kernel lists math
|
|
|
|
namespaces sequences strings styles words xml ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-07-11 22:47:38 -04:00
|
|
|
: hex-color, ( triplet -- )
|
2006-01-21 02:37:39 -05:00
|
|
|
3 swap head
|
|
|
|
[ 255 * >fixnum >hex 2 CHAR: 0 pad-left % ] each ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2006-01-21 02:37:39 -05:00
|
|
|
: fg-css, ( color -- )
|
|
|
|
"color: #" % hex-color, "; " % ;
|
2006-01-20 01:26:50 -05:00
|
|
|
|
2006-01-21 02:37:39 -05:00
|
|
|
: bg-css, ( color -- )
|
|
|
|
"background-color: #" % hex-color, "; " % ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-07-11 22:47:38 -04:00
|
|
|
: style-css, ( flag -- )
|
2006-01-20 01:26:50 -05:00
|
|
|
dup
|
|
|
|
{ italic bold-italic } member?
|
2005-07-11 22:47:38 -04:00
|
|
|
[ "font-style: italic; " % ] when
|
2006-01-20 01:26:50 -05: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
|
|
|
: size-css, ( size -- )
|
2006-01-20 01:26:50 -05:00
|
|
|
"font-size: " % # "pt; " % ;
|
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
|
|
|
|
2005-11-29 23:49:59 -05:00
|
|
|
: hash-apply ( value-hash quot-hash -- )
|
2005-10-29 16:53:47 -04:00
|
|
|
#! 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 ;
|
2005-10-29 16:53:47 -04:00
|
|
|
|
2006-01-20 01:26:50 -05:00
|
|
|
: span-css-style ( style -- str )
|
2004-11-11 15:15:43 -05:00
|
|
|
[
|
2005-11-29 23:49:59 -05:00
|
|
|
H{
|
|
|
|
{ foreground [ fg-css, ] }
|
2006-01-20 01:26:50 -05:00
|
|
|
{ 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
|
|
|
|
2004-09-02 19:38:05 -04:00
|
|
|
: span-tag ( style quot -- )
|
2006-01-20 01:26:50 -05:00
|
|
|
over span-css-style dup empty? [
|
2004-09-02 19:38:05 -04:00
|
|
|
drop call
|
|
|
|
] [
|
2005-09-22 21:01:55 -04:00
|
|
|
<span =style span> call </span>
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ;
|
2004-08-30 20:24:19 -04:00
|
|
|
|
2006-01-20 01:26:50 -05:00
|
|
|
: div-css-style ( style -- str )
|
2006-01-21 02:37:39 -05:00
|
|
|
[
|
|
|
|
H{
|
|
|
|
{ page-color [ bg-css, ] }
|
|
|
|
! { border-color [ font-css, ] }
|
|
|
|
} hash-apply
|
|
|
|
] "" make ;
|
2006-01-20 01:26:50 -05:00
|
|
|
|
|
|
|
: div-tag ( style quot -- )
|
|
|
|
over 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
|
|
|
|
2004-09-02 19:38:05 -04:00
|
|
|
: file-link-tag ( style quot -- )
|
2005-11-29 23:49:59 -05:00
|
|
|
over file swap hash [
|
2005-09-22 21:01:55 -04:00
|
|
|
<a file-link-href =href a> call </a>
|
2004-09-02 19:38:05 -04:00
|
|
|
] [
|
|
|
|
call
|
2005-09-24 15:21:17 -04:00
|
|
|
] if* ;
|
2004-08-30 20:24:19 -04:00
|
|
|
|
2006-01-21 02:37:39 -05:00
|
|
|
: do-escaping ( string style -- string )
|
|
|
|
html swap hash [ chars>entities ] unless ;
|
|
|
|
|
2006-01-20 01:26:50 -05:00
|
|
|
GENERIC: browser-link-href ( presented -- href )
|
|
|
|
|
|
|
|
M: word browser-link-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
|
|
|
|
2006-01-20 01:26:50 -05:00
|
|
|
M: link browser-link-href
|
|
|
|
link-name [ \ f ] unless* dup word? [
|
|
|
|
browser-link-href
|
2005-02-14 16:44:15 -05:00
|
|
|
] [
|
2006-01-20 01:26:50 -05:00
|
|
|
[ "/responder/help/" % url-encode % ] "" make
|
2005-09-24 15:21:17 -04:00
|
|
|
] if ;
|
2005-02-14 16:44:15 -05:00
|
|
|
|
2006-01-20 01:26:50 -05:00
|
|
|
M: object browser-link-href
|
|
|
|
drop f ;
|
|
|
|
|
|
|
|
: browser-link-tag ( style quot -- style )
|
|
|
|
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 ;
|
|
|
|
|
2005-03-08 22:54:59 -05:00
|
|
|
TUPLE: html-stream ;
|
2004-11-28 21:56:58 -05:00
|
|
|
|
2006-01-21 02:37:39 -05:00
|
|
|
C: html-stream ( stream -- stream ) [ set-delegate ] keep ;
|
|
|
|
|
2005-07-17 14:48:55 -04:00
|
|
|
M: html-stream stream-write1 ( char stream -- )
|
2006-01-20 01:26:50 -05:00
|
|
|
>r ch>string r> stream-write ;
|
|
|
|
|
2006-01-21 02:37:39 -05:00
|
|
|
: delegate-write delegate stream-write ;
|
|
|
|
|
|
|
|
M: html-stream stream-write ( str stream -- )
|
|
|
|
>r chars>entities r> delegate-write ;
|
2005-07-17 14:48:55 -04:00
|
|
|
|
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
|
|
|
[
|
|
|
|
[
|
2006-01-21 02:37:39 -05:00
|
|
|
[
|
|
|
|
do-escaping stdio get delegate-write
|
|
|
|
] span-tag
|
2005-09-01 16:37:32 -04:00
|
|
|
] file-link-tag
|
2005-02-14 16:44:15 -05:00
|
|
|
] browser-link-tag
|
2006-01-21 02:37:39 -05:00
|
|
|
] with-stream* ;
|
2004-11-28 21:56:58 -05:00
|
|
|
|
2006-01-21 02:37:39 -05:00
|
|
|
: pre-tag ( style quot -- )
|
2006-01-20 01:26:50 -05:00
|
|
|
wrap-margin rot hash [
|
|
|
|
call
|
|
|
|
] [
|
2006-01-21 02:37:39 -05:00
|
|
|
<pre> call </pre>
|
2006-01-20 01:26:50 -05:00
|
|
|
] if ;
|
|
|
|
|
|
|
|
M: html-stream with-nested-stream ( quot style stream -- )
|
2006-01-21 02:37:39 -05:00
|
|
|
[
|
|
|
|
[
|
|
|
|
[
|
|
|
|
stdio get <nested-stream> swap with-stream*
|
|
|
|
] pre-tag
|
|
|
|
] div-tag
|
|
|
|
] with-stream* ;
|
|
|
|
|
|
|
|
M: html-stream stream-terpri [ <br/> ] with-stream* ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: with-html-stream ( quot -- )
|
2006-01-21 02:37:39 -05:00
|
|
|
stdio get <html-stream> swap with-stream* ;
|
|
|
|
|
|
|
|
: default-css ( -- )
|
|
|
|
<style>
|
|
|
|
"A:link { text-decoration:none}" print
|
|
|
|
"A:visited { text-decoration:none}" print
|
|
|
|
"A:active { text-decoration:none}" print
|
|
|
|
"A:hover, A.nav:hover { border: 1px solid black; text-decoration: none; margin: -1px }" print
|
|
|
|
</style> ;
|
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>
|
2006-01-21 02:37:39 -05:00
|
|
|
default-css
|
2004-09-02 19:38:05 -04:00
|
|
|
</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 ;
|