factor/extra/html/parser/printer/printer.factor

90 lines
2.4 KiB
Factor
Raw Normal View History

2008-08-17 11:38:34 -04:00
USING: accessors assocs html.parser html.parser.utils combinators
2007-09-20 18:09:08 -04:00
continuations hashtables
hashtables.private io kernel math
namespaces prettyprint quotations sequences splitting
strings ;
2007-12-04 15:14:33 -05:00
IN: html.parser.printer
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
SYMBOL: printer
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
TUPLE: html-printer ;
TUPLE: text-printer < html-printer ;
TUPLE: src-printer < html-printer ;
TUPLE: html-prettyprinter < html-printer ;
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
HOOK: print-text-tag html-printer ( tag -- )
HOOK: print-comment-tag html-printer ( tag -- )
HOOK: print-dtd-tag html-printer ( tag -- )
HOOK: print-opening-tag html-printer ( tag -- )
HOOK: print-closing-tag html-printer ( tag -- )
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
ERROR: unknown-tag-error tag ;
: print-tag ( tag -- )
{
{ [ dup name>> text = ] [ print-text-tag ] }
{ [ dup name>> comment = ] [ print-comment-tag ] }
{ [ dup name>> dtd = ] [ print-dtd-tag ] }
{ [ dup [ name>> string? ] [ closing?>> ] bi and ]
[ print-closing-tag ] }
{ [ dup name>> string? ]
[ print-opening-tag ] }
[ unknown-tag-error ]
} cond ;
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
: print-tags ( vector -- ) [ print-tag ] each ;
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
: html-text. ( vector -- )
T{ text-printer } html-printer [ print-tags ] with-variable ;
: html-src. ( vector -- )
T{ src-printer } html-printer [ print-tags ] with-variable ;
M: html-printer print-text-tag ( tag -- ) text>> write ;
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
M: html-printer print-comment-tag ( tag -- )
"<!--" write text>> write "-->" write ;
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
M: html-printer print-dtd-tag ( tag -- )
"<!" write text>> write ">" write ;
2007-09-20 18:09:08 -04:00
: print-attributes ( hashtable -- )
2008-08-17 11:38:34 -04:00
[ [ bl write "=" write ] [ ?quote write ] bi* ] assoc-each ;
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
M: src-printer print-opening-tag ( tag -- )
2007-09-20 18:09:08 -04:00
"<" write
2008-08-17 11:38:34 -04:00
[ name>> write ]
[ attributes>> dup assoc-empty? [ drop ] [ print-attributes ] if ] bi
2007-09-20 18:09:08 -04:00
">" write ;
2008-08-17 11:38:34 -04:00
M: src-printer print-closing-tag ( tag -- )
2007-09-20 18:09:08 -04:00
"</" write
2008-08-17 11:38:34 -04:00
name>> write
2007-09-20 18:09:08 -04:00
">" write ;
SYMBOL: tab-width
SYMBOL: #indentations
2008-08-17 11:38:34 -04:00
SYMBOL: tagstack
: prettyprint-html ( vector -- )
[
T{ html-prettyprinter } printer set
V{ } clone tagstack set
2 tab-width set
0 #indentations set
print-tags
] with-scope ;
2007-09-20 18:09:08 -04:00
: print-tabs ( -- )
tab-width get #indentations get * CHAR: \s <repetition> write ;
2008-08-17 11:38:34 -04:00
M: html-prettyprinter print-opening-tag ( tag -- )
print-tabs "<" write
2008-08-17 11:38:34 -04:00
name>> write
">\n" write ;
2008-08-17 11:38:34 -04:00
M: html-prettyprinter print-closing-tag ( tag -- )
"</" write
2008-08-17 11:38:34 -04:00
name>> write
">" write ;