3 ! Copyright (C) 2004 Chris Double.
4 ! See http://factorcode.org/license.txt for BSD license.
15 ! These words are used to provide a means of writing
16 ! formatted HTML to standard output with a familiar 'html' look
17 ! and feel in the code.
19 ! HTML tags can be used in a number of different ways. The highest
20 ! level involves a similar syntax to HTML:
22 ! <p> "someoutput" write </p>
24 ! <p> will output the opening tag and </p> will output the closing
25 ! tag with no attributes.
27 ! <p "red" =class p> "someoutput" write </p>
29 ! This time the opening tag does not have the '>'. It pushes
30 ! a namespace on the stack to hold the attributes and values.
31 ! Any attribute words used will store the attribute and values
32 ! in that namespace. Before the attribute word should come the
33 ! value of that attribute.
34 ! The finishing word will print out the operning tag including
36 ! Any writes after this will appear after the opening tag.
38 ! Values for attributes can be used directly without any stack
42 ! <a =href a> "Click me" write </a>
45 ! <a "http://" swap append =href a> "click" write </a>
48 ! <a [ "http://" % % ] "" make =href a> "click" write </a>
50 ! Tags that have no 'closing' equivalent have a trailing tag/> form:
52 ! <input "text" =type "name" =name "20" =size input/>
56 : write-html ( str -- )
57 H{ { html t } } format ;
59 : html-word ( name def -- )
60 #! Define 'word creating' word to allow
61 #! dynamically creating words.
62 >r "html" create r> define-compound ;
64 : <foo> "<" swap ">" 3append ;
66 : def-for-html-word-<foo> ( name -- )
67 #! Return the name and code for the <foo> patterned
69 dup <foo> swap [ <foo> write-html ] curry html-word ;
71 : <foo "<" swap append ;
73 : def-for-html-word-<foo ( name -- )
74 #! Return the name and code for the <foo patterned
76 <foo dup [ write-html ] curry html-word ;
80 : def-for-html-word-foo> ( name -- )
81 #! Return the name and code for the foo> patterned
83 foo> [ ">" write-html ] html-word ;
85 : </foo> [ "</" % % ">" % ] "" make ;
87 : def-for-html-word-</foo> ( name -- )
88 #! Return the name and code for the </foo> patterned
90 </foo> dup [ write-html ] curry html-word ;
92 : <foo/> [ "<" % % "/>" % ] "" make ;
94 : def-for-html-word-<foo/> ( name -- )
95 #! Return the name and code for the <foo/> patterned
97 dup <foo/> swap [ <foo/> write-html ] curry html-word ;
101 : def-for-html-word-foo/> ( name -- )
102 #! Return the name and code for the foo/> patterned
104 foo/> [ "/>" write-html ] html-word ;
106 : define-closed-html-word ( name -- )
107 #! Given an HTML tag name, define the words for
108 #! that closable HTML tag.
109 dup def-for-html-word-<foo>
110 dup def-for-html-word-<foo
111 dup def-for-html-word-foo>
112 def-for-html-word-</foo> ;
114 : define-open-html-word ( name -- )
115 #! Given an HTML tag name, define the words for
116 #! that open HTML tag.
117 dup def-for-html-word-<foo/>
118 dup def-for-html-word-<foo
119 def-for-html-word-foo/> ;
121 : write-attr ( value name -- )
128 : define-attribute-word ( name -- )
129 dup "=" swap append swap
130 [ write-attr ] curry html-word ;
132 ! Define some closed HTML tags
134 "h1" "h2" "h3" "h4" "h5" "h6" "h7" "h8" "h9"
135 "ol" "li" "form" "a" "p" "html" "head" "body" "title"
136 "b" "i" "ul" "table" "tbody" "tr" "td" "th" "pre" "textarea"
137 "script" "div" "span" "select" "option" "style"
138 ] [ define-closed-html-word ] each
140 ! Define some open HTML tags
146 ] [ define-open-html-word ] each
148 ! Define some attributes
150 "method" "action" "type" "value" "name"
151 "size" "href" "class" "border" "rows" "cols"
152 "id" "onclick" "style" "valign" "accesskey"
153 "src" "language" "colspan" "onchange" "rel"
154 "width" "selected" "onsubmit"
155 ] [ define-attribute-word ] each