3 ! Copyright (C) 2004 Chris Double.
4 ! See http://factorcode.org/license.txt for BSD license.
6 USING: io kernel namespaces prettyprint quotations
7 sequences strings words xml.entities compiler.units effects ;
11 ! These words are used to provide a means of writing
12 ! formatted HTML to standard output with a familiar 'html' look
13 ! and feel in the code.
15 ! HTML tags can be used in a number of different ways. The highest
16 ! level involves a similar syntax to HTML:
18 ! <p> "someoutput" write </p>
20 ! <p> will output the opening tag and </p> will output the closing
21 ! tag with no attributes.
23 ! <p "red" =class p> "someoutput" write </p>
25 ! This time the opening tag does not have the '>'. It pushes
26 ! a namespace on the stack to hold the attributes and values.
27 ! Any attribute words used will store the attribute and values
28 ! in that namespace. Before the attribute word should come the
29 ! value of that attribute.
30 ! The finishing word will print out the operning tag including
32 ! Any writes after this will appear after the opening tag.
34 ! Values for attributes can be used directly without any stack
38 ! <a =href a> "Click me" write </a>
41 ! <a "http://" prepend =href a> "click" write </a>
44 ! <a [ "http://" % % ] "" make =href a> "click" write </a>
46 ! Tags that have no 'closing' equivalent have a trailing tag/> form:
48 ! <input "text" =type "name" =name "20" =size input/>
50 : elements-vocab ( -- vocab-name ) "html.elements" ;
54 : write-html ( str -- )
55 H{ { html t } } format ;
57 : print-html ( str -- )
58 write-html "\n" write-html ;
60 : html-word ( name def effect -- )
61 #! Define 'word creating' word to allow
62 #! dynamically creating words.
63 >r >r elements-vocab create r> r> define-declared ;
65 : <foo> "<" swap ">" 3append ;
67 : empty-effect T{ effect f 0 0 } ;
69 : def-for-html-word-<foo> ( name -- )
70 #! Return the name and code for the <foo> patterned
72 dup <foo> swap [ <foo> write-html ] curry
73 empty-effect html-word ;
77 : def-for-html-word-<foo ( name -- )
78 #! Return the name and code for the <foo patterned
80 <foo dup [ write-html ] curry
81 empty-effect html-word ;
85 : def-for-html-word-foo> ( name -- )
86 #! Return the name and code for the foo> patterned
88 foo> [ ">" write-html ] empty-effect html-word ;
90 : </foo> "</" swap ">" 3append ;
92 : def-for-html-word-</foo> ( name -- )
93 #! Return the name and code for the </foo> patterned
95 </foo> dup [ write-html ] curry empty-effect html-word ;
97 : <foo/> "<" swap "/>" 3append ;
99 : def-for-html-word-<foo/> ( name -- )
100 #! Return the name and code for the <foo/> patterned
102 dup <foo/> swap [ <foo/> write-html ] curry
103 empty-effect html-word ;
105 : foo/> "/>" append ;
107 : def-for-html-word-foo/> ( name -- )
108 #! Return the name and code for the foo/> patterned
110 foo/> [ "/>" write-html ] empty-effect html-word ;
112 : define-closed-html-word ( name -- )
113 #! Given an HTML tag name, define the words for
114 #! that closable HTML tag.
115 dup def-for-html-word-<foo>
116 dup def-for-html-word-<foo
117 dup def-for-html-word-foo>
118 def-for-html-word-</foo> ;
120 : define-open-html-word ( name -- )
121 #! Given an HTML tag name, define the words for
122 #! that open HTML tag.
123 dup def-for-html-word-<foo/>
124 dup def-for-html-word-<foo
125 def-for-html-word-foo/> ;
127 : write-attr ( value name -- )
131 escape-quoted-string write-html
134 : attribute-effect T{ effect f { "string" } 0 } ;
136 : define-attribute-word ( name -- )
138 [ write-attr ] curry attribute-effect html-word ;
141 ! Define some closed HTML tags
143 "h1" "h2" "h3" "h4" "h5" "h6" "h7" "h8" "h9"
144 "ol" "li" "form" "a" "p" "html" "head" "body" "title"
145 "b" "i" "ul" "table" "tbody" "tr" "td" "th" "pre" "textarea"
146 "script" "div" "span" "select" "option" "style"
147 ] [ define-closed-html-word ] each
149 ! Define some open HTML tags
155 ] [ define-open-html-word ] each
157 ! Define some attributes
159 "method" "action" "type" "value" "name"
160 "size" "href" "class" "border" "rows" "cols"
161 "id" "onclick" "style" "valign" "accesskey"
162 "src" "language" "colspan" "onchange" "rel"
163 "width" "selected" "onsubmit" "xmlns" "lang" "xml:lang"
165 ] [ define-attribute-word ] each
166 ] with-compilation-unit