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;
}
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;
}
}
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;
}
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;
}
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;
}
Aggregations