module Format where

import HXML
import DeleteBlnFromXML
--Ejercicio 2 de la Prac4

sepBln::Int
maxLengthElement::Int
sepBln = 3
maxLengthElement = 80

blancos 0 = []
blancos n = " ":(blancos (n-1))

attrFormat (x,y) =[(" ")++x++("=")++("\"")++y++("\"")]

showFormattedXML prof (Tree (ELNode name attr) [])  = (concat(blancos (prof*sepBln)))++("<")++(name)++(concat(concat(map attrFormat attr)))++(">\n")++(concat(blancos (prof*sepBln)))++("</")++(name)++(">\n")
showFormattedXML prof (Tree (TXNode text) [])  = (concat(blancos (prof*sepBln)))++(text)++("\n")

showFormattedXML prof (Tree (ELNode name attr) [Tree (TXNode text)[]]) = if (((2*(length name))+(length text)+5+(prof*sepBln))>maxLengthElement) 
									 then (concat(blancos (prof*sepBln)))++("<")++(name)++(concat(concat(map attrFormat attr)))++(">\n")++(concat(blancos ((prof+1)*sepBln)))++text++("\n")++(concat(blancos (prof*sepBln)))++("</")++(name)++(">\n")
									 else (concat(blancos (prof*sepBln)))++("<")++(name)++(concat(concat(map attrFormat attr)))++(">")++text++("</")++(name)++(">\n")
		
showFormattedXML prof (Tree (ELNode name attr) args) = (concat(blancos (prof*sepBln)))++("<")++(name)++(concat(concat(map attrFormat attr)))++(">\n")++(concat(map (showFormattedXML (prof+1)) args))++(concat(blancos (prof*sepBln)))++("</")++(name)++(">\n")				 
		  

showFormattedSpecAux [] = ")":[]
showFormattedSpecAux (x:xs) = (showFormattedSpec x):(showFormattedSpecAux xs)

showFormattedSpec (Tree (TXNode name) []) = name++","
showFormattedSpec (Tree (ELNode name []) args) = name++"("++(concat(showFormattedSpecAux args))++","

quitColons [] = []
quitColons (x:xs) 
		|(xs/=[]) = if ((x==',') && ((head xs)==')'))
		    	    then (quitColons xs)
		    	    else (x:(quitColons xs))
		|otherwise = if (x==',') 
		    	     then []
		    	     else [x]

showPropSpec spec = (quitColons (showFormattedSpec spec))