Search in sources :

Example 36 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecute.

// Tests
/**
 * Test normal wiki macro execution.
 */
@Test
public void testExecute() throws Exception {
    registerWikiMacro("wikimacro1", "This is **bold**", Syntax.XWIKI_2_0);
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("{{wikimacro1 param1=\"value1\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<p>This is <strong>bold</strong></p>", printer.toString());
    Assert.assertFalse(this.xcontext.containsKey("macro"));
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Example 37 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecuteWhenWikiMacroDirectlyProvideTheResult.

/**
 * A wiki macro can directly provide the list of blocks instead of having to render them to let
 * {@link DefaultWikiMacro} re-parse it.
 */
@Test
public void testExecuteWhenWikiMacroDirectlyProvideTheResult() throws Exception {
    registerWikiMacro("wikimacrowithresult", "{{groovy}}" + "xcontext.macro.result = java.util.Collections.singletonList(new org.xwiki.rendering.block.WordBlock(xcontext.macro.params.param1));" + "{{/groovy}}", Syntax.XWIKI_2_0);
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    // Note: We're putting the macro after the "Hello" text to force it as an inline macro.
    converter.convert(new StringReader("Hello {{wikimacrowithresult param1=\"World\" param2=\"param2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<p>Hello World</p>", printer.toString());
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Example 38 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testGetCurrentMacroBlock.

@Test
public void testGetCurrentMacroBlock() throws Exception {
    registerWikiMacro("wikimacro", "{{groovy}}" + "println xcontext.macro.context.getCurrentMacroBlock().id\n" + "println xcontext.macro.context.getCurrentMacroBlock().parent.class\n" + "println xcontext.macro.context.getCurrentMacroBlock().nextSibling.children[0].word\n" + "println xcontext.macro.context.getCurrentMacroBlock().previousSibling.children[0].word\n" + "{{/groovy}}", Syntax.XWIKI_2_0, Collections.<WikiMacroParameterDescriptor>emptyList());
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("before\n\n{{wikimacro/}}\n\nafter"), Syntax.XWIKI_2_0, Syntax.PLAIN_1_0, printer);
    Assert.assertEquals("before" + "\n\n" + "wikimacro\n" + "class org.xwiki.rendering.block.XDOM\n" + "after\n" + "before" + "\n\n" + "after", printer.toString());
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Example 39 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method testExecuteWhenWithDifferentMacroSyntax.

/**
 * Check that macro used inside wiki macro are executed with the right syntax.
 */
@Test
public void testExecuteWhenWithDifferentMacroSyntax() throws Exception {
    registerWikiMacro("wikimacro", "{{groovy}}println \"[[path:/some/path]]\"{{/groovy}}", Syntax.XWIKI_2_1);
    Converter converter = getComponentManager().getInstance(Converter.class);
    DefaultWikiPrinter printer = new DefaultWikiPrinter();
    converter.convert(new StringReader("{{wikimacro param1=\"value1\" param2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.XHTML_1_0, printer);
    // Note: We're using XHTML as the output syntax just to make it easy for asserting.
    Assert.assertEquals("<p><span class=\"wikiinternallink\"><a href=\"/some/path\">" + "<span class=\"wikigeneratedlinkcontent\">/some/path</span></a></span></p>", printer.toString());
}
Also used : DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) Test(org.junit.Test)

Example 40 with DefaultWikiPrinter

use of org.xwiki.rendering.renderer.printer.DefaultWikiPrinter in project xwiki-platform by xwiki.

the class DefaultContextualLocalizationManager method getTranslationPlain.

@Override
public String getTranslationPlain(String key, Object... parameters) {
    Translation translation = getTranslation(key);
    if (translation == null) {
        return null;
    }
    Block block = translation.render(parameters);
    DefaultWikiPrinter wikiPrinter = new DefaultWikiPrinter();
    this.plainRenderer.render(block, wikiPrinter);
    return wikiPrinter.toString();
}
Also used : Translation(org.xwiki.localization.Translation) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) Block(org.xwiki.rendering.block.Block)

Aggregations

DefaultWikiPrinter (org.xwiki.rendering.renderer.printer.DefaultWikiPrinter)41 WikiPrinter (org.xwiki.rendering.renderer.printer.WikiPrinter)28 StringReader (java.io.StringReader)18 Test (org.junit.Test)12 Converter (org.xwiki.rendering.converter.Converter)12 XDOM (org.xwiki.rendering.block.XDOM)10 BlockRenderer (org.xwiki.rendering.renderer.BlockRenderer)8 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)7 Block (org.xwiki.rendering.block.Block)6 ParseException (org.xwiki.rendering.parser.ParseException)5 Parser (org.xwiki.rendering.parser.Parser)5 TransformationException (org.xwiki.rendering.transformation.TransformationException)4 XWikiException (com.xpn.xwiki.XWikiException)3 HashMap (java.util.HashMap)3 Expectations (org.jmock.Expectations)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 TransformationContext (org.xwiki.rendering.transformation.TransformationContext)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 Reader (java.io.Reader)2