Permalink
Browse files

adds some api for applying transformations before macros

  • Loading branch information...
1 parent bf0b0c4 commit f3d894db0affa994ad71c2eba56672a6741d3437 @literalice committed Jan 20, 2013
View
163 src/java/com/monochromeroad/grails/plugins/xwiki/XWikiRenderer.java
@@ -6,6 +6,7 @@
import org.xwiki.rendering.transformation.Transformation;
import java.io.*;
+import java.util.Collections;
/**
* XWiki Rendering System -- XDOM based
@@ -41,12 +42,38 @@ public XWikiRenderer(XWikiComponentManager componentManager, XWikiConfigurationP
* @param source source text reader
* @param inputSyntax inputSyntax
* @param outputSyntax outputSyntax
+ * @return a rendered result
+ */
+ public String render(Reader source, Syntax inputSyntax, Syntax outputSyntax) {
+ return render(source, inputSyntax, outputSyntax, Collections.<Transformation>emptyList(), Collections.<Transformation>emptyList());
+ }
+
+ /**
+ * XWiki rendering
+ *
+ * @param source source text reader
+ * @param inputSyntax inputSyntax
+ * @param outputSyntax outputSyntax
* @param transformations transform parameters
* @return a rendered result
*/
- public String render(Reader source, Syntax inputSyntax, Syntax outputSyntax, Transformation ...transformations) {
+ public String render(Reader source, Syntax inputSyntax, Syntax outputSyntax, Iterable<Transformation> transformations) {
+ return render(source, inputSyntax, outputSyntax, Collections.<Transformation>emptyList(), transformations);
+ }
+
+ /**
+ * XWiki rendering
+ *
+ * @param source source text reader
+ * @param inputSyntax inputSyntax
+ * @param outputSyntax outputSyntax
+ * @param preTransformations transform parameters
+ * @param postTransformations transform parameters
+ * @return a rendered result
+ */
+ public String render(Reader source, Syntax inputSyntax, Syntax outputSyntax, Iterable<Transformation> preTransformations, Iterable<Transformation> postTransformations) {
XDOM xdom = buildXDOM(source, inputSyntax);
- transform(xdom, inputSyntax, transformations);
+ transform(xdom, inputSyntax, preTransformations, postTransformations);
return convertToString(xdom, outputSyntax);
}
@@ -55,28 +82,90 @@ public String render(Reader source, Syntax inputSyntax, Syntax outputSyntax, Tra
*
* @param source a source text
* @param inputSyntax input syntax
+ * @return a rendered result
+ */
+ public String render(Reader source, Syntax inputSyntax) {
+ Syntax outputSyntax = configurationProvider.getDefaultOutputSyntax();
+ return render(source, inputSyntax, outputSyntax);
+ }
+
+ /**
+ * XWiki XHTML rendering
+ *
+ * @param source a source text
+ * @param inputSyntax input syntax
* @param transformations transform parameters
* @return a rendered result
*/
- public String render(Reader source, Syntax inputSyntax, Transformation ...transformations) {
+ public String render(Reader source, Syntax inputSyntax, Iterable<Transformation> transformations) {
Syntax outputSyntax = configurationProvider.getDefaultOutputSyntax();
return render(source, inputSyntax, outputSyntax, transformations);
}
/**
+ * XWiki XHTML rendering
+ *
+ * @param source a source text
+ * @param inputSyntax input syntax
+ * @param preTransformations transform parameters
+ * @return a rendered result
+ */
+ public String render(Reader source, Syntax inputSyntax, Iterable<Transformation> preTransformations, Iterable<Transformation> postTransformations) {
+ Syntax outputSyntax = configurationProvider.getDefaultOutputSyntax();
+ return render(source, inputSyntax, outputSyntax, preTransformations, postTransformations);
+ }
+
+ /**
+ * XWiki XHTML rendering using default syntax.
+ *
+ * @param source a source text
+ * @return a rendered result
+ */
+ public String render(Reader source) {
+ Syntax inputSyntax = configurationProvider.getDefaultInputSyntax();
+ Syntax outputSyntax = configurationProvider.getDefaultOutputSyntax();
+ return render(source, inputSyntax, outputSyntax);
+ }
+
+ /**
* XWiki XHTML rendering using default syntax.
*
* @param source a source text
* @param transformations transform parameters
* @return a rendered result
*/
- public String render(Reader source, Transformation ...transformations) {
+ public String render(Reader source, Iterable<Transformation> transformations) {
Syntax inputSyntax = configurationProvider.getDefaultInputSyntax();
Syntax outputSyntax = configurationProvider.getDefaultOutputSyntax();
return render(source, inputSyntax, outputSyntax, transformations);
}
/**
+ * XWiki XHTML rendering using default syntax.
+ *
+ * @param source a source text
+ * @param preTransformations transform parameters
+ * @return a rendered result
+ */
+ public String render(Reader source, Iterable<Transformation> preTransformations, Iterable<Transformation> postTransformations) {
+ Syntax inputSyntax = configurationProvider.getDefaultInputSyntax();
+ Syntax outputSyntax = configurationProvider.getDefaultOutputSyntax();
+ return render(source, inputSyntax, outputSyntax, preTransformations, postTransformations);
+ }
+
+ /**
+ * XWiki rendering
+ *
+ * @param source a source text
+ * @param inputSyntax inputSyntax
+ * @param outputSyntax outputSyntax
+ * @return a rendered result
+ */
+ public String render(String source, Syntax inputSyntax, Syntax outputSyntax) {
+ return render(new StringReader(source), inputSyntax, outputSyntax);
+ }
+
+ /**
* XWiki rendering
*
* @param source a source text
@@ -85,33 +174,91 @@ public String render(Reader source, Transformation ...transformations) {
* @param transformations transform parameters
* @return a rendered result
*/
- public String render(String source, Syntax inputSyntax, Syntax outputSyntax, Transformation ...transformations) {
+ public String render(String source, Syntax inputSyntax, Syntax outputSyntax, Iterable<Transformation> transformations) {
return render(new StringReader(source), inputSyntax, outputSyntax, transformations);
}
/**
+ * XWiki rendering
+ *
+ * @param source a source text
+ * @param inputSyntax inputSyntax
+ * @param outputSyntax outputSyntax
+ * @param preTransformations transform parameters
+ * @return a rendered result
+ */
+ public String render(String source, Syntax inputSyntax, Syntax outputSyntax, Iterable<Transformation> preTransformations, Iterable<Transformation> postTransformations) {
+ return render(new StringReader(source), inputSyntax, outputSyntax, preTransformations, postTransformations);
+ }
+
+ /**
* XWiki XHTML rendering
*
* @param source a source text
* @param inputSyntax inputSyntax
- * @param transformations transform parameters
* @return a rendered result
*/
- public String render(String source, Syntax inputSyntax, Transformation ...transformations) {
+ public String render(String source, Syntax inputSyntax) {
+ return render(new StringReader(source), inputSyntax);
+ }
+
+ /**
+ * XWiki XHTML rendering
+ *
+ * @param source a source text
+ * @param inputSyntax inputSyntax
+ * @param transformations parameters
+ * @return a rendered result
+ */
+ public String render(String source, Syntax inputSyntax, Iterable<Transformation> transformations) {
return render(new StringReader(source), inputSyntax, transformations);
}
/**
+ * XWiki XHTML rendering
+ *
+ * @param source a source text
+ * @param inputSyntax inputSyntax
+ * @param preTransformations parameters
+ * @param postTransformations parameters
+ * @return a rendered result
+ */
+ public String render(String source, Syntax inputSyntax, Iterable<Transformation> preTransformations, Iterable<Transformation> postTransformations) {
+ return render(new StringReader(source), inputSyntax, preTransformations, postTransformations);
+ }
+
+ /**
+ * XWiki XHTML rendering using default syntax
+ *
+ * @param source a source text
+ * @return a rendered result
+ */
+ public String render(String source) {
+ return render(new StringReader(source));
+ }
+
+ /**
* XWiki XHTML rendering using default syntax
*
* @param source a source text
* @param transformations transform parameters
* @return a rendered result
*/
- public String render(String source, Transformation ...transformations) {
+ public String render(String source, Iterable<Transformation> transformations) {
return render(new StringReader(source), transformations);
}
+ /**
+ * XWiki XHTML rendering using default syntax
+ *
+ * @param source a source text
+ * @param preTransformations transform parameters
+ * @return a rendered result
+ */
+ public String render(String source, Iterable<Transformation> preTransformations, Iterable<Transformation> postTransformations) {
+ return render(new StringReader(source), preTransformations, postTransformations);
+ }
+
private String convertToString(XDOM xdom, Syntax syntax) {
DefaultWikiPrinter printer = new DefaultWikiPrinter();
applyRenderer(xdom, syntax, printer);
View
12 src/java/com/monochromeroad/grails/plugins/xwiki/XWikiRenderingSystem.java
@@ -12,7 +12,6 @@
import org.xwiki.rendering.transformation.TransformationException;
import java.io.Reader;
-import java.util.Arrays;
/**
* Base functions for XWiki rendering system.
@@ -45,18 +44,17 @@ protected final XDOM buildXDOM(Reader source, Syntax input) {
}
}
- protected final void transform(XDOM xdom, Syntax syntax, Transformation ...transformations) {
- transform(xdom, syntax, Arrays.asList(transformations));
- }
-
- protected final void transform(XDOM xdom, Syntax syntax, Iterable<Transformation> transformations) {
+ protected final void transform(XDOM xdom, Syntax syntax, Iterable<Transformation> preTransformations, Iterable<Transformation> postTransformations) {
TransformationContext txContext = new TransformationContext(xdom, syntax);
try {
+ for (Transformation transformation : preTransformations) {
+ transformation.transform(xdom, txContext);
+ }
if (configurationProvider.isMacrosEnabled()) {
getTransformationForMacro().transform(xdom, txContext);
}
- for (Transformation transformation : transformations) {
+ for (Transformation transformation : postTransformations) {
transformation.transform(xdom, txContext);
}
} catch (TransformationException e) {
View
66 src/java/com/monochromeroad/grails/plugins/xwiki/XWikiStreamRenderer.java
@@ -9,7 +9,7 @@
import org.xwiki.rendering.transformation.Transformation;
import java.io.Reader;
-import java.util.*;
+import java.util.Collections;
/**
* XWiki Rendering System -- streaming based
@@ -45,17 +45,31 @@ public XWikiStreamRenderer(XWikiComponentManager componentManager, XWikiConfigur
* @param source source text reader
* @param inputSyntax inputSyntax
* @param outputSyntax outputSyntax
+ * @param preTransformations transform parameters before applying macros
+ * @param postTransformations transform parameters after applying macros
* @param callback a callback function processed on parsing a text
*/
- public void render(Reader source, Syntax inputSyntax, Syntax outputSyntax, Closure callback) {
- if (configurationProvider.isMacrosEnabled()) {
- renderOnXDOM(source, inputSyntax, outputSyntax, Collections.<Transformation>emptyList(), callback);
+ public void render(Reader source, Syntax inputSyntax, Syntax outputSyntax, Iterable<Transformation> preTransformations, Iterable<Transformation> postTransformations, Closure callback) {
+ if (configurationProvider.isMacrosEnabled() || preTransformations.iterator().hasNext() || postTransformations.iterator().hasNext()) {
+ renderOnXDOM(source, inputSyntax, outputSyntax, preTransformations, postTransformations, callback);
} else {
renderOnStream(source, inputSyntax, outputSyntax, callback);
}
}
/**
+ * XWiki rendering
+ *
+ * @param source source text reader
+ * @param inputSyntax inputSyntax
+ * @param outputSyntax outputSyntax
+ * @param callback a callback function processed on parsing a text
+ */
+ public void render(Reader source, Syntax inputSyntax, Syntax outputSyntax, Closure callback) {
+ render(source, inputSyntax, outputSyntax, Collections.<Transformation>emptyList(), Collections.<Transformation>emptyList(), callback);
+ }
+
+ /**
* XWiki XHTML rendering
*
* @param source source text reader
@@ -88,12 +102,8 @@ public void render(Reader source, Closure callback) {
* @param transformations transform parameters
* @param callback a callback function processed on parsing a text
*/
- public void render(Reader source, Syntax inputSyntax, Syntax outputSyntax, List<Transformation> transformations, Closure callback) {
- if (configurationProvider.isMacrosEnabled() || !transformations.isEmpty()) {
- renderOnXDOM(source, inputSyntax, outputSyntax, transformations, callback);
- } else {
- renderOnStream(source, inputSyntax, outputSyntax, callback);
- }
+ public void render(Reader source, Syntax inputSyntax, Syntax outputSyntax, Iterable<Transformation> transformations, Closure callback) {
+ render(source, inputSyntax, outputSyntax, Collections.<Transformation>emptyList(), transformations, callback);
}
/**
@@ -104,27 +114,55 @@ public void render(Reader source, Syntax inputSyntax, Syntax outputSyntax, List<
* @param transformations transform parameters
* @param callback a callback function processed on parsing a text
*/
- public void render(Reader source, Syntax inputSyntax, List<Transformation> transformations, Closure callback) {
+ public void render(Reader source, Syntax inputSyntax, Iterable<Transformation> transformations, Closure callback) {
Syntax outputSyntax = configurationProvider.getDefaultOutputSyntax();
render(source, inputSyntax, outputSyntax, transformations, callback);
}
/**
+ * XWiki XHTML rendering
+ *
+ * @param source source text reader
+ * @param inputSyntax inputSyntax
+ * @param preTransformations transform parameters
+ * @param postTransformations transform parameters
+ * @param callback a callback function processed on parsing a text
+ */
+ public void render(Reader source, Syntax inputSyntax, Iterable<Transformation> preTransformations, Iterable<Transformation> postTransformations, Closure callback) {
+ Syntax outputSyntax = configurationProvider.getDefaultOutputSyntax();
+ render(source, inputSyntax, outputSyntax, preTransformations, postTransformations, callback);
+ }
+
+ /**
* XWiki XHTML rendering using the default syntax
*
* @param source source text reader
* @param transformations transform parameters
* @param callback a callback function processed on parsing a text
*/
- public void render(Reader source, List<Transformation> transformations, Closure callback) {
+ public void render(Reader source, Iterable<Transformation> transformations, Closure callback) {
Syntax inputSyntax = configurationProvider.getDefaultInputSyntax();
Syntax outputSyntax = configurationProvider.getDefaultOutputSyntax();
render(source, inputSyntax, outputSyntax, transformations, callback);
}
- private void renderOnXDOM(Reader source, Syntax inputSyntax, Syntax outputSyntax, List<Transformation> transformations, Closure callback) {
+ /**
+ * XWiki XHTML rendering using the default syntax
+ *
+ * @param source source text reader
+ * @param preTransformations transform parameters
+ * @param postTransformations transform parameters
+ * @param callback a callback function processed on parsing a text
+ */
+ public void render(Reader source, Iterable<Transformation> preTransformations, Iterable<Transformation> postTransformations, Closure callback) {
+ Syntax inputSyntax = configurationProvider.getDefaultInputSyntax();
+ Syntax outputSyntax = configurationProvider.getDefaultOutputSyntax();
+ render(source, inputSyntax, outputSyntax, preTransformations, postTransformations, callback);
+ }
+
+ private void renderOnXDOM(Reader source, Syntax inputSyntax, Syntax outputSyntax, Iterable<Transformation> preTransformations, Iterable<Transformation> postTransformations, Closure callback) {
XDOM xdom = buildXDOM(source, inputSyntax);
- transform(xdom, inputSyntax, transformations);
+ transform(xdom, inputSyntax, preTransformations, postTransformations);
applyRenderer(xdom, outputSyntax, new XWikiCallbackPrinter(callback));
}
View
34 test/unit/com/monochromeroad/grails/plugins/xwiki/TestTransformation.java
@@ -1,34 +0,0 @@
-/**
- * 11/10/20
- *
- * Copyright (c) 2011 Monochromeroad
- */
-package com.monochromeroad.grails.plugins.xwiki;
-
-import org.xwiki.rendering.block.Block;
-import org.xwiki.rendering.block.RawBlock;
-import org.xwiki.rendering.syntax.Syntax;
-import org.xwiki.rendering.transformation.AbstractTransformation;
-import org.xwiki.rendering.transformation.TransformationContext;
-import org.xwiki.rendering.transformation.TransformationException;
-
-public class TestTransformation extends AbstractTransformation {
-
- private String parameter;
-
- private int priority;
-
- public TestTransformation(String parameter, int priority) {
- this.parameter = parameter;
- this.priority = priority;
- }
-
- public void transform(
- Block block, TransformationContext transformationContext) throws TransformationException {
- block.addChild(new RawBlock(parameter, Syntax.XHTML_1_0));
- }
-
- public int getPriority() {
- return this.priority;
- }
-}
View
199 test/unit/com/monochromeroad/grails/plugins/xwiki/XWikiRendererSpec.groovy
@@ -1,8 +1,10 @@
package com.monochromeroad.grails.plugins.xwiki
-import com.monochromeroad.grails.plugins.xwiki.macro.DateMacro
-import com.monochromeroad.grails.plugins.xwiki.macro.CodeMacro
+import com.monochromeroad.grails.plugins.xwiki.macro.SampleMacro
+import com.monochromeroad.grails.plugins.xwiki.macro.SampleNoParameterMacro
+import com.monochromeroad.grails.plugins.xwiki.transformation.SampleTransformation
import org.xwiki.rendering.syntax.Syntax
+import org.xwiki.rendering.transformation.Transformation
import spock.lang.Specification
import spock.lang.Shared
import org.apache.log4j.BasicConfigurator
@@ -15,88 +17,173 @@ class XWikiRendererSpec extends Specification {
@Shared
XWikiRenderer renderer
+ @Shared
+ XWikiStreamRenderer streamRenderer
+
+ @Shared
+ XWikiConfigurationProvider configurationProvider = new XWikiConfigurationProvider()
+
def setupSpec() {
- BasicConfigurator.configure();
+ BasicConfigurator.configure()
- XWikiConfigurationProvider configurationProvider = new XWikiConfigurationProvider();
- XWikiComponentManager componentManager = new XWikiComponentManager(getClass().classLoader);
- componentManager.registerMacro(DateMacro)
- componentManager.registerMacro(CodeMacro)
+ XWikiComponentManager componentManager = new XWikiComponentManager(getClass().classLoader)
+ componentManager.registerMacro(SampleMacro)
+ componentManager.registerMacro(SampleNoParameterMacro)
renderer = new XWikiRenderer(componentManager, configurationProvider)
+ streamRenderer = new XWikiStreamRenderer(componentManager, configurationProvider)
}
- String testXWiki21Text = """
-=level1=
-text :**bold**
-"""
-
- String expectedHTML = '<h1 id="Hlevel1"><span>level1</span></h1><p>text :<strong>bold</strong></p>'
-
- String expectedText = "level1\n\ntext :bold"
+ def expected = "**test** {{sample data='test' /}} {{sample2}}body{{/sample2}}"
- def "Converts wiki text using XWiki syntax"() {
+ def "Renders a wiki text"() {
when:
- def result = renderer.render(new StringReader(testXWiki21Text), Syntax.XWIKI_2_1, Syntax.XHTML_1_0)
+ configurationProvider.macrosEnabled = true
+ def result = assertRendererApi(expected)
+
then:
- result == expectedHTML
+ result == "<p><strong>test</strong> data:test[inserted by a macro: sample] body[inserted by a macro: sample2]</p>"
}
- def "Converts XWiki 2_1 text to another format using XWiki syntax"() {
+ def "Renders a wiki text without macros"() {
when:
- def result = renderer.render(new StringReader(testXWiki21Text), Syntax.XWIKI_2_1, Syntax.PLAIN_1_0)
+ configurationProvider.macrosEnabled = false
+ and:
+ def result = assertRendererApi(expected)
+
then:
- result == expectedText
+ result == "<p><strong>test</strong>&nbsp;&nbsp;</p>"
}
- def "Embedded Macro Support"() {
- String text = """
-= level1 =
-== level2 ==
-{{comment}}
-this line is comment.
-{{/comment}}
-"""
-
+ def "Renders a wiki text with a transformation after applying a macro"() {
when:
- def result = renderer.render(text, Syntax.XWIKI_2_1, Syntax.XHTML_1_0)
- then:"The text written in Comment Macro is not rendered"
- result == '<h1 id="Hlevel1"><span>level1</span></h1><h2 id="Hlevel2"><span>level2</span></h2>'
+ configurationProvider.macrosEnabled = true
+ def result = assertRendererApi(expected, [new SampleTransformation("T1"), new SampleTransformation("T2")])
+
+ then:
+ result.trim() == "<p><strong>test</strong> data:test[inserted by a macro: sample] body[inserted by a macro: sample2]</p>It applies the transformation T1! [.. may be applied a macro.] It applies the transformation T2! [.. may be applied a macro.]"
}
- def "User Macro Support"() {
- String text = """
-{{date /}} {{date}}yyyy-MM-dd{{/date}}
+ def "Renders a wiki text with a transformation after/before applying a macro"() {
+ when:
+ configurationProvider.macrosEnabled = true
+ def result = assertRendererApi(expected, [new SampleTransformation("T1")], [new SampleTransformation("T2")])
-{{code mode="java"}}
-class TestClass{ }
-{{/code}}
-"""
+ then:
+ result.trim() == "<p><strong>test</strong> data:test[inserted by a macro: sample] body[inserted by a macro: sample2]</p>It applies the transformation T1! It applies the transformation T2! [.. may be applied a macro.]"
+ }
+ def "Renders a wiki text with another syntax"() {
when:
- def result = renderer.render(text, Syntax.XWIKI_2_1, Syntax.XHTML_1_0)
- def formattedDate = new Date().format("yyyy/MM/dd")
- def formattedDate2 = new Date().format("yyyy-MM-dd")
+ def result = assertRendererApi("''test'' '''test'''", Syntax.MEDIAWIKI_1_0)
then:
- result == "<p><span class=\"dateMacro\">${formattedDate}</span> <span class=\"dateMacro\">${formattedDate2}</span></p><pre class=\"java\">class TestClass&#123; }</pre>"
+ result == '<p><em>test</em> <strong>test</strong></p>'
}
- def "Extra API: using default input syntax and default output syntax"() {
+ def "Renders a wiki text with another output syntax"() {
when:
- String result = renderer.render(testXWiki21Text)
+ configurationProvider.macrosEnabled = true
+ def result = assertRendererApi(expected, Syntax.XWIKI_2_1, Syntax.PLAIN_1_0)
+
then:
- result == expectedHTML
+ result.trim() == "test data:test[inserted by a macro: sample] body[inserted by a macro: sample2]"
}
- def "Adds some custom transformer"() {
- def transformation1 = new TestTransformation("Pre Transformation", 1);
- def transformation2 = new TestTransformation("Post Transformation", 1000);
+ private String assertRendererApi(String src, Syntax inputSyntax) {
+ def result = renderer.render(src, inputSyntax)
+ assert result == renderer.render(src, inputSyntax, configurationProvider.defaultOutputSyntax)
+ assert result == renderer.render(new StringReader(src), inputSyntax)
+ assert result == renderer.render(new StringReader(src), inputSyntax, configurationProvider.defaultOutputSyntax)
- when:
- String result = renderer.render(
- testXWiki21Text, Syntax.XWIKI_2_1, Syntax.XHTML_1_0, transformation1, transformation2)
- println result
- then:
- result == expectedHTML + "Pre TransformationPost Transformation"
+ def streamResult = ""
+ streamRenderer.render(new StringReader(src), inputSyntax) { streamResult += it }
+ assert streamResult == result
+
+ streamResult = ""
+ streamRenderer.render(new StringReader(src), inputSyntax, configurationProvider.defaultOutputSyntax) { streamResult += it }
+ assert streamResult == result
+
+ result
+ }
+
+ private String assertRendererApi(String src, Syntax inputSyntax, Syntax outputSyntax) {
+ def result = renderer.render(src, inputSyntax, outputSyntax)
+ assert result == renderer.render(new StringReader(src), inputSyntax, outputSyntax)
+
+ def streamResult = ""
+ streamRenderer.render(new StringReader(src), inputSyntax, outputSyntax) { streamResult += it }
+ assert streamResult == result
+
+ result
+ }
+
+ private String assertRendererApi(String src) {
+ def result = renderer.render(src)
+
+ assert result == renderer.render(src, configurationProvider.defaultInputSyntax)
+ assert result == renderer.render(src, configurationProvider.defaultInputSyntax, configurationProvider.defaultOutputSyntax)
+ assert result == renderer.render(new StringReader(src))
+ assert result == renderer.render(new StringReader(src), configurationProvider.defaultInputSyntax)
+ assert result == renderer.render(new StringReader(src), configurationProvider.defaultInputSyntax, configurationProvider.defaultOutputSyntax)
+
+ def streamResult = ""
+ streamRenderer.render(new StringReader(src)) { streamResult += it }
+ assert streamResult == result
+
+ streamResult = ""
+ streamRenderer.render(new StringReader(src), configurationProvider.defaultInputSyntax) { streamResult += it }
+ assert streamResult == result
+
+ streamResult = ""
+ streamRenderer.render(new StringReader(src), configurationProvider.defaultInputSyntax, configurationProvider.defaultOutputSyntax) { streamResult += it }
+ assert streamResult == result
+
+ result
+ }
+
+ private String assertRendererApi(String src, List<Transformation> postTransformation) {
+ def result = renderer.render(src, postTransformation)
+ assert result == renderer.render(src, configurationProvider.defaultInputSyntax, postTransformation)
+ assert result == renderer.render(src, configurationProvider.defaultInputSyntax, configurationProvider.defaultOutputSyntax, postTransformation)
+ assert result == renderer.render(new StringReader(src), postTransformation)
+ assert result == renderer.render(new StringReader(src), configurationProvider.defaultInputSyntax, postTransformation)
+ assert result == renderer.render(new StringReader(src), configurationProvider.defaultInputSyntax, configurationProvider.defaultOutputSyntax, postTransformation)
+
+ def streamResult = ""
+ streamRenderer.render(new StringReader(src), postTransformation) { streamResult += it }
+ assert streamResult == result
+
+ streamResult = ""
+ streamRenderer.render(new StringReader(src), configurationProvider.defaultInputSyntax, postTransformation) { streamResult += it }
+ assert streamResult == result
+
+ streamResult = ""
+ streamRenderer.render(new StringReader(src), configurationProvider.defaultInputSyntax, configurationProvider.defaultOutputSyntax, postTransformation) { streamResult += it }
+ assert streamResult == result
+
+ result
+ }
+
+ private String assertRendererApi(String src, List<Transformation> preTransformation, List<Transformation> postTransformation) {
+ def result = renderer.render(src, preTransformation, postTransformation)
+ assert result == renderer.render(src, configurationProvider.defaultInputSyntax, preTransformation, postTransformation)
+ assert result == renderer.render(src, configurationProvider.defaultInputSyntax, configurationProvider.defaultOutputSyntax, preTransformation, postTransformation)
+ assert result == renderer.render(new StringReader(src), preTransformation, postTransformation)
+ assert result == renderer.render(new StringReader(src), configurationProvider.defaultInputSyntax, preTransformation, postTransformation)
+ assert result == renderer.render(new StringReader(src), configurationProvider.defaultInputSyntax, configurationProvider.defaultOutputSyntax, preTransformation, postTransformation)
+
+ def streamResult = ""
+ streamRenderer.render(new StringReader(src), preTransformation, postTransformation) { streamResult += it }
+ assert streamResult == result
+
+ streamResult = ""
+ streamRenderer.render(new StringReader(src), configurationProvider.defaultInputSyntax, preTransformation, postTransformation) { streamResult += it }
+ assert streamResult == result
+
+ streamResult = ""
+ streamRenderer.render(new StringReader(src), configurationProvider.defaultInputSyntax, configurationProvider.defaultOutputSyntax, preTransformation, postTransformation) { streamResult += it }
+ assert streamResult == result
+
+ result
}
}
View
44 test/unit/com/monochromeroad/grails/plugins/xwiki/macro/CodeMacro.groovy
@@ -1,44 +0,0 @@
-package com.monochromeroad.grails.plugins.xwiki.macro
-
-import org.xwiki.rendering.block.Block
-import org.xwiki.rendering.block.VerbatimBlock
-import org.xwiki.rendering.transformation.MacroTransformationContext
-
-/**
- * <p>HTML Code Macro.</p>
- *
- * Usage:
- * <pre>
- * {{code mode="java"}}
- * class TestClass{
- *
- * }
- * {{/code}}
- * </pre>
- */
-public class CodeMacro extends DefaultXWikiMacro<CodeMacroParameter> {
-
- public CodeMacro() {
- super("code", CodeMacroParameter)
- }
-
- @Override
- boolean supportsInlineMode() {
- return true
- }
-
- @Override
- List<Block> execute(CodeMacroParameter parameters, String content, MacroTransformationContext context) {
- Map<String, String> params = new HashMap<String, String>();
- params.put("class", parameters.mode);
- VerbatimBlock result = new VerbatimBlock(content, params, false);
-
- return Collections.<Block>singletonList(result);
- }
-
-}
-
-public class CodeMacroParameter {
- String mode
-}
-
View
38 test/unit/com/monochromeroad/grails/plugins/xwiki/macro/DateMacro.groovy
@@ -1,38 +0,0 @@
-package com.monochromeroad.grails.plugins.xwiki.macro
-
-import org.xwiki.rendering.block.Block
-import org.xwiki.rendering.transformation.MacroTransformationContext
-import org.xwiki.rendering.block.RawBlock
-import org.xwiki.rendering.syntax.Syntax
-
-/**
- * <p>Formatted Date Macro.</p>
- *
- * Usage:
- * <pre>
- * {{date /}}
- * </pre>
- * or
- * <pre>
- * {{date}}yyyy-MM-dd{{/date}}
- * </pre>
- */
-class DateMacro extends DefaultXWikiNoParameterMacro {
-
- public DateMacro() {
- super("date")
- }
-
- @Override
- boolean supportsInlineMode() {
- return true
- }
-
- @Override
- List<Block> execute(String content, MacroTransformationContext context) {
- String formattedDate = new Date().format((content) ?: "yyyy/MM/dd")
-
- return Collections.<Block>singletonList(
- new RawBlock("<span class=\"dateMacro\">${formattedDate}</span>", Syntax.XHTML_1_0))
- }
-}
View
28 test/unit/com/monochromeroad/grails/plugins/xwiki/macro/SampleMacro.groovy
@@ -0,0 +1,28 @@
+package com.monochromeroad.grails.plugins.xwiki.macro
+
+import org.xwiki.rendering.block.Block
+import org.xwiki.rendering.block.WordBlock
+import org.xwiki.rendering.transformation.MacroTransformationContext
+
+/**
+ * @author Masatoshi Hayashi
+ */
+class SampleMacro extends DefaultXWikiMacro<SampleMacroParameter> {
+
+ SampleMacro() {
+ super("sample", SampleMacroParameter)
+ }
+
+ @Override
+ boolean supportsInlineMode() { true }
+
+ @Override
+ List<Block> execute(SampleMacroParameter p, String content, MacroTransformationContext macroTransformationContext) {
+ [new WordBlock("data:${p.data}[inserted by a macro: ${macroName}]")]
+ }
+}
+
+class SampleMacroParameter {
+ String data
+}
+
View
28 test/unit/com/monochromeroad/grails/plugins/xwiki/macro/SampleNoParameterMacro.groovy
@@ -0,0 +1,28 @@
+package com.monochromeroad.grails.plugins.xwiki.macro
+
+import org.xwiki.rendering.block.Block
+import org.xwiki.rendering.block.WordBlock
+import org.xwiki.rendering.macro.MacroExecutionException
+import org.xwiki.rendering.transformation.MacroTransformationContext
+
+/**
+ * Created with IntelliJ IDEA.
+ * User: masatoshi
+ * Date: 13/01/20
+ * Time: 1:47
+ * To change this template use File | Settings | File Templates.
+ */
+class SampleNoParameterMacro extends DefaultXWikiNoParameterMacro {
+
+ SampleNoParameterMacro() {
+ super("sample2")
+ }
+
+ @Override
+ protected List<Block> execute(String content, MacroTransformationContext context) throws MacroExecutionException {
+ [new WordBlock("$content[inserted by a macro: ${macroName}]")]
+ }
+
+ @Override
+ boolean supportsInlineMode() { true }
+}
View
31 test/unit/com/monochromeroad/grails/plugins/xwiki/transformation/SampleTransformation.groovy
@@ -0,0 +1,31 @@
+package com.monochromeroad.grails.plugins.xwiki.transformation
+
+import org.xwiki.rendering.block.Block
+import org.xwiki.rendering.block.WordBlock
+import org.xwiki.rendering.block.match.ClassBlockMatcher
+import org.xwiki.rendering.transformation.AbstractTransformation
+import org.xwiki.rendering.transformation.TransformationContext
+import org.xwiki.rendering.transformation.TransformationException
+
+/**
+ * @author Masatoshi Hayashi
+ */
+class SampleTransformation extends AbstractTransformation {
+
+ String name
+
+ SampleTransformation(String name) {
+ this.name = name
+ }
+
+ @Override
+ void transform(Block block, TransformationContext transformationContext) throws TransformationException {
+ def comment = "It applies the transformation $name! "
+ def wordBlocks = block.<WordBlock>getBlocks(new ClassBlockMatcher(WordBlock.class), Block.Axes.DESCENDANT_OR_SELF)
+ if (wordBlocks.any{ it.word.contains("macro") }) {
+ comment += "[.. may be applied a macro.] "
+ }
+ block.addChild(new WordBlock(comment))
+ }
+
+}

0 comments on commit f3d894d

Please sign in to comment.