Commits

Steve Hetland  committed 3f81037

Initial checkin of confluence 3x macro

  • Participants

Comments (0)

Files changed (7)

+Copyright (c) 2012, Atlassian Pty Ltd
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
+      documentation and/or other materials provided with the distribution.
+    * Neither the name of Atlassian nor the names of its contributors may be used to endorse or promote products derived from this software without
+      specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGE.
+This code is used for the "writing a macro using JSON" tutorial for Confluence, which
+can be found here:
+https://developer.atlassian.com/display/CONFDEV/Writing+Macros+for+pre-4.0+versions+of+Confluence
+Good luck and have fun!

File TutorialMacro/pom.xml

+<?xml version="1.0" encoding="UTF-8"?>
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.atlassian.plugins.tutorial</groupId>
+    <artifactId>confluence-macro-tutorial-plugin</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <name>Confluence Macro Tutorial</name>
+    <description>This is the com.atlassian.test:conf-test plugin for Atlassian Confluence.</description>
+    <packaging>atlassian-plugin</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <version>4.6</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.atlassian.confluence</groupId>
+            <artifactId>confluence</artifactId>
+            <version>${confluence.version}</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>com.atlassian.confluence.plugin</groupId>
+            <artifactId>func-test</artifactId>
+            <version>2.1.1</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>com.atlassian.maven.plugins</groupId>
+                <artifactId>maven-confluence-plugin</artifactId>
+                <version>3.0.6</version>
+                <extensions>true</extensions>
+                <configuration>
+                    <productVersion>${confluence.version}</productVersion>
+                    <productDataVersion>${confluence.data.version}</productDataVersion>
+                </configuration>
+            </plugin>
+            <plugin>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <configuration>
+                    <source>1.5</source>
+                    <target>1.5</target>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <properties>
+        <confluence.version>3.0.1</confluence.version>
+        <confluence.data.version>3.0</confluence.data.version>
+    </properties>
+
+</project>

File TutorialMacro/src/main/java/com/atlassian/plugins/tutorial/macro/TutorialMacro.java

+package com.atlassian.plugins.tutorial.macro;
+
+import java.util.Map;
+import java.util.List;
+import java.util.Random;
+
+import com.atlassian.renderer.RenderContext;
+import com.atlassian.renderer.v2.macro.BaseMacro;
+import com.atlassian.renderer.v2.macro.MacroException;
+import com.atlassian.renderer.v2.RenderMode;
+import com.atlassian.confluence.pages.Page;
+import com.atlassian.confluence.spaces.SpaceManager;
+import com.atlassian.confluence.spaces.Space;
+import com.atlassian.confluence.user.AuthenticatedUserThreadLocal;
+import com.atlassian.confluence.renderer.radeox.macros.MacroUtils;
+import com.atlassian.confluence.util.velocity.VelocityUtils;
+import com.atlassian.user.User;
+
+/**
+ * A macro showing the basics of macro writing in Confluence.
+ */
+public class TutorialMacro extends BaseMacro
+{
+    private static final String MACRO_BODY_TEMPLATE = "templates/tutorial-macro.vm";
+
+    private final SpaceManager spaceManager;
+
+    /**
+     * Constructor. When the plugin containing the macro is activated, Confluence
+     * will instantiate this class and automatically inject an implementation
+     * of {@code SpaceManager}.
+     * @param spaceManager the {@code SpaceManager} implementation to use
+     */
+    public TutorialMacro(SpaceManager spaceManager)
+    {
+        this.spaceManager = spaceManager;
+    }
+
+    public boolean isInline()
+    {
+        // This macro is meant to render outside of a block element, so return
+        // false.
+        return false;
+    }
+
+    public boolean hasBody()
+    {
+        // This macro isn't meant to have any body text, so return false.
+        return false;
+    }
+
+    public RenderMode getBodyRenderMode()
+    {
+        // This macro uses Velocity to render finished HTML, so no further
+        // renderer massaging should be performed on the final result.
+        return RenderMode.NO_RENDER;
+    }
+
+    public String execute(Map params, String body, RenderContext renderContext)
+            throws MacroException
+    {
+        // create a Velocity context object as a model between the controller
+        // (this macro) and the view (the Velocity template)
+        Map<String, Object> context = MacroUtils.defaultVelocityContext();
+
+        // check if the user supplied a "greeting" parameter
+        if (params.containsKey("greeting"))
+        {
+            context.put("greeting", params.get("greeting"));
+        }
+        else
+        {
+            // we'll construct one. get the currently logged in user and display
+            // their name
+            User user = AuthenticatedUserThreadLocal.getUser();
+            if (user != null)
+            {
+                context.put("greeting", "Hello " + user.getFullName());
+            }
+        }
+
+        // get all spaces in this installation
+        @SuppressWarnings("unchecked")
+            List<Space> spaces = spaceManager.getAllSpaces();
+        context.put("totalSpaces", spaces.size());
+
+        if (!spaces.isEmpty())
+        {
+            // pick a space at random and find its home page
+            Random random = new Random();
+            int randomSpaceIndex = random.nextInt(spaces.size());
+            Space randomSpace = spaces.get(randomSpaceIndex);
+            context.put("spaceName", randomSpace.getName());
+
+            Page homePage = randomSpace.getHomePage();
+            context.put("homePageTitle", homePage.getTitle());
+            context.put("homePageCreator", homePage.getCreatorName());
+        }
+
+        // render the Velocity template with the assembled context
+        return VelocityUtils.getRenderedTemplate(MACRO_BODY_TEMPLATE, context);
+
+    }
+
+}

File TutorialMacro/src/main/resources/atlassian-plugin.xml

+<atlassian-plugin key="confluence-macro-tutorial-plugin"
+                  name="Confluence Macro Tutorial Plugin" plugins-version="2">
+    <plugin-info>
+        <description>A sample plugin showing how to write Confluence macros.</description>
+        <version>1.0</version>
+        <vendor name="Atlassian" url="http://www.atlassian.com" />
+    </plugin-info>
+
+    <!-- Registers the macro in a plugin module. -->
+    <macro name="tutorial-macro" class="com.atlassian.plugins.tutorial.macro.TutorialMacro" key="tutorial-macro">
+        <description>Demonstrates Confluence dependency injection and Velocity output.</description>
+        <!-- Provides help text for the Confluence notation guide. -->
+        <resource type="velocity" name="help" location="templates/tutorial-macro-help.vm">
+            <param name="help-section" value="confluence"/>
+        </resource>
+        <!-- Specifies which macro browser category this macro should display in. -->
+        <category name="confluence-content"/>
+        <!-- Defines the parameters this macro may consume. -->
+        <parameters>
+            <parameter name="greeting" type="string"/>
+        </parameters>
+    </macro>
+</atlassian-plugin>

File TutorialMacro/src/main/resources/templates/tutorial-macro-help.vm

+<tr>
+    <td>
+        {tutorial-macro}
+        <br><br>
+        {tutorial-macro:greeting=Hello from the macro!}
+    </td>
+
+    <td>
+        <p>A macro provided by the Confluence Macro Tutorial.</p>
+
+        <ul>
+            <li>greeting - An optional message to show at the top of the macro.</li>
+        </ul>
+    </td>
+</tr>

File TutorialMacro/src/main/resources/templates/tutorial-macro.vm

+<table border="1">
+    <tr>
+        <td>
+            #if ($greeting)
+                <p><strong>$greeting</strong></p>
+            #end
+
+            #if ($totalSpaces == 0)
+                <p>No spaces found in this installation!</p>
+            #else
+                <p>Of the $totalSpaces spaces in this installation, the random winner is...</p>
+
+                <strong>$spaceName!</strong>
+
+                <p>Its home page is named $homePageTitle, and it was created by $homePageCreator.</p>
+            #end
+
+            <p>Thanks for playing!</p>
+
+            <p>(Everything in this border was generated by {tutorial-macro}.)</p>                
+        </td>
+    </tr>
+</table>