mandag 28. januar 2013

html to xsl in groovy

I work with a cms where we use xslt in order to render html out on the pages.
When we write xslt it's usually based on a prototype we did in html. I wrote a quick script to render the html-tags into xsl (in groovy).



def output = ""
new File(args[0]).eachLine { line ->
def txt = line.find(/<(span|img|h2|a|form|p|div|label|input|textarea)(.*)>/)
if(txt){
output+="<xsl:element name=\""+txt.find(/span|img|h2|a|form|p|div|label|input|textarea/)+"\">\n"
txt.eachMatch(/(id|class|href|src|for|type|method|rows|src)=\"(.*?)\"/){it->
output+="<xsl:attribute name=\"" + it[1] + "\">"+ it[2] +"</xsl:attribute>\n"
}
}
if( line.find( />(.*?)<\// ) ){
output += "</xsl:element> <!-- $line -->\n"
}
if( line.replaceAll(/\s/,"").find(/^<\//) ){
output += "</xsl:element><!-- $line --> \n"
}
}
println output

Could be useful for other people than me, I guess.

3 kommentarer:

Anonym sa...

YOU PAGE LOOKS AMAZING ON MOBILE VIEW!!§!!

Anonym sa...

Very inpiring article!

Anonym sa...

Wow, watch Espen when he codes, he does it really really high.

 
 
Copyright © >> /dev/null
Blogger Theme by BloggerThemes Design by Diovo.com