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)(.*)>/)
output+="<xsl:element name=\""+txt.find(/span|img|h2|a|form|p|div|label|input|textarea/)+"\">\n"
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.
Copyright © >> /dev/null
Blogger Theme by BloggerThemes Design by