From 68381dd5713bb552d46fccf51995e0bb964504a2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bj=C3=B6rn=20Lindqvist?= Date: Mon, 22 Jul 2013 14:05:14 +0200 Subject: [PATCH] html.parser.printer: reasonable tree-structured formatting with prettyprint-html --- extra/html/parser/printer/printer.factor | 25 ++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/extra/html/parser/printer/printer.factor b/extra/html/parser/printer/printer.factor index 4419eec70e..fbad8581df 100644 --- a/extra/html/parser/printer/printer.factor +++ b/extra/html/parser/printer/printer.factor @@ -1,8 +1,8 @@ USING: accessors assocs html.parser html.parser.utils combinators continuations hashtables -hashtables.private io kernel math -namespaces prettyprint quotations sequences splitting -strings ; +hashtables.private io kernel make math +namespaces prettyprint quotations sequences sequences.repeating splitting +strings unicode.categories ; IN: html.parser.printer SYMBOL: printer @@ -68,22 +68,23 @@ SYMBOL: tagstack : prettyprint-html ( vector -- ) [ - T{ html-prettyprinter } printer set + T{ html-prettyprinter } html-printer set V{ } clone tagstack set 2 tab-width set 0 #indentations set print-tags ] with-scope ; -: print-tabs ( -- ) - tab-width get #indentations get * CHAR: \s write ; +: tabs ( -- str ) + " " tab-width get #indentations get * repeat ; M: html-prettyprinter print-opening-tag ( tag -- ) - print-tabs "<" write - name>> write - ">\n" write ; + name>> + [ tabs "<" append ">\n" surround write ] + [ { "br" "img" } member? [ #indentations inc ] unless ] bi ; M: html-prettyprinter print-closing-tag ( tag -- ) - "> write - ">" write ; + #indentations dec name>> tabs "\n" surround write ; + +M: html-prettyprinter print-text-tag ( tag -- ) + text>> [ blank? ] trim [ tabs "\n" surround write ] unless-empty ; -- 2.34.1