Question
· Nov 2, 2016

html beautifier

Hello,

I'm sending http request through %Net.HttpRequest and I have html page in response. Is there any built-in tool for beautify html for printing in terminal?
Thanks.

Discussion (4)0
Log in or sign up to continue

I know of no such built in tool, however here's a routine I wrote to pretty print an XML string:

 PrettyPrintXML(pXML)
	for tI = 1:1:10 write ! }
	set tTabCount = 0
	// if starting with an XML prolog, do not increment tab on next line
	if ($E(pXML,1,5)="<?xml") 
		set tI = $FIND(pXML,">")
		set $EXTRACT(pXML,tI-1) = ">"_$C(13,10) 
	}
	else set tI = 1 
	for {
		set tI = $FIND(pXML,"<",tI) quit:tI=0
		//cdata
		if ($EXTRACT(pXML,tI,tI+7)="![CDATA[") {
			//set tTabCount = tTabCount + 1
			do Tabs
			set $EXTRACT(pXML,tI-1) = tTabs_"<"
			set tI = $FIND(pXML,">",tI) quit:tI=0
			set $EXTRACT(pXML,tI-1) = ">"_$C(13,10)
			//set tTabCount = tTabCount - 1
		}
		//open tag
		elseif ($EXTRACT(pXML,tI) '= "/") {
			do Tabs
			set $EXTRACT(pXML,tI-1) = tTabs_"<"
			set tI = $FIND(pXML,">",tI) quit:tI=0
			// only increment tab count if this is not an empty tag (<tag/>)
			if ($E(pXML,tI-2)'="/") set tTabCount = tTabCount + 1 }
			//if followed by another open tag, insert a newline before it
			if ($EXTRACT(pXML,tI) = "<") set $EXTRACT(pXML,tI) = $C(13,10)_"<" }
		}
		//close tag
		else {
			set tTabCount = tTabCount - 1
			//if following another close tag, put tabs before this one ($C(62) = ">")
			if ($EXTRACT(pXML,tI-4,tI-2) = $C(62,13,10)) {
				do Tabs
				set $EXTRACT(pXML,tI-1) = tTabs_"<"
			}
			set tI = $FIND(pXML,">",tI) quit:tI=0
			set $EXTRACT(pXML,tI-1) = ">"_$C(13,10)
		}
	}
	write pXML
	quit
Tabs
	set tTabs = ""
	for i=1:1:tTabCount set tTabs = tTabs_$C(9) }
	quit