use of com.xpn.xwiki.api.DocumentSection 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 com.xpn.xwiki.api.DocumentSection in project xwiki-platform by xwiki.
the class XWikiDocument method getContentOfSection10.
/**
* 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
* @throws XWikiException error when trying to extract section content
*/
private String getContentOfSection10(int sectionNumber) throws XWikiException {
List<DocumentSection> splitSections = getSections();
int indexEnd = 0;
// get current section
DocumentSection section = splitSections.get(sectionNumber - 1);
int indexStart = section.getSectionIndex();
String sectionLevel = section.getSectionLevel();
// same or a higher level.
for (int i = sectionNumber; i < splitSections.size(); i++) {
DocumentSection nextSection = splitSections.get(i);
String nextLevel = nextSection.getSectionLevel();
if (sectionLevel.equals(nextLevel) || sectionLevel.length() > nextLevel.length()) {
indexEnd = nextSection.getSectionIndex();
break;
}
}
String sectionContent = null;
if (indexStart < 0) {
indexStart = 0;
}
if (indexEnd == 0) {
sectionContent = getContent().substring(indexStart);
} else {
sectionContent = getContent().substring(indexStart, indexEnd);
}
return sectionContent;
}
use of com.xpn.xwiki.api.DocumentSection in project xwiki-platform by xwiki.
the class XWikiDocument method getSections10.
/**
* @return the sections in the current document
*/
private List<DocumentSection> getSections10() {
// Pattern to match the title. Matches only level 1 and level 2 headings.
Pattern headingPattern = Pattern.compile("^[ \\t]*+(1(\\.1){0,1}+)[ \\t]++(.++)$", Pattern.MULTILINE);
Matcher matcher = headingPattern.matcher(getContent());
List<DocumentSection> splitSections = new ArrayList<DocumentSection>();
int sectionNumber = 0;
// find title to split
while (matcher.find()) {
++sectionNumber;
String sectionLevel = matcher.group(1);
String sectionTitle = matcher.group(3);
int sectionIndex = matcher.start();
// Initialize a documentSection object.
DocumentSection docSection = new DocumentSection(sectionNumber, sectionIndex, sectionLevel, sectionTitle);
// Add the document section to list.
splitSections.add(docSection);
}
return splitSections;
}
use of com.xpn.xwiki.api.DocumentSection in project xwiki-platform by xwiki.
the class XWikiDocumentTest method testGetDocumentSection.
public void testGetDocumentSection() throws XWikiException {
this.document.setContent("content not in section\n" + "= header 1=\nheader 1 content\n" + "== header 2==\nheader 2 content");
this.document.setSyntax(Syntax.XWIKI_2_0);
DocumentSection header1 = this.document.getDocumentSection(1);
DocumentSection header2 = this.document.getDocumentSection(2);
assertEquals("header 1", header1.getSectionTitle());
assertEquals(-1, header1.getSectionIndex());
assertEquals(1, header1.getSectionNumber());
assertEquals("1", header1.getSectionLevel());
assertEquals("header 2", header2.getSectionTitle());
assertEquals(-1, header2.getSectionIndex());
assertEquals(2, header2.getSectionNumber());
assertEquals("1.1", header2.getSectionLevel());
}
use of com.xpn.xwiki.api.DocumentSection in project xwiki-platform by xwiki.
the class XWikiDocumentTest method testGetDocumentSectionWhenSectionInGroups.
/**
* Verify that if we have sections nested in groups, they are not taken into account when computing document
* sections by number. See <a href="https://jira.xwiki.org/browse/XWIKI-6195">XWIKI-6195</a>.
*
* @since 5.0M1
*/
public void testGetDocumentSectionWhenSectionInGroups() throws XWikiException {
this.document.setContent("= Heading1 =\n" + "para1\n" + "== Heading2 ==\n" + "para2\n" + "(((\n" + "== Heading3 ==\n" + "para3\n" + "(((\n" + "== Heading4 ==\n" + "para4\n" + ")))\n" + ")))\n" + "== Heading5 ==\n" + "para5\n");
this.document.setSyntax(Syntax.XWIKI_2_0);
DocumentSection section = this.document.getDocumentSection(3);
assertEquals("Heading5", section.getSectionTitle());
}
Aggregations