Search in sources :

Example 6 with Document

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());
}
Also used : Document(org.tautua.markdownpapers.ast.Document) Section(org.eclipse.ceylon.common.tools.help.Markdown.Section) Test(org.junit.Test)

Example 7 with Document

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());
}
Also used : Document(org.tautua.markdownpapers.ast.Document) Section(com.redhat.ceylon.common.tools.help.Markdown.Section) Test(org.junit.Test)

Example 8 with Document

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;
}
Also used : Header(org.tautua.markdownpapers.ast.Header) Node(org.tautua.markdownpapers.ast.Node) SimpleNode(org.tautua.markdownpapers.ast.SimpleNode) ArrayList(java.util.ArrayList) Document(org.tautua.markdownpapers.ast.Document)

Example 9 with Document

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());
    }
}
Also used : Node(org.tautua.markdownpapers.ast.Node) Resource(org.tautua.markdownpapers.ast.Resource) Document(org.tautua.markdownpapers.ast.Document)

Example 10 with Document

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));
}
Also used : Document(org.tautua.markdownpapers.ast.Document) Test(org.junit.Test)

Aggregations

Document (org.tautua.markdownpapers.ast.Document)11 Test (org.junit.Test)4 Node (org.tautua.markdownpapers.ast.Node)4 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 Section (org.eclipse.ceylon.common.tools.help.Markdown.Section)2 SimpleNode (org.tautua.markdownpapers.ast.SimpleNode)2 Parser (org.tautua.markdownpapers.parser.Parser)2 WordWrap (com.redhat.ceylon.common.tool.WordWrap)1 Section (com.redhat.ceylon.common.tools.help.Markdown.Section)1 PlaintextMarkdownVisitor (com.redhat.ceylon.common.tools.help.PlaintextMarkdownVisitor)1 WordWrap (org.eclipse.ceylon.common.tool.WordWrap)1 PlaintextMarkdownVisitor (org.eclipse.ceylon.common.tools.help.PlaintextMarkdownVisitor)1 DescribedSection (org.eclipse.ceylon.common.tools.help.model.DescribedSection)1 OptionsSection (org.eclipse.ceylon.common.tools.help.model.OptionsSection)1 SummarySection (org.eclipse.ceylon.common.tools.help.model.SummarySection)1 SynopsesSection (org.eclipse.ceylon.common.tools.help.model.SynopsesSection)1 Header (org.tautua.markdownpapers.ast.Header)1