Search in sources :

Example 6 with SectionBlock

use of org.xwiki.rendering.block.SectionBlock in project xwiki-platform by xwiki.

the class XWikiDocument method getFilteredHeaders.

/**
 * Filter the headers from a document XDOM based on xwiki.section.depth property from xwiki.cfg file.
 *
 * @return the filtered headers
 */
private List<HeaderBlock> getFilteredHeaders() {
    List<HeaderBlock> filteredHeaders = new ArrayList<HeaderBlock>();
    // Get the maximum header level
    int sectionDepth = 2;
    XWikiContext context = getXWikiContext();
    if (context != null) {
        sectionDepth = (int) context.getWiki().getSectionEditingDepth();
    }
    // Get the headers.
    // 
    // Note that we need to only take into account SectionBlock that are children of other SectionBlocks so that
    // we are in sync with the section editing buttons added in xwiki.js. Being able to section edit any heading is
    // too complex. For example if you have (in XWiki Syntax 2.0):
    // = Heading1 =
    // para1
    // == Heading2 ==
    // para2
    // (((
    // == Heading3 ==
    // para3
    // (((
    // == Heading4 ==
    // para4
    // )))
    // )))
    // == Heading5 ==
    // para5
    // 
    // Then if we were to support editing "Heading4", its content would be:
    // para4
    // )))
    // )))
    // 
    // Which obviously is not correct...
    final XDOM xdom = getXDOM();
    if (!xdom.getChildren().isEmpty()) {
        Block currentBlock = xdom.getChildren().get(0);
        while (currentBlock != null) {
            if (currentBlock instanceof SectionBlock) {
                // The next children block is a HeaderBlock but we check to be on the safe side...
                Block nextChildrenBlock = currentBlock.getChildren().get(0);
                if (nextChildrenBlock instanceof HeaderBlock) {
                    HeaderBlock headerBlock = (HeaderBlock) nextChildrenBlock;
                    if (headerBlock.getLevel().getAsInt() <= sectionDepth) {
                        filteredHeaders.add(headerBlock);
                    }
                }
                currentBlock = nextChildrenBlock;
            } else {
                Block nextSibling = currentBlock.getNextSibling();
                if (nextSibling == null) {
                    currentBlock = currentBlock.getParent();
                    while (currentBlock != null) {
                        if (currentBlock.getNextSibling() != null) {
                            currentBlock = currentBlock.getNextSibling();
                            break;
                        }
                        currentBlock = currentBlock.getParent();
                    }
                } else {
                    currentBlock = nextSibling;
                }
            }
        }
    }
    return filteredHeaders;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) MacroBlock(org.xwiki.rendering.block.MacroBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock)

Example 7 with SectionBlock

use of org.xwiki.rendering.block.SectionBlock in project xwiki-platform by xwiki.

the class XWikiDocument method updateDocumentSection.

/**
 * Update a section content in document.
 *
 * @param sectionNumber the index (starting at 1) of the section in the list of all sections in the document.
 * @param newSectionContent the new section content.
 * @return the new document content.
 * @throws XWikiException error when updating content
 */
public String updateDocumentSection(int sectionNumber, String newSectionContent) throws XWikiException {
    String content;
    if (is10Syntax()) {
        content = updateDocumentSection10(sectionNumber, newSectionContent);
    } else {
        // Get the current section block
        HeaderBlock header = getFilteredHeaders().get(sectionNumber - 1);
        XDOM xdom = (XDOM) header.getRoot();
        // newSectionContent -> Blocks
        List<Block> blocks = parseContent(newSectionContent).getChildren();
        int sectionLevel = header.getLevel().getAsInt();
        for (int level = 1; level < sectionLevel && blocks.size() == 1 && blocks.get(0) instanceof SectionBlock; ++level) {
            blocks = blocks.get(0).getChildren();
        }
        // replace old current SectionBlock with new Blocks
        Block section = header.getSection();
        section.getParent().replaceChild(blocks, section);
        // render back XDOM to document's content syntax
        content = renderXDOM(xdom, getSyntax());
    }
    return content;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) LinkBlock(org.xwiki.rendering.block.LinkBlock) Block(org.xwiki.rendering.block.Block) MacroBlock(org.xwiki.rendering.block.MacroBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) ToString(org.suigeneris.jrcs.util.ToString) SectionBlock(org.xwiki.rendering.block.SectionBlock)

Aggregations

HeaderBlock (org.xwiki.rendering.block.HeaderBlock)7 SectionBlock (org.xwiki.rendering.block.SectionBlock)7 Block (org.xwiki.rendering.block.Block)6 LinkBlock (org.xwiki.rendering.block.LinkBlock)5 XDOM (org.xwiki.rendering.block.XDOM)5 IdBlock (org.xwiki.rendering.block.IdBlock)3 WordBlock (org.xwiki.rendering.block.WordBlock)3 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ToString (org.suigeneris.jrcs.util.ToString)2 WikiDocument (org.xwiki.refactoring.WikiDocument)2 SplittingCriterion (org.xwiki.refactoring.splitter.criterion.SplittingCriterion)2 NamingCriterion (org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion)2 MacroBlock (org.xwiki.rendering.block.MacroBlock)2 ParagraphBlock (org.xwiki.rendering.block.ParagraphBlock)2 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 List (java.util.List)1 BlockFilter (org.xwiki.rendering.block.BlockFilter)1 NewLineBlock (org.xwiki.rendering.block.NewLineBlock)1