This tutorial gives you the basics on how to build macros. A user adds a macro to content in order to do specific kinds of formatting, include particular kinds of content, and so on.
With a macro, a user adds the macro tag to their document in plain text, then publishes the document to view the macro's result. An example is using the following to display text in red:
{color:red}This is red text.{color}
The result would be something like this:
This is red text.
A macro is one or two tags bracketed by curly braces. They look like this:
The macro at the end of this sentence could embed a {username}.
Or you could format text by {blinking_lights}surrounding it with blinking lights{blinking_lights}.
With just a bit of code, you can add macros to those that Clearspace includes by default. In this tutorial you'll build a simple "Author" macro that displays the name of the person who's logged in. Along the way you'll see the pieces that make up a basic macro – a plugin.xml file and the Java code behind the macro.
For simple macros, you'll really need just two pieces of code:
com.jivesoftware.base.plugin.Macro
. Your implementation includes a render method that Clearspace calls to get the String value for display in the user interface where the user has inserted your macro. If there are attributes (such as the "red" attribute in the {font} macro) above or enclosed text (such as "This is red text.") your code will receive those as well for processing. You'll learn more about those methods in the code included in this tutorial. Note that your
return value must qualify as well-formed XML.You package your macro in a JAR file and deploy it by copying it to the <jiveHome>/plugins directory of your Clearspace instance. You can also install a macro using the admin console at System > Plugins > Add Plugin.
This tutorial is about the basics, so you'll want only a few things to build all the pieces it describes:
That's about it. Check out the next section for a few setup steps, then you can get started writing code.
These setup steps are likely pretty common to other extension work you'll do, whether with macros or plugins or themes.
If you haven't installed Clearspace and used its setup tool, use the following instructions. If you have, you can skip this part. Here are the steps:
To test your macro you'll create a document and add your macro markup to it. This assumes you've set up spaces or communities in your Clearspace instance. You can test with any spaces or communities you like, but you might find it easier to follow the tutorial if you set up as described here:
Linux/Unix
sh start-clearspace.sh
Windows
start-clearspace.bat
http://<hostname>:<portnumber>/<context>/admin
You don't need to add users if you don't have any. You'll log in as a system admin during the tutorial.
Note: At this point, if you don't know where your application log files are, you might want to find them. The logs can be very helpful for debugging. If you're using the standalone distribution, you'll find the log files at <dist_root>/server/logs.
It's easiest to develop plugins such as macros if you have your source and compiled artifacts in certain places. In particular, Clearspace expects your compiled Java classes to live in a classes directory under your plugin's root, your plugin.xml file will need to be at the root, and so on.
This tutorial (and the Ant build file that does with it) assumes you're using the Clearspace standalone distribution. Of course, you can use this tutorial with another Clearspace distribution you've set up for development and testing (such as a Clearspace WAR file deployed to your application server). You can also set a jiveHome directory that's external to the distribution (this is the best practice for production). If you do either of these, you'll want to edit a couple of properties in the included Ant build file:
server.lib.dir property value to point to your application server's lib directory. Note that the property is needed to find the servlet-api.jar file, so you might need to edit the <classpath> element, too.clearspace.lib.dir property value to point to the directory that contains the libraries deployed with Clearspace. This might be at a path such as <app_server_root>/webapps/clearspace/WEB-INF/lib. The build file will resolve JAR file dependencies based on this path.jiveHome property to point to the jiveHome directory you specified when you ran the Clearspace setup tool. The build file will deploy your plugin JAR file to a plugins directory inside jiveHome.<distribution_root>/plugins/plugins/SimpleExamples
ant create.plugin.dirs
Every plugin has one. This file tells Clearspace what's in the plugin – in short, what Clearspace features you're extending – and where to find code and other supporting files your plugin needs (although not necessarily all of them, as you'll see in the next section).
<plugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.jivesoftware.com/schemas/clearspace/2_0/plugin.xsd">
<name>example</name>
<description>A simple macro that displays the name of the enclosing content's author.</description>
<author>Me</author>
<version>1.0.0</version>
<minServerVersion>2.0.0</minServerVersion>
<!--
A macro element indicates a macro included in this plugin. The class
attribute points to the Java implementation included in the classes directory.
The name attribute is the name that will be used in the macro tag in content.
The hasBody attribute tells Clearspace whether to look for content (the "body")
between two macro tags rather than just a single tag. This macro has one tag.
-->
<name="author" hasBody="false"
class="com.jivesoftware.clearspace.plugin.example.macro.AuthorsNameMacro" />
</plugin>
Your plugin class will provide the logic to tell Clearspace what to do when rendering a page that has your macro in it.
com.jivesoftware.clearspace.plugin.example.macro.package com.jivesoftware.clearspace.plugin.example.macro;
import java.util.Map;
import com.jivesoftware.base.plugin.Macro;
import com.jivesoftware.base.plugin.MacroContext;
/**
* A simple macro that displays the name of the logged in user. This macro illustrates how to, well,
* write a simple macro. Essentially, your Macro implementation -- and the render method in
* particular -- provides your macro code with the context it needs to process what the user has
* done with your macro and to return the HTML markup Clearspace needs to display the published
* document containing your macro.
*/
public class AuthorsNameMacro implements Macro {
/**
* Called by Clearspace to get the HTML markup that should be displayed for the macro.
*
* @param body The content between the macro's tags, if any.
* @param parameters Parameters set by the user and passed as part of the macro.
* @param macroContext Information about the context in which the macro is executing.
*/
public String render(String body, Map<String, String> parameters, MacroContext macroContext) {
return encodeInHtmlTag("b", macroContext.getContentObject().getUser().getUsername());
}
/**
* Wraps the content in the HTML tag.
*
* @param tag The HTML tag in which to wrap the content.
* @param content The content to wrap in HTML tags.
* @return The generated HTML.
*/
private String encodeInHtmlTag(String tag, String content) {
String result = String.format("<%1$s class='author-name'>%2$s</%1$s>", tag, content);
return result;
}
}
Use Ant to compile and package the code into a JAR file, then copy your macro into the <jiveHome>/plugins directory.
ant build.plugins
ant deploy.plugins
Here's a test of a new macro.
{author} says hello, world!Here's a test of a new macro.
Me says hello, world!That's it for building a simple macro! Be sure to check out more content and samples on Jivespace.