1 ! Copyright (C) 2004, 2009 Chris Double, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io.styles kernel present sequences words xml.entities ;
8 : write-html ( str -- )
9 H{ { html t } } format ;
11 : print-html ( str -- )
12 write-html "\n" write-html ;
16 CONSTANT: elements-vocab "html.elements"
18 : html-word ( name def effect -- )
19 ! Define 'word creating' word to allow
20 ! dynamically creating words.
21 [ elements-vocab create-word ] 2dip define-declared ;
23 : <foo> ( str -- <str> ) "<" ">" surround ;
25 : def-for-html-word-<foo> ( name -- )
26 ! Return the name and code for the <foo> patterned
28 dup <foo> swap '[ _ <foo> write-html ]
31 : <foo ( str -- <str ) "<" prepend ;
33 : def-for-html-word-<foo ( name -- )
34 ! Return the name and code for the <foo patterned
36 <foo dup '[ _ write-html ]
39 : foo> ( str -- foo> ) ">" append ;
41 : def-for-html-word-foo> ( name -- )
42 ! Return the name and code for the foo> patterned
44 foo> [ ">" write-html ] ( -- ) html-word ;
46 : </foo> ( str -- </str> ) "</" ">" surround ;
48 : def-for-html-word-</foo> ( name -- )
49 ! Return the name and code for the </foo> patterned
51 </foo> dup '[ _ write-html ] ( -- ) html-word ;
53 : <foo/> ( str -- <str/> ) "<" "/>" surround ;
55 : def-for-html-word-<foo/> ( name -- )
56 ! Return the name and code for the <foo/> patterned
58 dup <foo/> swap '[ _ <foo/> write-html ]
61 : foo/> ( str -- str/> ) "/>" append ;
63 : def-for-html-word-foo/> ( name -- )
64 ! Return the name and code for the foo/> patterned
66 foo/> [ "/>" write-html ] ( -- ) html-word ;
68 : define-closed-html-word ( name -- )
69 ! Given an HTML tag name, define the words for
70 ! that closable HTML tag.
71 dup def-for-html-word-<foo>
72 dup def-for-html-word-<foo
73 dup def-for-html-word-foo>
74 def-for-html-word-</foo> ;
76 : define-open-html-word ( name -- )
77 ! Given an HTML tag name, define the words for
79 dup def-for-html-word-<foo/>
80 dup def-for-html-word-<foo
81 def-for-html-word-foo/> ;
83 : write-attr ( value name -- )
87 present escape-quoted-string write-html
90 : define-attribute-word ( name -- )
92 '[ _ write-attr ] ( string -- ) html-word ;
94 ! Define some closed HTML tags
96 "h1" "h2" "h3" "h4" "h5" "h6" "h7" "h8" "h9"
97 "ol" "li" "form" "a" "p" "html" "head" "body" "title"
98 "b" "i" "ul" "table" "thead" "tfoot" "tbody" "tr" "td" "th" "pre" "textarea"
99 "script" "div" "span" "select" "option" "style" "input"
101 ] [ define-closed-html-word ] each
103 ! Define some open HTML tags
111 ] [ define-open-html-word ] each
113 ! Define some attributes
115 "method" "action" "type" "value" "name"
116 "size" "href" "class" "border" "rows" "cols"
117 "id" "onclick" "style" "valign" "accesskey"
118 "src" "language" "colspan" "onchange" "rel"
119 "width" "selected" "onsubmit" "xmlns" "lang" "xml:lang"
120 "media" "title" "multiple" "checked"
121 "summary" "cellspacing" "align" "scope" "abbr"
122 "nofollow" "alt" "target"
123 ] [ define-attribute-word ] each