Commit e73c33b9 authored by Thomas Mortagne's avatar Thomas Mortagne
Browse files

XRENDERING-624: Provide a tool to convert a parsed wiki content to inline

* don't improve the convertInline implementation just yet, will come later (and impact a few existing tests)
* fix a few test after the changes made to the default macro error messages
parent 2bf36638
......@@ -184,16 +184,18 @@ public void addChildren(List<? extends Block> blocksToAdd)
@Override
public void setChildren(List<? extends Block> children)
{
if (children.isEmpty()) {
if (this.childrenBlocks != null) {
this.childrenBlocks.clear();
}
} else {
if (this.childrenBlocks != null) {
this.childrenBlocks.clear();
}
if (children != this.childrenBlocks) {
if (children.isEmpty()) {
if (this.childrenBlocks != null) {
this.childrenBlocks.clear();
}
} else {
if (this.childrenBlocks != null) {
this.childrenBlocks.clear();
}
addChildren(children);
addChildren(children);
}
}
}
......
......@@ -26,10 +26,8 @@
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.block.CompositeBlock;
import org.xwiki.rendering.block.MacroBlock;
import org.xwiki.rendering.block.MetaDataBlock;
import org.xwiki.rendering.block.ParagraphBlock;
import org.xwiki.rendering.block.XDOM;
import org.xwiki.rendering.listener.MetaData;
import org.xwiki.stability.Unstable;
/**
......@@ -66,11 +64,12 @@ public void removeTopLevelParagraph(List<Block> blocks)
* inline block, in which case it will just be returned as is.
*
* @param rootBlock the block to convert
* @param preserveXDOM true of the XDOM should be returned
* @return the inline version of the passed block
* @since 14.0RC1
*/
@Unstable
public Block convertToInline(Block rootBlock)
public Block convertToInline(Block rootBlock, boolean preserveXDOM)
{
List<Block> blocks;
if (rootBlock instanceof XDOM || rootBlock instanceof CompositeBlock) {
......@@ -87,12 +86,10 @@ public Block convertToInline(Block rootBlock)
convertToInline(blocks);
// Preserve source metadata if any
if (rootBlock instanceof XDOM) {
MetaData metadata = ((XDOM) rootBlock).getMetaData();
if (preserveXDOM && rootBlock instanceof XDOM) {
rootBlock.setChildren(blocks);
if (!metadata.getMetaData().isEmpty()) {
return new MetaDataBlock(blocks, metadata);
}
return rootBlock;
}
return blocks.size() == 1 ? blocks.get(0) : new CompositeBlock(blocks);
......@@ -106,21 +103,19 @@ public Block convertToInline(Block rootBlock)
* @since 14.0RC1
*/
@Unstable
// TODO: improve the implementation to really convert to inline everything that can be converted
public void convertToInline(List<Block> blocks)
{
if (!blocks.isEmpty()) {
// Clean top level paragraph
removeTopLevelParagraph(blocks);
// Make sure all macros are inline
for (int i = 0; i < blocks.size(); ++i) {
Block block = blocks.get(i);
if (block instanceof MacroBlock) {
MacroBlock macro = (MacroBlock) block;
if (!macro.isInline()) {
blocks.set(i, new MacroBlock(macro.getId(), macro.getParameters(), macro.getContent(), true));
}
// Make sure included macro is inline when script macro itself is inline
Block block = blocks.get(0);
if (block instanceof MacroBlock) {
MacroBlock macro = (MacroBlock) block;
if (!macro.isInline()) {
blocks.set(0, new MacroBlock(macro.getId(), macro.getParameters(), macro.getContent(), true));
}
}
}
......
......@@ -29,7 +29,7 @@
* @version $Id$
* @since 3.0RC1
*/
@RenderingTests.Scope(value = "simple")
@RenderingTests.Scope(value = "simple"/*, pattern = "macro35.test"*/)
@AllComponents
public class SimpleIntegrationTests implements RenderingTests
{
......
......@@ -14,10 +14,10 @@ onWord [Unknown]
onSpace
beginMacroMarkerInline [unknown] []
beginFormat [NONE] [[class]=[xwikirenderingerror]]
onWord [Unknown macro: unknown.]
onWord [Unknown macro: unknown. Click on this message for details.]
endFormat [NONE] [[class]=[xwikirenderingerror]]
beginFormat [NONE] [[class]=[xwikirenderingerrordescription hidden]]
onVerbatim [The "unknown" macro is not in the list of registered macros. Verify the spelling or contact your administrator.] [true]
onVerbatim [The [unknown] macro is not in the list of registered macros. Verify the spelling or contact your administrator.] [true]
endFormat [NONE] [[class]=[xwikirenderingerrordescription hidden]]
endMacroMarkerInline [unknown] []
onSpace
......@@ -27,11 +27,11 @@ endDocument
.#-----------------------------------------------------
.expect|xhtml/1.0
.#-----------------------------------------------------
<p>Unknown <span class="xwikirenderingerror">Unknown macro: unknown.</span><span class="xwikirenderingerrordescription hidden"><tt class="wikimodel-verbatim">The "unknown" macro is not in the list of registered macros. Verify the spelling or contact your administrator.</tt></span> macro</p>
<p>Unknown <span class="xwikirenderingerror">Unknown macro: unknown. Click on this message for details.</span><span class="xwikirenderingerrordescription hidden"><tt class="wikimodel-verbatim">The [unknown] macro is not in the list of registered macros. Verify the spelling or contact your administrator.</tt></span> macro</p>
.#-----------------------------------------------------
.expect|annotatedxhtml/1.0
.#-----------------------------------------------------
<p>Unknown <!--startmacro:unknown|-|--><span class="xwikirenderingerror">Unknown macro: unknown.</span><span class="xwikirenderingerrordescription hidden"><tt class="wikimodel-verbatim">The "unknown" macro is not in the list of registered macros. Verify the spelling or contact your administrator.</tt></span><!--stopmacro--> macro</p>
<p>Unknown <!--startmacro:unknown|-|--><span class="xwikirenderingerror">Unknown macro: unknown. Click on this message for details.</span><span class="xwikirenderingerrordescription hidden"><tt class="wikimodel-verbatim">The [unknown] macro is not in the list of registered macros. Verify the spelling or contact your administrator.</tt></span><!--stopmacro--> macro</p>
.#-----------------------------------------------------
.expect|xwiki/2.0
.#-----------------------------------------------------
......
......@@ -15,7 +15,7 @@ onWord [inline]
onSpace
beginMacroMarkerInline [testsimplemacro] []
beginFormat [NONE] [[class]=[xwikirenderingerror]]
onWord [The [testsimplemacro] macro is a standalone macro and it cannot be used inline]
onWord [The [testsimplemacro] macro is a standalone macro and it cannot be used inline. Click on this message for details.]
endFormat [NONE] [[class]=[xwikirenderingerror]]
beginFormat [NONE] [[class]=[xwikirenderingerrordescription hidden]]
onVerbatim [This macro generates standalone content. As a consequence you need to make sure to use a syntax that separates your macro from the content before and after it so that it's on a line by itself. For example in XWiki Syntax 2.0+ this means having 2 newline characters (a.k.a line breaks) separating your macro from the content before and after it.] [true]
......@@ -28,11 +28,11 @@ endDocument
.#-----------------------------------------------------
.expect|xhtml/1.0
.#-----------------------------------------------------
<p>Not inline <span class="xwikirenderingerror">The [testsimplemacro] macro is a standalone macro and it cannot be used inline</span><span class="xwikirenderingerrordescription hidden"><tt class="wikimodel-verbatim">This macro generates standalone content. As a consequence you need to make sure to use a syntax that separates your macro from the content before and after it so that it's on a line by itself. For example in XWiki Syntax 2.0+ this means having 2 newline characters (a.k.a line breaks) separating your macro from the content before and after it.</tt></span> macro</p>
<p>Not inline <span class="xwikirenderingerror">The [testsimplemacro] macro is a standalone macro and it cannot be used inline. Click on this message for details.</span><span class="xwikirenderingerrordescription hidden"><tt class="wikimodel-verbatim">This macro generates standalone content. As a consequence you need to make sure to use a syntax that separates your macro from the content before and after it so that it's on a line by itself. For example in XWiki Syntax 2.0+ this means having 2 newline characters (a.k.a line breaks) separating your macro from the content before and after it.</tt></span> macro</p>
.#-----------------------------------------------------
.expect|annotatedxhtml/1.0
.#-----------------------------------------------------
<p>Not inline <!--startmacro:testsimplemacro|-|--><span class="xwikirenderingerror">The [testsimplemacro] macro is a standalone macro and it cannot be used inline</span><span class="xwikirenderingerrordescription hidden"><tt class="wikimodel-verbatim">This macro generates standalone content. As a consequence you need to make sure to use a syntax that separates your macro from the content before and after it so that it's on a line by itself. For example in XWiki Syntax 2.0+ this means having 2 newline characters (a.k.a line breaks) separating your macro from the content before and after it.</tt></span><!--stopmacro--> macro</p>
<p>Not inline <!--startmacro:testsimplemacro|-|--><span class="xwikirenderingerror">The [testsimplemacro] macro is a standalone macro and it cannot be used inline. Click on this message for details.</span><span class="xwikirenderingerrordescription hidden"><tt class="wikimodel-verbatim">This macro generates standalone content. As a consequence you need to make sure to use a syntax that separates your macro from the content before and after it so that it's on a line by itself. For example in XWiki Syntax 2.0+ this means having 2 newline characters (a.k.a line breaks) separating your macro from the content before and after it.</tt></span><!--stopmacro--> macro</p>
.#-----------------------------------------------------
.expect|xwiki/2.0
.#-----------------------------------------------------
......
......@@ -250,7 +250,7 @@ private void assertExpectedResult(String expected, String result)
Pattern pattern = Pattern.compile(builder.toString(), Pattern.DOTALL);
Matcher matcher = pattern.matcher(result);
if (!matcher.matches()) {
throwAssertionException("", expected, result);
throwAssertionException("Result does not match the expected value", expected, result);
}
}
......
......@@ -22,11 +22,12 @@
import java.util.List;
import java.util.Map;
import org.opentest4j.AssertionFailedError;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.rendering.test.integration.AbstractRenderingTest;
import org.xwiki.rendering.test.integration.TestDataParser;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* A generic JUnit Test used by {@link RenderingTestExecutor} to parse some passed content and verify it matches some
* passed expectation. The format of the input/expectation is specified in {@link TestDataParser}.
......@@ -46,6 +47,6 @@ public RenderingTest(String input, String expected, String parserId, String targ
@Override
protected void throwAssertionException(String message, String expected, String result)
{
throw new AssertionFailedError(message, expected, result);
assertEquals(expected, result, message);
}
}
......@@ -21,7 +21,6 @@
import java.io.StringReader;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Singleton;
......@@ -32,7 +31,6 @@
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.rendering.block.Block;
import org.xwiki.rendering.block.Block.Axes;
import org.xwiki.rendering.block.CompositeBlock;
import org.xwiki.rendering.block.MacroBlock;
import org.xwiki.rendering.block.MetaDataBlock;
import org.xwiki.rendering.block.XDOM;
......@@ -152,16 +150,7 @@ private void performTransformation(MutableRenderingContext renderingContext, Tra
private XDOM convertToInline(XDOM xdom)
{
if (!xdom.getChildren().isEmpty()) {
Block inlineBlock = this.parserUtils.convertToInline(xdom);
List<Block> blocks;
if (inlineBlock instanceof CompositeBlock) {
blocks = inlineBlock.getChildren();
} else {
blocks = Collections.singletonList(inlineBlock);
}
xdom.setChildren(blocks);
return (XDOM) this.parserUtils.convertToInline(xdom, true);
}
return xdom;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment