Search in sources :

Example 1 with HeaderBlock

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

the class HeadingNameNamingCriterion method getDocumentName.

@Override
public String getDocumentName(XDOM newDoc) {
    String documentName = null;
    String prefix = spaceName + ".";
    if (newDoc.getChildren().size() > 0) {
        Block firstChild = newDoc.getChildren().get(0);
        if (firstChild instanceof HeaderBlock) {
            // Clone the header block and remove any unwanted stuff
            Block clonedHeaderBlock = firstChild.clone(new BlockFilter() {

                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;
                }
            });
            XDOM xdom = new XDOM(clonedHeaderBlock.getChildren());
            WikiPrinter printer = new DefaultWikiPrinter();
            this.plainSyntaxRenderer.render(xdom, printer);
            documentName = cleanPageName(printer.toString());
        }
    }
    // Fall back if necessary.
    if (null == documentName || documentName.equals("")) {
        documentName = mainPageNameAndNumberingNamingCriterion.getDocumentName(newDoc);
    } else if (prependBasePageName) {
        documentName = prefix + basePageName + INDEX_SEPERATOR + documentName;
    } else {
        documentName = prefix + documentName;
    }
    // Truncate long document names.
    int maxWidth = (documentNames.contains(documentName) || docBridge.exists(documentName)) ? 252 : 255;
    if (documentName.length() > maxWidth) {
        documentName = documentName.substring(0, maxWidth);
    }
    // Resolve any name clashes.
    String newDocumentName = documentName;
    int localIndex = 0;
    while (documentNames.contains(newDocumentName) || docBridge.exists(newDocumentName)) {
        // Append a trailing local index if the page already exists
        newDocumentName = documentName + INDEX_SEPERATOR + (++localIndex);
    }
    // Add the newly generated document name into the pool of generated document names.
    documentNames.add(newDocumentName);
    return newDocumentName;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) WordBlock(org.xwiki.rendering.block.WordBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) WordBlock(org.xwiki.rendering.block.WordBlock) Block(org.xwiki.rendering.block.Block) SpecialSymbolBlock(org.xwiki.rendering.block.SpecialSymbolBlock) SpaceBlock(org.xwiki.rendering.block.SpaceBlock) ArrayList(java.util.ArrayList) List(java.util.List) BlockFilter(org.xwiki.rendering.block.BlockFilter)

Example 2 with HeaderBlock

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

the class XWikiDocument method getSections.

/**
 * Get the top sections contained in the document.
 * <p>
 * The section are filtered by xwiki.section.depth property on the maximum depth of the sections to return. This
 * method is usually used to get "editable" sections.
 *
 * @return the sections in the current document
 */
public List<DocumentSection> getSections() throws XWikiException {
    if (is10Syntax()) {
        return getSections10();
    } else {
        List<DocumentSection> splitSections = new ArrayList<DocumentSection>();
        List<HeaderBlock> headers = getFilteredHeaders();
        int sectionNumber = 1;
        for (HeaderBlock header : headers) {
            // put -1 as index since there is no way to get the position of the header in the source
            int documentSectionIndex = -1;
            // Need to do the same thing than 1.0 content here
            String documentSectionLevel = StringUtils.repeat("1.", header.getLevel().getAsInt() - 1) + "1";
            DocumentSection docSection = new DocumentSection(sectionNumber++, documentSectionIndex, documentSectionLevel, renderXDOM(new XDOM(header.getChildren()), getSyntax()));
            splitSections.add(docSection);
        }
        return splitSections;
    }
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) ArrayList(java.util.ArrayList) DocumentSection(com.xpn.xwiki.api.DocumentSection) ToString(org.suigeneris.jrcs.util.ToString)

Example 3 with HeaderBlock

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

the class XWikiDocument method getContentOfSection.

/**
 * Return the content of a section.
 *
 * @param sectionNumber the index (+1) of the section in the list of all sections in the document.
 * @return the content of a section or null if the section can't be found.
 * @throws XWikiException error when trying to extract section content
 */
public String getContentOfSection(int sectionNumber) throws XWikiException {
    String content = null;
    if (is10Syntax()) {
        content = getContentOfSection10(sectionNumber);
    } else {
        List<HeaderBlock> headers = getFilteredHeaders();
        if (headers.size() >= sectionNumber) {
            SectionBlock section = headers.get(sectionNumber - 1).getSection();
            content = renderXDOM(new XDOM(Collections.<Block>singletonList(section)), getSyntax());
        }
    }
    return content;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) XDOM(org.xwiki.rendering.block.XDOM) ToString(org.suigeneris.jrcs.util.ToString) SectionBlock(org.xwiki.rendering.block.SectionBlock)

Example 4 with HeaderBlock

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

the class DefaultGadgetRenderer method decorateGadget.

@Override
public List<Block> decorateGadget(Gadget gadget) {
    List<Block> result;
    // a gadget or not.
    if (!this.emptyXDOMChecker.check(gadget.getContent())) {
        // prepare the title of the gadget, in a heading 2
        HeaderBlock titleBlock = new HeaderBlock(gadget.getTitle(), HeaderLevel.LEVEL1);
        titleBlock.setParameter(CLASS, "gadget-title");
        // And then the content wrapped in a group block with class, to style it
        GroupBlock contentGroup = new GroupBlock();
        contentGroup.setParameter(CLASS, "gadget-content");
        contentGroup.addChildren(gadget.getContent());
        // and wrap everything in a container, to give it a class
        GroupBlock gadgetBlock = new GroupBlock();
        String idPrefix = "gadget";
        gadgetBlock.setParameter(CLASS, idPrefix);
        // put an underscore here because it doesn't hurt at this level and it helps scriptaculous on the frontend
        gadgetBlock.setParameter(ID, idPrefix + "_" + gadget.getId());
        gadgetBlock.addChild(titleBlock);
        gadgetBlock.addChild(contentGroup);
        result = Collections.singletonList(gadgetBlock);
    } else {
        result = Collections.emptyList();
    }
    return result;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock) Block(org.xwiki.rendering.block.Block) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) GroupBlock(org.xwiki.rendering.block.GroupBlock)

Example 5 with HeaderBlock

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

the class DocumentTitleDisplayer method extractTitleFromContent.

@Override
protected XDOM extractTitleFromContent(DocumentModelBridge document, DocumentDisplayerParameters parameters) {
    // Note: Ideally we should apply transformations on the document's returned XDOM here since macros could
    // generate headings for example or some other transformations could modify headings. However we don't do this
    // at the moment since it would be too costly to do so. In the future we will even probably remove the feature
    // of generating the title from the content.
    List<HeaderBlock> blocks = document.getXDOM().getBlocks(new ClassBlockMatcher(HeaderBlock.class), Block.Axes.DESCENDANT);
    if (!blocks.isEmpty()) {
        HeaderBlock heading = blocks.get(0);
        // Check the heading depth after which we should return null if no heading was found.
        if (heading.getLevel().getAsInt() <= displayConfiguration.getTitleHeadingDepth()) {
            XDOM headingXDOM = new XDOM(Collections.<Block>singletonList(heading));
            try {
                TransformationContext txContext = new TransformationContext(headingXDOM, document.getSyntax(), parameters.isTransformationContextRestricted());
                txContext.setTargetSyntax(parameters.getTargetSyntax());
                transformationManager.performTransformations(headingXDOM, txContext);
                Block headingBlock = headingXDOM.getChildren().size() > 0 ? headingXDOM.getChildren().get(0) : null;
                if (headingBlock instanceof HeaderBlock) {
                    return new XDOM(headingBlock.getChildren());
                }
            } catch (TransformationException e) {
                getLogger().warn("Failed to extract title from document content.");
            }
        }
    }
    return null;
}
Also used : HeaderBlock(org.xwiki.rendering.block.HeaderBlock) TransformationException(org.xwiki.rendering.transformation.TransformationException) XDOM(org.xwiki.rendering.block.XDOM) ClassBlockMatcher(org.xwiki.rendering.block.match.ClassBlockMatcher) HeaderBlock(org.xwiki.rendering.block.HeaderBlock) Block(org.xwiki.rendering.block.Block) TransformationContext(org.xwiki.rendering.transformation.TransformationContext)

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