factor/extra/html/elements/elements.factor

129 lines
3.5 KiB
Factor
Raw Normal View History

2009-01-29 14:33:04 -05:00
! Copyright (C) 2004, 2009 Chris Double, Daniel Ehrenberg.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
2009-01-13 18:35:45 -05:00
USING: io io.styles kernel namespaces prettyprint quotations
sequences strings words xml.entities compiler.units effects
2009-02-05 22:17:03 -05:00
xml.data urls math math.parser combinators
2009-01-30 20:28:16 -05:00
present fry io.streams.string xml.writer html ;
2007-09-20 18:09:08 -04:00
IN: html.elements
SYMBOL: html
: write-html ( str -- )
H{ { html t } } format ;
: print-html ( str -- )
write-html "\n" write-html ;
<<
2008-09-19 16:45:45 -04:00
: elements-vocab ( -- vocab-name ) "html.elements" ;
2008-01-11 00:48:04 -05:00
: html-word ( name def effect -- )
2007-09-20 18:09:08 -04:00
#! Define 'word creating' word to allow
#! dynamically creating words.
2008-12-03 09:46:16 -05:00
[ elements-vocab create ] 2dip define-declared ;
2008-01-11 00:48:04 -05:00
2008-12-06 19:58:45 -05:00
: <foo> ( str -- <str> ) "<" ">" surround ;
2007-09-20 18:09:08 -04:00
: def-for-html-word-<foo> ( name -- )
#! Return the name and code for the <foo> patterned
#! word.
2008-09-10 23:11:40 -04:00
dup <foo> swap '[ _ <foo> write-html ]
( -- ) html-word ;
2007-09-20 18:09:08 -04:00
: <foo ( str -- <str ) "<" prepend ;
2007-09-20 18:09:08 -04:00
: def-for-html-word-<foo ( name -- )
#! Return the name and code for the <foo patterned
#! word.
2008-09-10 23:11:40 -04:00
<foo dup '[ _ write-html ]
( -- ) html-word ;
2007-09-20 18:09:08 -04:00
: foo> ( str -- foo> ) ">" append ;
2007-09-20 18:09:08 -04:00
: def-for-html-word-foo> ( name -- )
#! Return the name and code for the foo> patterned
#! word.
foo> [ ">" write-html ] ( -- ) html-word ;
2007-09-20 18:09:08 -04:00
2008-12-06 19:58:45 -05:00
: </foo> ( str -- </str> ) "</" ">" surround ;
2007-09-20 18:09:08 -04:00
: def-for-html-word-</foo> ( name -- )
#! Return the name and code for the </foo> patterned
2008-01-11 00:48:04 -05:00
#! word.
</foo> dup '[ _ write-html ] ( -- ) html-word ;
2007-09-20 18:09:08 -04:00
2008-12-06 19:58:45 -05:00
: <foo/> ( str -- <str/> ) "<" "/>" surround ;
2007-09-20 18:09:08 -04:00
: def-for-html-word-<foo/> ( name -- )
#! Return the name and code for the <foo/> patterned
#! word.
2008-09-10 23:11:40 -04:00
dup <foo/> swap '[ _ <foo/> write-html ]
( -- ) html-word ;
2007-09-20 18:09:08 -04:00
: foo/> ( str -- str/> ) "/>" append ;
2007-09-20 18:09:08 -04:00
: def-for-html-word-foo/> ( name -- )
#! Return the name and code for the foo/> patterned
2008-01-11 00:48:04 -05:00
#! word.
foo/> [ "/>" write-html ] ( -- ) html-word ;
2007-09-20 18:09:08 -04:00
2008-01-11 00:48:04 -05:00
: define-closed-html-word ( name -- )
2007-09-20 18:09:08 -04:00
#! Given an HTML tag name, define the words for
#! that closable HTML tag.
dup def-for-html-word-<foo>
dup def-for-html-word-<foo
dup def-for-html-word-foo>
def-for-html-word-</foo> ;
2008-01-11 00:48:04 -05:00
: define-open-html-word ( name -- )
2007-09-20 18:09:08 -04:00
#! Given an HTML tag name, define the words for
#! that open HTML tag.
dup def-for-html-word-<foo/>
dup def-for-html-word-<foo
def-for-html-word-foo/> ;
: write-attr ( value name -- )
" " write-html
write-html
"='" write-html
present escape-quoted-string write-html
2007-09-20 18:09:08 -04:00
"'" write-html ;
: define-attribute-word ( name -- )
dup "=" prepend swap
'[ _ write-attr ] ( string -- ) html-word ;
2007-09-20 18:09:08 -04:00
! Define some closed HTML tags
[
"h1" "h2" "h3" "h4" "h5" "h6" "h7" "h8" "h9"
"ol" "li" "form" "a" "p" "html" "head" "body" "title"
2009-06-30 11:26:51 -04:00
"b" "i" "ul" "table" "thead" "tfoot" "tbody" "tr" "td" "th" "pre" "textarea"
"script" "div" "span" "select" "option" "style" "input"
"strong"
] [ define-closed-html-word ] each
! Define some open HTML tags
[
"input"
"br"
2008-09-23 02:50:34 -04:00
"hr"
"link"
"img"
2008-09-29 05:10:00 -04:00
"base"
] [ define-open-html-word ] each
! Define some attributes
2007-09-20 18:09:08 -04:00
[
"method" "action" "type" "value" "name"
"size" "href" "class" "border" "rows" "cols"
"id" "onclick" "style" "valign" "accesskey"
"src" "language" "colspan" "onchange" "rel"
"width" "selected" "onsubmit" "xmlns" "lang" "xml:lang"
"media" "title" "multiple" "checked"
"summary" "cellspacing" "align" "scope" "abbr"
2008-09-29 05:10:00 -04:00
"nofollow" "alt" "target"
] [ define-attribute-word ] each
>>