Posts mit dem Label Confluence werden angezeigt. Alle Posts anzeigen
Posts mit dem Label Confluence werden angezeigt. Alle Posts anzeigen

Freitag, 21. Januar 2011

Confluence: Include previous versions of a page inside another page

The company I work for had an demand for a confluence plugin/macro that allows to inline pages of older versions. The standard Atlassian include macro just allows to include the most recent version of a page.
With some digging in the Atlassian confluence documentation and with some helpful comments i figured it out by myself and wrote a macro for it.

The macro is based on the render macro from James Mortimer (thx for it) and a code snippet from Azwandi Mohd Aris from Atlassian.

Below is the source of the macro. Just create a new user macro which outputs HTML and is set to have no body.


## declare render macro
## thanks to James Mortimer
## see http://confluence.atlassian.com/display/DISC/Render+any+text+from+a+user+macro
#macro(trimp $xhtml)
## remove leading and trailing 
#if($xhtml.startsWith("",0) && $xhtml.endsWith("
"))
#set($len=$xhtml.length()-$generalUtil.convertToInteger(4))
#set($xhtml=$xhtml.substring(3,$len))
#end
$xhtml
#end
#macro(render $wiki)
#set($globalHelper=$action.getHelper())
#if($content)## i. e. we render a normal page
#set($renderer=$globalHelper.getWikiStyleRenderer())
#set($context=$content.toPageContext())
#set($xhtml=$renderer.convertWikiToXHtml($context, $wiki).replaceAll("\n|\r",""))
#else## we are e. g. in Global Template Preview
#set($xhtml=$globalHelper.renderConfluenceMacro($wiki).replaceAll("\n|\r",""))
#end
#trimp(${xhtml})
#end


## get pagemanager
## thants to Azwandi Mohd Aris [Atlassian]
## see http://confluence.atlassian.com/display/DOC/Include+Page+Macro?focusedCommentId=218279433#comment-218279433
#set($containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager'))
#set($getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null))
#set($containerManager=$getInstanceMethod.invoke(null,null))
#set($containerContext=$containerManager.containerContext)
#set($pageManager=$containerContext.getComponent('pageManager'))

## parse the parameters

#set($spacekey=$space.key)
#if($paramspace)
#set($spacekey=$paramspace)
#end

#set($pageversion='0')
#if($paramversion)
#set($pageversion=$paramversion)
#end

#set($pagename="undefined")
#if($parampage)
#set($pagename=$parampage)
#end

## parse the versionstring to an int
#set($versionInt=$generalUtil.convertToInteger($pageversion))

#set($helper=$action.getHelper())
#set($page=$pageManager.getPage($spacekey,$pagename))


## only advance if we have a page
#if($page)
#set($historypage=$pageManager.getOtherVersion($page,$versionInt))
#end

#if($historypage.getId()==$content.getId())
$helper.renderConfluenceMacro("{warning}The version '$pageversion' of page '$pagename' in space '$spacekey' is the same page as this one! Please correct the version information used for the include macro.{warning}")
#elseif($historypage)
#render($historypage.getContent())
#else
$helper.renderConfluenceMacro("{warning}Version '$pageversion' of page '$pagename' in space '$spacekey' not found!{warning}")
#end

Usage:
{pageinclude:page=name of the page|version=x[|space=spacekey]} 
The spacekey is optional. By default the spacekey of the current space is used.

Example:
If you want to include the page "Demonstration" in version 3 from the space with the key "myspace" use the following macro statment
{pageinclude:page=Demonstration|version=3|space=myspace}

Important:
Do not surround the pagename and the spacekey with quotes!

Have fun with it! :-)


P.S. Do not try to include the same page and version as the page that includes the macro. I took this into account! ;-)

Dienstag, 9. November 2010

Confluence: Freemind mindmaps embedded in pages

If you want to embed freemind (*.mm) mindmaps into your confluence page (which is not possible by default) just follow these steps and you're ready to go.


mindmap macro setup
  1. Get the Freemind browser applet from here Freemind at Sourceforge
  2. Create a page in your confluence that can be access by an anonymous user and attach the freemindbrowser.jar to it.
  3. Login as Confluence Administrator and create a new user macro
  4. Put the following code into the template textbox:

#set($width= $param1)
#if (!$width)
  #set ($width=600)
#end
#set($height= $param2)
#if (!$height)
  #set ($height=450)
#end
#set($attachmentUrl="$action.getGlobalSettings().getBaseUrl()$content.getAttachmentNamed($param0).getDownloadPathWithoutEncoding()")
<applet code="freemind.main.FreeMindApplet.class" 
    archive="http://your.company.confluence.server/download/attachments/2687383/freemindbrowser.jar"       width="$width" height="$height">
    <param name="type" value="application/x-java-applet;version=1.4">
    <param name="scriptable" value="false">
    <param name="modes" value="freemind.modes.browsemode.BrowseMode">
    <param name="browsemode_initial_map"
         value="$attachmentUrl">
    <param name="initial_mode" value="Browse">
    <param name="selection_method" value="selection_method_direct">
</applet>
Just replace http://your.company.confluence.server/download/attachments/2687383/freemindbrowser.jar with the url of the attached freemindbrowser.jar

Now you can use the macro with the following syntax to embed freemind mindmaps:
{mindmap:<attachment_filename>[|<width>[|<height>]]}

Example:
{mindmap:my_mindmap.mm|650|500}


Enjoy it! :-)