In the "good old days" of Confluence plugin development, when plugins add-ons were still called plugins, people used to throw wiki markup into themes and plugins with wild abandon.
Just like this:
$helper.renderConfluenceMacro("{info}*This* _is_ the cheese macro: {cheese}{info}")
With the arrival of the XML storage format and the new XHTML Macros in Confluence 4, this kind of thing no longer worked. Newer macros couldn't be expressed in wiki markup, so this handy method of rendering wiki-markup became redundant.
Modernise
Some old hands are still nostalgic, but times change. Actually, that old renderConfluenceMacro(…) method was kind of cool. I'd still like to be able to use it or an equivalent from time to time. So why not roll your own an updated equivalent?
To modernise, you need to take a look at the storage format and see how Confluence renders storage into the view format (HTML).
Then you can add the helper class:
package com.appfusions.confluence.addon.theme.helper;
import com.atlassian.confluence.content.render.xhtml.ConversionContext;
import com.atlassian.confluence.content.render.xhtml.DefaultConversionContext;
import com.atlassian.confluence.content.render.xhtml.XhtmlException;
import com.atlassian.confluence.pages.Page;
import com.atlassian.confluence.xhtml.api.XhtmlContent;
import javax.xml.stream.XMLStreamException;
public class MyVelocityHelper
{
private XhtmlContent xhtmlContent;
/**
* A little trick so that I can use:
* $myHelper.convertStorageToView('<ac:structured-macro ac:name="cheese"></ac:structured-macro>;')
*
* @param storage
* @return
*/
@HtmlSafe
public String convertStorageToView(String storage)
{
Page page = new Page();
String view = "";
try
{
final ConversionContext conversionContext = new DefaultConversionContext(page.toPageContext());
view = xhtmlContent.convertStorageToView(storage, conversionContext);
}
catch (XhtmlException e)
{
e.printStackTrace();
}
catch (XMLStreamException e)
{
e.printStackTrace();
}
return view;
}
public void setXhtmlContent(XhtmlContent xhtmlContent) {
this.xhtmlContent = xhtmlContent;
}
}
You'll need a velocity context module to add an object to the velocity context.
Add this to atlassian-plugin.xml:
<velocity-context-item key="myHelper" name="My Velocity Helper" class="com.appfusions.confluence.addon.theme.helper.MyVelocityHelper" context-key="myHelper" />;
Once that's done, you can start using a more modern equivalent to renderConfluenceMacro(…):
$myHelper.convertStorageToView('<ac:structured-macro ac:name="info"><ac:rich-text-body><p><strong>This</strong> <em>is</em> the cheese macro: <ac:structured-macro ac:name="cheese" /></p></ac:rich-text-body></ac:structured-macro>');
And relax.
Update: I added the @HtmlSafe
annotation to the convertStorageToView
so that the HTML is not escaped.