html.parser.printer: reasonable tree-structured formatting with prettyprint-html
							parent
							
								
									490091eb22
								
							
						
					
					
						commit
						68381dd571
					
				| 
						 | 
				
			
			@ -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 <repetition> 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
 | 
			
		||||
    name>> write
 | 
			
		||||
    ">" write ;
 | 
			
		||||
    #indentations dec name>> tabs "</" append ">\n" surround write ;
 | 
			
		||||
 | 
			
		||||
M: html-prettyprinter print-text-tag ( tag -- )
 | 
			
		||||
    text>> [ blank? ] trim [ tabs "\n" surround write ] unless-empty ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue