Created
December 31, 2023 16:29
-
-
Save blackdrag/860a1e156d5eca5ddd846260faf2d7fa to your computer and use it in GitHub Desktop.
Very Simple Groovy HTML builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new SimpleBuilder().html { | |
head { | |
title ("Simple Groovy HTML builder") | |
body { | |
h1 ("Simple Groovy HTML builder") | |
p ("this format can be used as an alternative markup to HTML") | |
a(href: "http://groovy-lang.org","Groovy") | |
p { | |
_ ("This is some") | |
b ("mixed") | |
_ "text. For more see the " | |
a(href:"http://groovy-lang.org","Groovy") | |
_ "project" | |
} | |
p { | |
_ "some text" | |
ul { | |
5.times { i -> li("${i}*2 = ${i*2}" )} | |
} | |
} | |
} | |
} | |
} | |
class SimpleBuilder { | |
def index = 0 | |
def invokeMethod(String name, Object args) { | |
print " "*index | |
if (name=="_") { | |
println(args[0]) | |
return | |
} | |
print "<"+name | |
def processStack = args as List | |
def toProcess = processStack.pop() | |
if (toProcess instanceof Map) { | |
toProcess.each{k,v -> print " $k=\"$v\""} | |
toProcess = processStack.pop() | |
} | |
println ">" | |
if (toProcess instanceof Closure) { | |
toProcess.delegate = this | |
index++ | |
toProcess() | |
index-- | |
} else if (toProcess) { | |
print " "*(index+1) | |
println toProcess | |
} | |
print " "*index | |
println("</"+name+">") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment