Search in sources :

Example 6 with HeaderBlock

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

the class DefaultDocumentSplitterTest method updateAnchors.

@Test
public void updateAnchors() throws Exception {
    SplittingCriterion splittingCriterion = mock(SplittingCriterion.class);
    when(splittingCriterion.shouldSplit(any(Block.class), anyInt())).thenReturn(false, true, true);
    when(splittingCriterion.shouldIterate(any(SectionBlock.class), anyInt())).thenReturn(false, false, false);
    NamingCriterion namingCriterion = mock(NamingCriterion.class);
    when(namingCriterion.getDocumentName(any(XDOM.class))).thenReturn("Child1", "Child2");
    // [[link>>||anchor="chapter1"]]
    // = {{id name="chapter1"}}Chapter 1 =
    // = Chapter 2 ==
    // [[link>>path:#chapter1]]
    ResourceReference reference = new ResourceReference("", ResourceType.DOCUMENT);
    reference.setParameter("anchor", "chapter1");
    LinkBlock link = new LinkBlock(Arrays.<Block>asList(new WordBlock("link")), reference, false);
    ParagraphBlock paragraph = new ParagraphBlock(Arrays.<Block>asList(link));
    HeaderBlock header = new HeaderBlock(Arrays.<Block>asList(new IdBlock("chapter1"), new WordBlock("Chapter 1")), HeaderLevel.LEVEL1);
    SectionBlock section1 = new SectionBlock(Arrays.<Block>asList(header));
    header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Chapter 2")), HeaderLevel.LEVEL1);
    reference = new ResourceReference("#chapter1", ResourceType.PATH);
    link = new LinkBlock(Arrays.<Block>asList(new WordBlock("link")), reference, false);
    SectionBlock section2 = new SectionBlock(Arrays.<Block>asList(header, new ParagraphBlock(Arrays.<Block>asList(link))));
    XDOM xdom = new XDOM(Arrays.<Block>asList(paragraph, section1, section2));
    WikiDocument document = new WikiDocument("Space.Page", xdom, null);
    List<WikiDocument> result = mocker.getComponentUnderTest().split(document, splittingCriterion, namingCriterion);
    ClassBlockMatcher linkMatcher = new ClassBlockMatcher(LinkBlock.class);
    ResourceReference updatedReference = document.getXdom().<LinkBlock>getFirstBlock(linkMatcher, Axes.DESCENDANT).getReference();
    assertEquals("chapter1", updatedReference.getParameter("anchor"));
    assertEquals(result.get(1).getFullName(), updatedReference.getReference());
    updatedReference = result.get(2).getXdom().<LinkBlock>getFirstBlock(linkMatcher, Axes.DESCENDANT).getReference();
    assertEquals(ResourceType.DOCUMENT, updatedReference.getType());
    assertEquals("chapter1", updatedReference.getParameter("anchor"));
    assertEquals(result.get(1).getFullName(), updatedReference.getReference());
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) WordBlock(org.xwiki.rendering.block.WordBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) WikiDocument(org.xwiki.refactoring.WikiDocument) IdBlock(org.xwiki.rendering.block.IdBlock) NamingCriterion(org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion) LinkBlock(org.xwiki.rendering.block.LinkBlock) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) LinkBlock(org.xwiki.rendering.block.LinkBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) IdBlock(org.xwiki.rendering.block.IdBlock) WordBlock(org.xwiki.rendering.block.WordBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) ResourceReference(org.xwiki.rendering.listener.reference.ResourceReference) SectionBlock(org.xwiki.rendering.block.SectionBlock) SplittingCriterion(org.xwiki.refactoring.splitter.criterion.SplittingCriterion) Test(org.junit.Test)

Example 7 with HeaderBlock

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

the class DefaultDocumentSplitterTest method split.

@Test
public void split() throws Exception {
    SplittingCriterion splittingCriterion = mock(SplittingCriterion.class);
    when(splittingCriterion.shouldSplit(any(Block.class), anyInt())).thenReturn(true, false, false, true);
    when(splittingCriterion.shouldIterate(any(SectionBlock.class), anyInt())).thenReturn(true, false, false, false);
    NamingCriterion namingCriterion = mock(NamingCriterion.class);
    when(namingCriterion.getDocumentName(any(XDOM.class))).thenReturn("Child1", "Child2");
    // = Chapter 1 =
    // Once upon a time..
    // == Section 1.1 ==
    // In a kingdom far away..
    HeaderBlock header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Section 1.1")), HeaderLevel.LEVEL2);
    ParagraphBlock paragraph = new ParagraphBlock(Arrays.<Block>asList(new WordBlock("In a kingdom far away..")));
    SectionBlock subSection = new SectionBlock(Arrays.<Block>asList(header, paragraph));
    header = new HeaderBlock(Arrays.<Block>asList(new WordBlock("Chapter 1")), HeaderLevel.LEVEL1);
    paragraph = new ParagraphBlock(Arrays.<Block>asList(new WordBlock("Once upon a time..")));
    SectionBlock section = new SectionBlock(Arrays.<Block>asList(header, paragraph, subSection));
    XDOM xdom = new XDOM(Arrays.<Block>asList(section));
    WikiDocument document = new WikiDocument("Space.Page", xdom, null);
    List<WikiDocument> result = mocker.getComponentUnderTest().split(document, splittingCriterion, namingCriterion);
    assertEquals(3, result.size());
    assertSame(document, result.get(0));
    assertEquals("Child1", result.get(1).getFullName());
    assertSame(document, result.get(1).getParent());
    assertEquals("Child2", result.get(2).getFullName());
    assertSame(result.get(1), result.get(2).getParent());
    ClassBlockMatcher headerMatcher = new ClassBlockMatcher(HeaderBlock.class);
    assertTrue(document.getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).isEmpty());
    assertEquals(1, result.get(1).getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).size());
    assertEquals(1, result.get(2).getXdom().<LinkBlock>getBlocks(headerMatcher, Axes.DESCENDANT).size());
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) NamingCriterion(org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) LinkBlock(org.xwiki.rendering.block.LinkBlock) WordBlock(org.xwiki.rendering.block.WordBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) IdBlock(org.xwiki.rendering.block.IdBlock) WordBlock(org.xwiki.rendering.block.WordBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) ParagraphBlock(org.xwiki.rendering.block.ParagraphBlock) WikiDocument(org.xwiki.refactoring.WikiDocument) SectionBlock(org.xwiki.rendering.block.SectionBlock) SplittingCriterion(org.xwiki.refactoring.splitter.criterion.SplittingCriterion) Test(org.junit.Test)

Example 8 with HeaderBlock

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

the class DefaultDocumentSplitter method createLink.

/**
 * Creates a {@link LinkBlock} suitable to be placed in the parent document.
 *
 * @param block the {@link Block} that has just been split into a separate document.
 * @param target name of the target wiki document.
 * @return a {@link LinkBlock} representing the link from the parent document to new document.
 */
private LinkBlock createLink(Block block, String target) {
    Block firstBlock = block.getChildren().get(0);
    if (firstBlock instanceof HeaderBlock) {
        DocumentResourceReference reference = new DocumentResourceReference(target);
        // Clone the header block and remove any unwanted stuff
        Block clonedHeaderBlock = firstBlock.clone(new BlockFilter() {

            @Override
            public List<Block> filter(Block block) {
                List<Block> blocks = new ArrayList<Block>();
                if (block instanceof WordBlock || block instanceof SpaceBlock || block instanceof SpecialSymbolBlock) {
                    blocks.add(block);
                }
                return blocks;
            }
        });
        return new LinkBlock(clonedHeaderBlock.getChildren(), reference, false);
    } else if (firstBlock instanceof SectionBlock) {
        return createLink(firstBlock, target);
    } else {
        throw new IllegalArgumentException("A SectionBlock should either begin with a HeaderBlock or another SectionBlock.");
    }
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) WordBlock(org.xwiki.rendering.block.WordBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) LinkBlock(org.xwiki.rendering.block.LinkBlock) 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) ArrayList(java.util.ArrayList) List(java.util.List) DocumentResourceReference(org.xwiki.rendering.listener.reference.DocumentResourceReference) SectionBlock(org.xwiki.rendering.block.SectionBlock) BlockFilter(org.xwiki.rendering.block.BlockFilter)

Example 9 with HeaderBlock

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

the class HeadingLevelSplittingCriterion method shouldSplit.

@Override
public boolean shouldSplit(Block block, int depth) {
    boolean shouldSplit = false;
    if (block instanceof SectionBlock) {
        SectionBlock section = (SectionBlock) block;
        Block firstChild = section.getChildren().get(0);
        shouldSplit = (firstChild instanceof HeaderBlock) && (Arrays.binarySearch(headingLevels, depth) >= 0);
    }
    return shouldSplit;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock) SectionBlock(org.xwiki.rendering.block.SectionBlock)

Example 10 with HeaderBlock

use of org.xwiki.rendering.block.HeaderBlock 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)

Aggregations

HeaderBlock (org.xwiki.rendering.block.HeaderBlock)11 Block (org.xwiki.rendering.block.Block)9 XDOM (org.xwiki.rendering.block.XDOM)8 SectionBlock (org.xwiki.rendering.block.SectionBlock)7 LinkBlock (org.xwiki.rendering.block.LinkBlock)5 ArrayList (java.util.ArrayList)4 WordBlock (org.xwiki.rendering.block.WordBlock)4 ToString (org.suigeneris.jrcs.util.ToString)3 IdBlock (org.xwiki.rendering.block.IdBlock)3 ClassBlockMatcher (org.xwiki.rendering.block.match.ClassBlockMatcher)3 List (java.util.List)2 Test (org.junit.Test)2 WikiDocument (org.xwiki.refactoring.WikiDocument)2 SplittingCriterion (org.xwiki.refactoring.splitter.criterion.SplittingCriterion)2 NamingCriterion (org.xwiki.refactoring.splitter.criterion.naming.NamingCriterion)2 BlockFilter (org.xwiki.rendering.block.BlockFilter)2 MacroBlock (org.xwiki.rendering.block.MacroBlock)2 ParagraphBlock (org.xwiki.rendering.block.ParagraphBlock)2 SpaceBlock (org.xwiki.rendering.block.SpaceBlock)2 SpecialSymbolBlock (org.xwiki.rendering.block.SpecialSymbolBlock)2