Search in sources :

Example 1 with NewLineBlock

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);
        }
    }
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) NewLineBlock(org.xwiki.rendering.block.NewLineBlock) IdBlock(org.xwiki.rendering.block.IdBlock) WordBlock(org.xwiki.rendering.block.WordBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) WikiDocument(org.xwiki.refactoring.WikiDocument) NewLineBlock(org.xwiki.rendering.block.NewLineBlock)

Example 2 with NewLineBlock

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;
}
Also used : ScriptException(javax.script.ScriptException) Block(org.xwiki.rendering.block.Block) NewLineBlock(org.xwiki.rendering.block.NewLineBlock) IOException(java.io.IOException) ParseException(org.xwiki.rendering.parser.ParseException) NewLineBlock(org.xwiki.rendering.block.NewLineBlock)

Aggregations

Block (org.xwiki.rendering.block.Block)2 NewLineBlock (org.xwiki.rendering.block.NewLineBlock)2 IOException (java.io.IOException)1 ScriptException (javax.script.ScriptException)1 WikiDocument (org.xwiki.refactoring.WikiDocument)1 HeaderBlock (org.xwiki.rendering.block.HeaderBlock)1 IdBlock (org.xwiki.rendering.block.IdBlock)1 LinkBlock (org.xwiki.rendering.block.LinkBlock)1 SectionBlock (org.xwiki.rendering.block.SectionBlock)1 SpaceBlock (org.xwiki.rendering.block.SpaceBlock)1 SpecialSymbolBlock (org.xwiki.rendering.block.SpecialSymbolBlock)1 WordBlock (org.xwiki.rendering.block.WordBlock)1 XDOM (org.xwiki.rendering.block.XDOM)1 ParseException (org.xwiki.rendering.parser.ParseException)1