1 !:folding=indent:collapseFolds=1:
5 ! Copyright (C) 2004 Slava Pestov.
7 ! Redistribution and use in source and binary forms, with or without
8 ! modification, are permitted provided that the following conditions are met:
10 ! 1. Redistributions of source code must retain the above copyright notice,
11 ! this list of conditions and the following disclaimer.
13 ! 2. Redistributions in binary form must reproduce the above copyright notice,
14 ! this list of conditions and the following disclaimer in the documentation
15 ! and/or other materials provided with the distribution.
17 ! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 ! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 ! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 ! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 ! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 ! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23 ! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 ! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 ! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 : html-entities ( -- alist )
50 : chars>entities ( str -- str )
51 #! Convert <, >, &, ' and " to HTML entities.
52 [ dup html-entities assoc dup rot ? ] str-map ;
54 : opening-tag ( tag attrs -- )
55 "<" % swap % [ " " % % ] when* ">" % ;
57 : closing-tag ( tag -- )
60 : html-tag ( str tag attrs -- str )
61 #! Wrap a string in an HTML tag.
62 <% dupd opening-tag swap % closing-tag %> ;
64 : responder-link% ( -- )
65 "/" % "responder" get % "/" % ;
67 : link-attrs ( link -- attrs )
68 <% "href=\"" % responder-link% % "\"" % %> ;
70 : link-tag ( string link -- string )
71 "a" swap link-attrs html-tag ;
73 : bold-tag ( string -- string )
76 : italics-tag ( string -- string )
79 : underline-tag ( string -- string )
82 : >hex-color ( triplet -- hex )
83 [ >hex 2 digits ] inject "#" swons cat ;
85 : fg-tag ( string color -- string )
86 "font" swap "color=\"" swap >hex-color "\"" cat3 html-tag ;
88 : size-tag ( string size -- string )
89 "font" swap "size=\"" swap "\"" cat3 html-tag ;
91 : html-attr-string ( string -- string )
93 "fg" get [ fg-tag ] when*
94 "bold" get [ bold-tag ] when
95 "italics" get [ italics-tag ] when
96 "underline" get [ underline-tag ] when
97 "size" get [ size-tag ] when*
98 "link" get [ url-encode link-tag ] when* ;
100 : <html-stream>/fwrite-attr ( string stream -- )
101 [ html-attr-string ] dip fwrite ;
103 : <html-stream> ( stream -- stream )
104 #! Wraps the given stream in an HTML stream. An HTML stream
105 #! converts special characters to entities when being
106 #! written, and supports writing attributed strings with
107 #! the following attributes:
109 #! link - an object path
110 #! fg - an rgb triplet in a list
111 #! bg - an rgb triplet in a list
116 [ chars>entities "stream" get fwrite ] "fwrite" set
117 [ chars>entities "stream" get fprint ] "fprint" set
118 [ "stream" get <html-stream>/fwrite-attr ] "fwrite-attr" set
121 : with-html-stream ( quot -- )
123 "stdio" get <html-stream> "stdio" set call
126 : html-head ( title -- )
127 "<html><head><title>" write
129 "</title></head><body><h1>" write write "</h1>" write ;
131 : html-tail ( -- ) "</body></html>" print ;
133 : html-document ( title quot -- )
134 swap chars>entities html-head call html-tail ;
136 : preformatted-html ( quot -- )
137 "<pre>" print call "</pre>" print ;
139 : simple-html-document ( title quot -- )
141 [ [ call ] with-html-stream ] preformatted-html