RenderingModule

Version 28.10 by Vincent Massol on 2009/06/04 10:01
Warning: For security reasons, the document is displayed in restricted mode as it is not the current version. There may be differences and errors due to this.

Failed to execute the [velocity] macro. Cause: [The execution of the [velocity] script macro is not allowed in [extensions:Extension.Rendering Module.WebHome]. Check the rights of its last author or the parameters if it's rendered from another script.]. Click on this message for details.

Rendering Module

This module is in charge of converting textual input in a given syntax into some rendered output. It's used in XWiki notably for rendering pages in XHTML, for performing syntax conversions, for performing page refactorings and to provide users with access to the page's structured information directly inside their wiki pages using one of the supported scripting macros.

Features

  • Parsers for multiple syntaxes
  • Round trip between XWiki Syntax 2.0 and XHTML. This features allows us to have a strong WYSIWYG editor that doesn't loose information when editing wiki pages. It also allows us to import Office documents into XWiki without loosing information.
  • Macro support.
  • Ability to get the result of the parsing as an AST tree (called XDOM) which can then be used to get access to all structured elements from the flat text input.
  • Supports wiki syntax in link labels even for syntaxes that don't support it.
  • Automatic conversion from any of the supported input syntaxes to XWiki Syntax 2.0 or to XHTML.

General Architecture

rendering.png

  • Parser: Parses some textual input in a given syntax and generate a XDOM object which is an AST representing the input into structured blocks.
  • Renderer: Takes a XDOM as input and generates some output.
  • Transformation: Takes some XDOM and modifies it to generate modified XDOM. One important Transformation registered by default is the MacroTransformation which looks for all Macro Blocks in the XDOM object and replaces them by blocks generated by the various Macros. Note that executing Transformations is an optional step and if you don't run them then you'll get a XDOM object without any transformation applied on it (e.g. without Macros executed).
  • Macro: Takes a Macro definition as input and generates XDOM Blocks.

Supported Syntaxes

Input Syntaxes:

Output Syntaxes:

  • XWiki Syntax 2.0
  • XHTML
  • Plain Text: Print all than can be rendered in a simple notepad-like editor such as words, special symbols and spaces. It also generates link labels for links that have no labels and print the generated labels. Last it provides very basic formatting (e.g. separates paragraphs with new lines and separates list items with new lines).

Example

// Parse xwiki 2.0 syntax
Parser parser = componentManager.lookup(Parser.class, Syntax.XWIKI_2_0.toIdString());
XDOM xdom = parser.parse(new StringReader("This is **bold** {{code language=\"java\"}}something{{/code}}"));
       
// Run macros (and any other registered Transformations)
TransformationManager txManager = (TransformationManager) componentManager.lookup(TransformationManager.class);
txManager.performTransformations(xdom, parser.getSyntax());

// Generate XHTML (for example)
WikiPrinter printer = new DefaultWikiPrinter();
PrintRendererFactory prf = componentManager.lookup(PrintRendererFactory.class);
Renderer htmlRenderer = prf.createRenderer(Syntax.XHTML_1_0, printer);

// Perform the rendering        
xdom.traverse(htmlRenderer);

assertEquals("<p>This is <strong>bold</strong> <!--startmacro:code|-|language=\"java\"|-|something-->"
   + "<span class=\"box code\">something</span><!--stopmacro--></p>", printer.toString());

Note that in this example the generated XHTML contains a comment where the Code Macro was used. This to allow the XHTML parser to be able to regenerate the exact same XDOM object as the original input in XWiki 2.0 Syntax. This is what allows us to do round tripping between XHTML and wiki syntaxes.

Adding a new Syntax

Adding support for a new syntax (i.e. the ability to write page contents using a new syntax) is as easy as implementing a Parser. To do so simply implement the Parser interface and register it as a component against the Component Manager.

Example:

public class MyParser implements Parser
{
   private static final Syntax SYNTAX = new Syntax(SyntaxType.getSyntaxType("mysyntax"), "1.0");

   public Syntax getSyntax()
   {
       return SYNTAX;
   }

   public XDOM parse(Reader source) throws ParseException
   {
        XDOM xdom = new XDOM(Collections.singletonList(new WordBlock("amazing")));
       return xdom;
   }
}

Adding a new Macro

In order to implement a new Macro you'll need to write 2 classes:

  • One that is a pure Java Bean and that represents the parameters allowed for that macro, including mandatory parameters, default values, parameter descriptions. An intance of this class will be automagically populated when the user calls the macro in wiki syntax.
  • Another one that is the Macro itself. This class should implement the Macro interface or for simplicity extend AbstractMacro (it does more of the heavy lifting for you). 

Then you'll need to register the Macro class with the Component Manager so that it can be called from a page. For example if you've registered your macro under the "mymacro" hint, you'll be able to call:

{{mymacro param1="value" myEnum="VALUE1"/}}

 

Example of a Macro Parameters class (notice the annotations):

public class MyMacroParameters
{
   public enum MyEnum { VALUE1, VALUE2; }

   private MyEnum myEnum;
   
   private String param1;

   @ParameterMandatory
   @ParameterDescription("param explanation here")
   public void setParam1(String param1)
   {
       this.param1 = param1;
   }

   public String getParam1()
   {
       return this.param1;
   }
   
   @ParameterDescription("explanations here")
   public void setMyEnum(MyEnum myEnum)
   {
       this.myEnum = myEnum;
   }

   public MyEnum getMyEnum()
   {
       return this.myEnum;
   }
}

 

Example of a Macro class:

public class MyMacro extends AbstractMacro<MyMacroParameters>
{
   public MyMacro()
   {
       super(new DefaultMacroDescriptor("Macro description", MyMacroParameters.class));
        registerConverter(new EnumConverter(MyEnum.class), MyEnum.class);
   }

   public List<Block> execute(MyMacroParameters parameters, String content, MacroTransformationContext context)
       throws MacroExecutionException
   {
       return Collections.<Block>singletonList(new WordBlock("hello"));
   }

   public boolean supportsInlineMode()
   {
       return true;
   }
}

More details:

  • Macros can support being put inline or not. If supportsInlineMode() returns true then the macro can be placed in inline text, for example in between words in a paragraph.
  • Macros are executed with a priority. By default your macro will run with a default priority, if you want to explicitely set the priority, call the setPriority() method in your constructor. Low values are executed before (default value is 1000). 

Adding a new Transformation

At the time of this writing, the Transformation mechanism has not been finalized yet. While it's possible to write new Transformations the problem is that we don't yet have a generic marker mechanism in place so that the WYSIWYG editor considers transformed content as read only. If you're interested in this topic follow this JIRA issue.

Get Connected