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