use of org.tautua.markdownpapers.ast.Document in project ceylon by eclipse.
the class MarkdownTests method testSplit.
@Test
public void testSplit() {
Document doc = Markdown.markdown("Some stuff\n" + "\n" + "# First `H1`\n" + "\n" + "A sentence\n" + "\n" + "## `H2` under first `H1`" + "\n" + "A sentence\n" + "\n" + "# Second `H1`\n" + "\n" + "A sentence\n" + "\n" + "## `H2` under second `H1`" + "\n" + "A sentence\n" + "\n");
List<Section> sections = Markdown.extractSections(doc);
Assert.assertEquals(3, sections.size());
Section section = sections.get(0);
Assert.assertNull(section.getHeading());
Document sectionBody = section.getDoc();
Assert.assertEquals("<p>Some stuff</p>", html(sectionBody).trim());
section = sections.get(1);
Assert.assertEquals("<h1> First <code>H1</code></h1>", html(section.getHeading()).trim());
sectionBody = section.getDoc();
Assert.assertEquals("" + "<p>A sentence</p>\n" + "\n" + "<h2> <code>H2</code> under first <code>H1</code></h2>\n" + "\n" + "<p>A sentence</p>", html(sectionBody).trim());
section = sections.get(2);
Assert.assertEquals("<h1> Second <code>H1</code></h1>", html(section.getHeading()).trim());
sectionBody = section.getDoc();
Assert.assertEquals("" + "<p>A sentence</p>\n" + "\n" + "<h2> <code>H2</code> under second <code>H1</code></h2>\n" + "\n" + "<p>A sentence</p>", html(sectionBody).trim());
}
use of org.tautua.markdownpapers.ast.Document in project ceylon-compiler by ceylon.
the class MarkdownTests method testSplit.
@Test
public void testSplit() {
Document doc = Markdown.markdown("Some stuff\n" + "\n" + "# First `H1`\n" + "\n" + "A sentence\n" + "\n" + "## `H2` under first `H1`" + "\n" + "A sentence\n" + "\n" + "# Second `H1`\n" + "\n" + "A sentence\n" + "\n" + "## `H2` under second `H1`" + "\n" + "A sentence\n" + "\n");
List<Section> sections = Markdown.extractSections(doc);
Assert.assertEquals(3, sections.size());
Section section = sections.get(0);
Assert.assertNull(section.getHeading());
Document sectionBody = section.getDoc();
Assert.assertEquals("<p>Some stuff</p>", html(sectionBody).trim());
section = sections.get(1);
Assert.assertEquals("<h1> First <code>H1</code></h1>", html(section.getHeading()).trim());
sectionBody = section.getDoc();
Assert.assertEquals("" + "<p>A sentence</p>\n" + "\n" + "<h2> <code>H2</code> under first <code>H1</code></h2>\n" + "\n" + "<p>A sentence</p>", html(sectionBody).trim());
section = sections.get(2);
Assert.assertEquals("<h1> Second <code>H1</code></h1>", html(section.getHeading()).trim());
sectionBody = section.getDoc();
Assert.assertEquals("" + "<p>A sentence</p>\n" + "\n" + "<h2> <code>H2</code> under second <code>H1</code></h2>\n" + "\n" + "<p>A sentence</p>", html(sectionBody).trim());
}
use of org.tautua.markdownpapers.ast.Document in project ceylon by eclipse.
the class Markdown method extractSections.
/**
* Nasty method for extracting doc sections from a markdown document.
* <ol>
* <li>The most prominent heading(s) are identified
* <li>The document is split into a list of sections corresponding to the
* tree following headings of that level. If the document didn't begin
* with a heading of that level, the first Section in the result list will
* have a null heading.
* </ol>
* @param document
* @return
*/
public static List<Section> extractSections(Document document) {
SectionsMarkdownVisitor v = new SectionsMarkdownVisitor();
document.accept(v);
List<Section> result = new ArrayList<>();
for (int levelIndex = 1; levelIndex <= 6; levelIndex++) {
List<Header> sections = v.sections.get(levelIndex);
if (sections != null) {
Node parent;
{
Header header = sections.get(0);
parent = header.jjtGetParent();
Document doc = extractBetween(v, parent, null, header);
result.add(new Section(null, doc));
}
for (int sectionIndex = 0; sectionIndex < sections.size(); sectionIndex++) {
Header header = sections.get(sectionIndex);
Header next = sectionIndex < sections.size() - 1 ? sections.get(sectionIndex + 1) : null;
Document doc = extractBetween(v, parent, header, next);
result.add(new Section(header, doc));
}
break;
}
}
return result;
}
use of org.tautua.markdownpapers.ast.Document in project ceylon by eclipse.
the class PlaintextMarkdownVisitor method visit.
@Override
public void visit(Link node) {
Resource resource = node.getResource();
if (resource == null) {
Node doc = node.jjtGetParent();
while (!(doc instanceof Document)) {
doc = doc.jjtGetParent();
}
resource = ((Document) doc).findResource(node.getReference());
}
if (resource != null) {
out.append(node.getText()).append(" (").append(resource.getLocation()).append(")");
} else {
out.append(node.getText());
}
}
use of org.tautua.markdownpapers.ast.Document in project ceylon by eclipse.
the class MarkdownTests method testAdjustHeadings.
@Test
public void testAdjustHeadings() {
Document doc = Markdown.markdown("Some stuff\n" + "\n" + "# First `H1`\n" + "\n" + "A sentence\n" + "\n" + "## `H2` under first `H1`" + "\n" + "A sentence");
Markdown.adjustHeadings(doc, 1);
Assert.assertEquals("<p>Some stuff</p>\n" + "\n" + "<h2> First <code>H1</code></h2>\n" + "\n" + "<p>A sentence</p>\n" + "\n" + "<h3> <code>H2</code> under first <code>H1</code></h3>\n" + "\n" + "<p>A sentence</p>\n", html(doc));
}
Aggregations