One of many open source products that has helped to make Splatforms.com what it is today is Apache Velocity. One of the most powerful features of Velocity is the ability to create reusable functions known as Velocimacros. The Splatforms Task specification provides a section in the Task definition where Task Developers can include any number of Velocimacros needed during the Task processing. To get an idea of the kinds of things that you can do with a Velocimacro, check out the Velocimacros section of the Velocity User’s Guide on the Apache Velocity web site.
Most of the web sites that serve as a destination target for the data found in a Link *form accept “tags” as one of their input fields, and most of those accept the tags input as a single, comma-delimited string. A single comma-delimited string is the expected input from a Link *form, so passing on the tags to most target destinations is simply matter of sending the user’s input as is. For those sites that has a field length limitation, a simple #crop Velocimacro will handle that requirement. But there are a few sites, such as Delicious and Trunk.ly, that expect their tag string input to be delimited by spaces, not commas. For that, we need little more sophisticated processing, but something that a Velocimacro can easily handle.
#macro(fixTags $tagString) #if ($tagString) #set ($separator = "") #set ($tagArray = $tagString.split(",")) #foreach ($tag in $tagArray) #set ($tag = $tag.trim()) #set ($tag = $tag.replace(" ","_")) $separator$tag #set($separator = " ") #end #end #end
The code fairly self-explanatory, but let’s break it down, just for fun:
This is the macro definition; the first argument inside of the parenthesis is the name of the macro, which is used to invoke it, and the second is the single argument that will be passed to the macro at execution time. To invoke the #fixTags macro, you would type:
The next line just verifies that there are, in fact, tags to be processed:
While this is not technically required, there is no point in going through the rest of the process if the tag string is empty, so this screens out all of the calls made where there are no tags.
The next couple of lines set up an empty string for the first tag separator, and split the comma-separated string in an array of strings:
#set ($separator = "") #set ($tagArray = $tagString.split(","))
Once we have an array, we can use a for loop to process each item in the array individually:
#foreach ($tag in $tagArray) #set ($tag = $tag.trim()) #set ($tag = $tag.replace(" ","_")) $separator$tag #set($separator = " ") #end
Inside the loop, for every tag, we trim off any excess white space, convert any spaces to underscores, and then output the separator and the cleaned-up tag. Once that’s done, we convert the separator to a single space for the next time through (the first time only, the separator is an empty string, as we do not want to start out with a space).
That’s about all there is to it. If you want to see another example of how Velocimacros can be used in a Splatform Task, check out the Spintax Velocimacro. And if you really want to have some fun, sign up as Task Developer and try it out for yourself!