use of org.xwiki.rendering.block.NewLineBlock in project xwiki-platform by xwiki.
the class DefaultDocumentSplitter method split.
/**
* A recursive method for traversing the xdom of the root document and splitting it into sub documents.
*
* @param parentDoc the parent {@link WikiDocument} under which the given list of children reside.
* @param children current list of blocks being traversed.
* @param depth the depth from the root xdom to current list of children.
* @param result space for storing the resulting documents.
* @param splittingCriterion the {@link SplittingCriterion}.
* @param namingCriterion the {@link NamingCriterion}.
*/
private void split(WikiDocument parentDoc, List<Block> children, int depth, List<WikiDocument> result, SplittingCriterion splittingCriterion, NamingCriterion namingCriterion) {
ListIterator<Block> it = children.listIterator();
while (it.hasNext()) {
Block block = it.next();
if (splittingCriterion.shouldSplit(block, depth)) {
// Split a new document and add it to the results list.
XDOM xdom = new XDOM(block.getChildren());
String newDocumentName = namingCriterion.getDocumentName(xdom);
WikiDocument newDoc = new WikiDocument(newDocumentName, xdom, parentDoc);
result.add(newDoc);
// Remove the original block from the parent document.
it.remove();
// Place a link from the parent to child.
it.add(new NewLineBlock());
it.add(createLink(block, newDocumentName));
// Check whether this node should be further traversed.
if (splittingCriterion.shouldIterate(block, depth)) {
split(newDoc, newDoc.getXdom().getChildren(), depth + 1, result, splittingCriterion, namingCriterion);
}
} else if (splittingCriterion.shouldIterate(block, depth)) {
split(parentDoc, block.getChildren(), depth + 1, result, splittingCriterion, namingCriterion);
}
}
}
use of org.xwiki.rendering.block.NewLineBlock in project xwiki-platform by xwiki.
the class PygmentsParser method highlight.
@Override
public List<Block> highlight(String syntaxId, Reader source) throws ParseException {
String code;
try {
code = IOUtils.toString(source);
} catch (IOException e) {
throw new ParseException("Failed to read source", e);
}
if (code.length() == 0) {
return Collections.emptyList();
}
List<Block> blocks;
try {
blocks = highlight(syntaxId, code);
} catch (ScriptException e) {
throw new ParseException("Failed to highlight code", e);
}
// TODO: there is a bug in Pygments that makes it always put a newline at the end of the content
if (code.charAt(code.length() - 1) != '\n' && !blocks.isEmpty() && blocks.get(blocks.size() - 1) instanceof NewLineBlock) {
blocks.remove(blocks.size() - 1);
}
return blocks;
}
Aggregations