On Wed, Aug 5, 2009 at 1:13 PM, Niels Mayer <nielsmayer(a)gmail.com> wrote:
I wrote my first macro-bridge macro (see attachment
Main.TestMacroBridge.xar
) in groovy in preparation for my experiments with scripting in Clojure.
The
hardest thing was finding out that the name of "context" had changed to
"xcontext" for groovy and other JSR-223 languages, whereas velocity (and
WikiMacroTutorial<http://platform.xwiki.org/xwiki/bin/view/DevGuide/Wiki…)
uses "context". This has been discussed on the lists (
http://markmail.org/message/o2trzrnv5xts6x6v ), and the WikiMacroTutorial
needs updating to reflect the final status & naming-conventions.
Actually, it's in the documentation for the GroovyMacro:
http://code.xwiki.org/xwiki/bin/view/Macros/GroovyMacro
Note that since context is a reserved binding in JSR-223 specifications used
for Scripting Macros, the XWiki Api Context is now
accessible
throught xcontext
What is the full list of globals bound for groovy? e.g. "request"
"xwiki"
"xcontext" etc. Are there any additional
bindings when accessed through the macro-bridge? (other than
"xcontext.macro.content" and "xcontext.macro.context").
Is there a way to access the parameters passed in to a "script" macro. If
one sets
extra parameters beyond the defined ones, e.g. "{{script language=groovy
zip=92625}}" is there a way to access the "extra" "zip=92625"
parameter
passed in to the "script" macro?
....
Also, the code I posted from "XWiki.WikiMacroClass WikiMacroClass 0:
weather_by_zip" had its line-feeds removed "in transit". Here it is again,
hopefully intact:
{{script language=groovy}}
def rss = new XmlSlurper().parseText(("
+
xcontext.macro.params.zip).toURL().text)
println rss.channel.title
println "Sunrise: ${rss.channel.astronomy.@sunrise}"
println "Sunset: ${rss.channel.astronomy.@sunset}"
println "Currently:"
println "\t" + rss.channel.item.condition.@date
println "\t" + rss.channel.item.condition.@temp
println "\t" + rss.channel.item.condition.@text
{{/script}}
Same example, in the form of a 2.0 document w/o macro bridge. using
"request.zip" to pass parameter to groovy:
== Groovy Example From [[IBM DeveloperWorks>>
Note: correct usage requres
{{code}}request.zip{{/code}} parameter, e.g
[[92625>>Main.GroovyTest3?zip=92625]] [[92660>>Main.GroovyTest3?zip=92660]]
[[10001>>Main.GroovyTest3?zip=10001]]...
{{groovy}}
def rss = new XmlSlurper().parseText(("
+ request.zip).toURL().text)
println rss.channel.title
println "Sunrise: ${rss.channel.astronomy.@sunrise}"
println "Sunset: ${rss.channel.astronomy.@sunset}"
println "Currently:"
println "\t" + rss.channel.item.condition.@date
println "\t" + rss.channel.item.condition.@temp
println "\t" + rss.channel.item.condition.@text
{{/groovy}}
-- Niels
http://nielsmayer.com