This page last changed on Jan 17, 2007 by jnolen.
Code Formatting plugin modules are available in Confluence 2.2 and later versions

Code formatting plugin modules allow you to add new languages to the {code} macro. Whenever the code macro is invoked, the macro checks the 'language' parameter against the languages supported by the available formatting plugins, and uses that plugin to format the source code.

Code Formatting Plugins

Here is an example atlassian-plugin.xml file containing a single code formatter:

<atlassian-plugin name="My Formatter" key="confluence.extra.formatters">
    ...
    <codeformatter name="ruby" key="ruby" class="com.example.confluence.formatters.RubyFormatter">
        <description>Code formatter for the Ruby programming language</description>
    </codeformatter>
    ...
</atlassian-plugin>
  • the class attribute defines the class that will be added to the available formatters. This class must implement com.atlassian.renderer.v2.macro.code.SourceCodeFormatter

The SourceCodeFormatter Interface

All code formatters must implement the following simple interface:

package com.atlassian.renderer.v2.macro.code;

/**
 * Strategy for converting a block of source code into pretty-printed HTML. SourceCodeFormatters MUST be forgiving:
 * they will be dealing with user-supplied input, so they can't afford to blow up on bad data.
 */
public interface SourceCodeFormatter
{
    /**
     * Inform the CodeMacro which languages this formatter supports. So if someone writes {code:java}, then only
     * the formatter that returns "java" from this method will be used to format it.
     *
     * @return an array of languages that this formatter supports
     */
    String[] getSupportedLanguages();

    /**
     * Convert source code into HTML.
     *
     * @param code the source code as a string
     * @param language the programming language that it is believed this code is written in
     * @return the source code formatted as HTML
     */
    String format(String code, String language);
}

Formatter Priority

There is no concept of priority for formatters. If two formatters are installed and both return the same value from getSupportedLanguages(), one will be selected pretty much at random. If you want to avoid this behaviour, deactivate formatters that you no longer want to use.

I'm having requests for new code formatting macros, but can not find anywhere to tell me exactly how to create them. I have syntax examples from other programs such as Geshi and EditPad, but don't know how to write a "plugin" to get it into confluence.

Posted by tom.moore@sas.com at May 26, 2006 08:12

Tom,

The best place to start is the Writing Confluence Plugins page in the documentation. You might also find it helpful to take a look at existing plugins. You can find the source code to most of the existing Confluence macros in the plugin folder within your Confluence installation.

Cheers,
Jens

Posted by jens@atlassian.com at May 29, 2006 21:51

Tom - I just released a new code macro which is easier to write highlighting additions for. It also supports more languages. Which languages did you need? Let me know if you have any problems.

Posted by mike@atlassian.com at May 30, 2006 21:16

The new code plugin is awesome - is it going to replace the old one in the next Conf release?

I was extremely pleased to see that it now outputs it's styles on to the page where it's used rather than bloating the main Confluence style sheet - if it does replace the old one in the next version of Confluence, can those old styles be removed to shrink the main CSS and make notification emails a little less bloated?

Posted by gfraser at May 30, 2006 21:26

I noticed in some of the comments that some folks had problems with printing and that it is missing some of the features of the original.  Is this still the case?  I'm assumming that this plugin (Charles') won't be lacking those features.  I've looked into creating a plugin but I'm not a java developer and some of the instructions assumed knowledge I don't have.   It'd be nice if the Eclipse instructions were as fleshed out as the IDEA instructions.

Posted by solitaryrpr at Jun 21, 2006 16:03
Document generated by Confluence on Mar 22, 2007 21:00