]> gitweb.factorcode.org Git - factor.git/blob - library/httpd/html.factor
622b7abed8d9ea932abe215e4d015d5f4713c983
[factor.git] / library / httpd / html.factor
1 !:folding=indent:collapseFolds=1:
2
3 ! $Id$
4 !
5 ! Copyright (C) 2004 Slava Pestov.
6
7 ! Redistribution and use in source and binary forms, with or without
8 ! modification, are permitted provided that the following conditions are met:
9
10 ! 1. Redistributions of source code must retain the above copyright notice,
11 !    this list of conditions and the following disclaimer.
12
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.
16
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.
27
28 IN: html
29 USE: combinators
30 USE: format
31 USE: lists
32 USE: logic
33 USE: namespaces
34 USE: stack
35 USE: stdio
36 USE: streams
37 USE: strings
38 USE: unparser
39 USE: url-encoding
40
41 : html-entities ( -- alist )
42     [
43         [ #\< | "&lt;"   ]
44         [ #\> | "&gt;"   ]
45         [ #\& | "&amp;"  ]
46         [ #\' | "&apos;" ]
47         [ #\" | "&quot;" ]
48     ] ;
49
50 : chars>entities ( str -- str )
51     #! Convert <, >, &, ' and " to HTML entities.
52     [ dup html-entities assoc dup rot ? ] str-map ;
53
54 : opening-tag ( tag attrs -- )
55     "<" % swap % [ " " % % ] when* ">" % ;
56
57 : closing-tag ( tag -- )
58     "</" % % ">" % ;
59
60 : html-tag ( str tag attrs -- str )
61     #! Wrap a string in an HTML tag.
62     <% dupd opening-tag swap % closing-tag %> ;
63
64 : responder-link% ( -- )
65     "/" % "responder" get % "/" % ;
66
67 : link-attrs ( link -- attrs )
68     <% "href=\"" % responder-link% % "\"" % %> ;
69
70 : link-tag ( string link -- string )
71     "a" swap link-attrs html-tag ;
72
73 : bold-tag ( string -- string )
74     "b" f html-tag ;
75
76 : italics-tag ( string -- string )
77     "i" f html-tag ;
78
79 : underline-tag ( string -- string )
80     "u" f html-tag ;
81
82 : >hex-color ( triplet -- hex )
83     [ >hex 2 digits ] inject "#" swons cat ;
84
85 : fg-tag ( string color -- string )
86     "font" swap "color=\"" swap >hex-color "\"" cat3 html-tag ;
87
88 : size-tag ( string size -- string )
89     "font" swap "size=\"" swap "\"" cat3 html-tag ;
90
91 : html-attr-string ( string -- string )
92     chars>entities
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* ;
99
100 : <html-stream>/fwrite-attr ( string stream -- )
101     [ html-attr-string ] dip fwrite ;
102
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:
108     #!
109     #! link - an object path
110     #! fg - an rgb triplet in a list
111     #! bg - an rgb triplet in a list
112     #! bold
113     #! italic
114     #! underline
115     <extend-stream> [
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
119     ] extend ;
120
121 : with-html-stream ( quot -- )
122      <namespace> [
123         "stdio" get <html-stream> "stdio" set call
124     ] bind ;
125
126 : html-head ( title -- )
127     "<html><head><title>" write
128     dup write
129     "</title></head><body><h1>" write write "</h1>" write ;
130
131 : html-tail ( -- ) "</body></html>" print ;
132
133 : html-document ( title quot -- )
134     swap chars>entities html-head call html-tail ;
135
136 : preformatted-html ( quot -- )
137     "<pre>" print call "</pre>" print ;
138
139 : simple-html-document ( title quot -- )
140     swap [
141         [ [ call ] with-html-stream ] preformatted-html
142     ] html-document ;