1 ! Copyright (C) 2004, 2009 Chris Double, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io io.styles kernel namespaces prettyprint quotations
4 sequences strings words xml.entities compiler.units effects
5 xml.data xml.interpolate urls math math.parser combinators
6 present fry io.streams.string xml.writer ;
12 : write-html ( str -- )
13 H{ { html t } } format ;
15 : print-html ( str -- )
16 write-html "\n" write-html ;
20 : elements-vocab ( -- vocab-name ) "html.elements" ;
22 : html-word ( name def effect -- )
23 #! Define 'word creating' word to allow
24 #! dynamically creating words.
25 [ elements-vocab create ] 2dip define-declared ;
27 : <foo> ( str -- <str> ) "<" ">" surround ;
29 : def-for-html-word-<foo> ( name -- )
30 #! Return the name and code for the <foo> patterned
32 dup <foo> swap '[ _ <foo> write-html ]
35 : <foo ( str -- <str ) "<" prepend ;
37 : def-for-html-word-<foo ( name -- )
38 #! Return the name and code for the <foo patterned
40 <foo dup '[ _ write-html ]
43 : foo> ( str -- foo> ) ">" append ;
45 : def-for-html-word-foo> ( name -- )
46 #! Return the name and code for the foo> patterned
48 foo> [ ">" write-html ] (( -- )) html-word ;
50 : </foo> ( str -- </str> ) "</" ">" surround ;
52 : def-for-html-word-</foo> ( name -- )
53 #! Return the name and code for the </foo> patterned
55 </foo> dup '[ _ write-html ] (( -- )) html-word ;
57 : <foo/> ( str -- <str/> ) "<" "/>" surround ;
59 : def-for-html-word-<foo/> ( name -- )
60 #! Return the name and code for the <foo/> patterned
62 dup <foo/> swap '[ _ <foo/> write-html ]
65 : foo/> ( str -- str/> ) "/>" append ;
67 : def-for-html-word-foo/> ( name -- )
68 #! Return the name and code for the foo/> patterned
70 foo/> [ "/>" write-html ] (( -- )) html-word ;
72 : define-closed-html-word ( name -- )
73 #! Given an HTML tag name, define the words for
74 #! that closable HTML tag.
75 dup def-for-html-word-<foo>
76 dup def-for-html-word-<foo
77 dup def-for-html-word-foo>
78 def-for-html-word-</foo> ;
80 : define-open-html-word ( name -- )
81 #! Given an HTML tag name, define the words for
82 #! that open HTML tag.
83 dup def-for-html-word-<foo/>
84 dup def-for-html-word-<foo
85 def-for-html-word-foo/> ;
87 : write-attr ( value name -- )
91 present escape-quoted-string write-html
94 : define-attribute-word ( name -- )
96 '[ _ write-attr ] (( string -- )) html-word ;
98 ! Define some closed HTML tags
100 "h1" "h2" "h3" "h4" "h5" "h6" "h7" "h8" "h9"
101 "ol" "li" "form" "a" "p" "html" "head" "body" "title"
102 "b" "i" "ul" "table" "tbody" "tr" "td" "th" "pre" "textarea"
103 "script" "div" "span" "select" "option" "style" "input"
105 ] [ define-closed-html-word ] each
107 ! Define some open HTML tags
115 ] [ define-open-html-word ] each
117 ! Define some attributes
119 "method" "action" "type" "value" "name"
120 "size" "href" "class" "border" "rows" "cols"
121 "id" "onclick" "style" "valign" "accesskey"
122 "src" "language" "colspan" "onchange" "rel"
123 "width" "selected" "onsubmit" "xmlns" "lang" "xml:lang"
124 "media" "title" "multiple" "checked"
125 "summary" "cellspacing" "align" "scope" "abbr"
126 "nofollow" "alt" "target"
127 ] [ define-attribute-word ] each
131 : xhtml-preamble ( -- )
132 "<?xml version=\"1.0\"?>" write-html
133 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">" write-html ;
135 : simple-page ( title head-quot body-quot -- )
136 [ with-string-writer <unescaped> ] bi@
138 <?xml version="1.0"?>
139 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
140 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
147 XML> write-xml ; inline
149 : render-error ( message -- )
150 [XML <span class="error"><-></span> XML] write-xml ;