Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
XWiki
xwiki-rendering
Commits
2bf36638
Commit
2bf36638
authored
Dec 08, 2021
by
Thomas Mortagne
Browse files
XRENDERING-624: Provide a tool to convert a parsed wiki content to inline
parent
157bd2b5
Changes
2
Hide whitespace changes
Inline
Side-by-side
xwiki-rendering-api/src/main/java/org/xwiki/rendering/util/ParserUtils.java
View file @
2bf36638
...
...
@@ -19,10 +19,18 @@
*/
package
org.xwiki.rendering.util
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
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
;
/**
* Methods for helping in parsing.
...
...
@@ -52,4 +60,69 @@ public void removeTopLevelParagraph(List<Block> blocks)
}
}
}
/**
* Make its best to convert a passed block to its inline version. Sometime it's simply impossible to convert an
* inline block, in which case it will just be returned as is.
*
* @param rootBlock the block to convert
* @return the inline version of the passed block
* @since 14.0RC1
*/
@Unstable
public
Block
convertToInline
(
Block
rootBlock
)
{
List
<
Block
>
blocks
;
if
(
rootBlock
instanceof
XDOM
||
rootBlock
instanceof
CompositeBlock
)
{
blocks
=
rootBlock
.
getChildren
();
// Make sure to the list is modifiable List
if
(!(
blocks
instanceof
ArrayList
))
{
blocks
=
new
ArrayList
<>(
blocks
);
}
}
else
{
blocks
=
Arrays
.
asList
(
rootBlock
);
}
convertToInline
(
blocks
);
// Preserve source metadata if any
if
(
rootBlock
instanceof
XDOM
)
{
MetaData
metadata
=
((
XDOM
)
rootBlock
).
getMetaData
();
if
(!
metadata
.
getMetaData
().
isEmpty
())
{
return
new
MetaDataBlock
(
blocks
,
metadata
);
}
}
return
blocks
.
size
()
==
1
?
blocks
.
get
(
0
)
:
new
CompositeBlock
(
blocks
);
}
/**
* Make its best to convert a passed blocks to their inline version. Sometime it's simply impossible to convert an
* inline block, in which case it will just be returned as is.
*
* @param blocks the blocks to convert
* @since 14.0RC1
*/
@Unstable
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
));
}
}
}
}
}
}
xwiki-rendering-transformations/xwiki-rendering-transformation-macro/src/main/java/org/xwiki/rendering/internal/macro/DefaultMacroContentParser.java
View file @
2bf36638
...
...
@@ -20,7 +20,6 @@
package
org.xwiki.rendering.internal.macro
;
import
java.io.StringReader
;
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
...
...
@@ -33,6 +32,7 @@
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
;
...
...
@@ -151,22 +151,14 @@ private void performTransformation(MutableRenderingContext renderingContext, Tra
*/
private
XDOM
convertToInline
(
XDOM
xdom
)
{
List
<
Block
>
blocks
=
new
ArrayList
<>(
xdom
.
getChildren
());
// TODO: use inline parser instead
if
(!
blocks
.
isEmpty
())
{
this
.
parserUtils
.
removeTopLevelParagraph
(
blocks
);
// Make sure included macro is inline when script macro itself is 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
));
}
}
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
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment