html.parser.printer: reasonable tree-structured formatting with prettyprint-html

db4
Björn Lindqvist 2013-07-22 14:05:14 +02:00 committed by John Benediktsson
parent 490091eb22
commit 68381dd571
1 changed files with 13 additions and 12 deletions

View File

@ -1,8 +1,8 @@
USING: accessors assocs html.parser html.parser.utils combinators USING: accessors assocs html.parser html.parser.utils combinators
continuations hashtables continuations hashtables
hashtables.private io kernel math hashtables.private io kernel make math
namespaces prettyprint quotations sequences splitting namespaces prettyprint quotations sequences sequences.repeating splitting
strings ; strings unicode.categories ;
IN: html.parser.printer IN: html.parser.printer
SYMBOL: printer SYMBOL: printer
@ -68,22 +68,23 @@ SYMBOL: tagstack
: prettyprint-html ( vector -- ) : prettyprint-html ( vector -- )
[ [
T{ html-prettyprinter } printer set T{ html-prettyprinter } html-printer set
V{ } clone tagstack set V{ } clone tagstack set
2 tab-width set 2 tab-width set
0 #indentations set 0 #indentations set
print-tags print-tags
] with-scope ; ] with-scope ;
: print-tabs ( -- ) : tabs ( -- str )
tab-width get #indentations get * CHAR: \s <repetition> write ; " " tab-width get #indentations get * repeat ;
M: html-prettyprinter print-opening-tag ( tag -- ) M: html-prettyprinter print-opening-tag ( tag -- )
print-tabs "<" write name>>
name>> write [ tabs "<" append ">\n" surround write ]
">\n" write ; [ { "br" "img" } member? [ #indentations inc ] unless ] bi ;
M: html-prettyprinter print-closing-tag ( tag -- ) M: html-prettyprinter print-closing-tag ( tag -- )
"</" write #indentations dec name>> tabs "</" append ">\n" surround write ;
name>> write
">" write ; M: html-prettyprinter print-text-tag ( tag -- )
text>> [ blank? ] trim [ tabs "\n" surround write ] unless-empty ;