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

97 lines
2.8 KiB
Factor
Raw Normal View History

USING: accessors assocs combinators html.parser
html.parser.utils io kernel math math.order namespaces sequences
strings unicode.categories ;
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
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: text-printer print-opening-tag
name>> "br" = [ nl ] when ;
M: text-printer print-closing-tag
name>> "p" = [ nl ] when ;
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 } html-printer set
2008-08-17 11:38:34 -04:00
V{ } clone tagstack set
2 tab-width set
0 #indentations set
print-tags
] with-scope ;
2007-09-20 18:09:08 -04:00
: tabs ( -- vseq )
tab-width get #indentations get 0 max * CHAR: \s <repetition> ;
2008-08-17 11:38:34 -04:00
M: html-prettyprinter print-opening-tag ( tag -- )
name>>
[ tabs write "<" write write ">\n" write ]
! These tags usually don't have any closing tag associated with them.
[ { "br" "img" } member? [ #indentations inc ] unless ] bi ;
2008-08-17 11:38:34 -04:00
M: html-prettyprinter print-closing-tag ( tag -- )
[ tabs write "</" write name>> write ">\n" write ]
! These tags usually don't have any closing tag associated with them.
[ { "br" "img" } member? [ #indentations dec ] unless ] bi ;
M: html-prettyprinter print-text-tag ( tag -- )
text>> [ blank? ] trim [ tabs write write "\n" write ] unless-empty ;