Search in sources :

Example 1 with Document

use of org.tautua.markdownpapers.ast.Document in project ceylon-compiler by ceylon.

the class PlaintextTests method renderAndCompare.

public void renderAndCompare(String mdFile, String txtFile) throws Exception {
    StringWriter sw = new StringWriter();
    try (InputStreamReader stream = new InputStreamReader(PlaintextTests.class.getResourceAsStream(mdFile))) {
        Parser parser = new Parser(stream);
        Document document = parser.parse();
        PlaintextMarkdownVisitor emitter = new PlaintextMarkdownVisitor(new WordWrap(sw));
        document.accept(emitter);
    }
    StringBuilder sb = new StringBuilder();
    try (BufferedReader r = new BufferedReader(new InputStreamReader(PlaintextTests.class.getResourceAsStream(txtFile)))) {
        String line = r.readLine();
        while (line != null) {
            sb.append(line).append("\n");
            line = r.readLine();
        }
    }
    Assert.assertEquals(sb.toString(), sw.toString().replace("\r\n", "\n"));
}
Also used : StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) WordWrap(com.redhat.ceylon.common.tool.WordWrap) BufferedReader(java.io.BufferedReader) PlaintextMarkdownVisitor(com.redhat.ceylon.common.tools.help.PlaintextMarkdownVisitor) Document(org.tautua.markdownpapers.ast.Document) Parser(org.tautua.markdownpapers.parser.Parser)

Example 2 with Document

use of org.tautua.markdownpapers.ast.Document in project ceylon-compiler by ceylon.

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)

Example 3 with Document

use of org.tautua.markdownpapers.ast.Document in project ceylon by eclipse.

the class DocBuilder method buildAdditionalSections.

private List<DescribedSection> buildAdditionalSections(ToolModel<?> model) {
    List<DescribedSection> additionalSections = new ArrayList<DescribedSection>();
    String sections = getSections(model);
    if (sections != null && !sections.isEmpty()) {
        Document doc = Markdown.markdown(sections);
        List<Section> markdownSections = Markdown.extractSections(doc);
        for (Markdown.Section sect : markdownSections) {
            DescribedSection ds = new DescribedSection();
            ds.setRole(Role.ADDITIONAL);
            Document sectionDoc = sect.getDoc();
            if (sect.getHeading() == null) {
                // TODO Warn that there were no section headings
                continue;
            } else {
                // Adjust the heading levels, so that the most prominent
                // heading is H2
                Markdown.adjustHeadings(sectionDoc, 2 - sect.getHeading().getLevel());
            }
            ds.setTitle(sect.getHeading());
            ds.setDescription(sectionDoc);
            additionalSections.add(ds);
        }
    }
    return additionalSections;
}
Also used : Section(org.eclipse.ceylon.common.tools.help.Markdown.Section) ArrayList(java.util.ArrayList) DescribedSection(org.eclipse.ceylon.common.tools.help.model.DescribedSection) Document(org.tautua.markdownpapers.ast.Document) OptionsSection(org.eclipse.ceylon.common.tools.help.model.OptionsSection) SynopsesSection(org.eclipse.ceylon.common.tools.help.model.SynopsesSection) Section(org.eclipse.ceylon.common.tools.help.Markdown.Section) DescribedSection(org.eclipse.ceylon.common.tools.help.model.DescribedSection) SummarySection(org.eclipse.ceylon.common.tools.help.model.SummarySection)

Example 4 with Document

use of org.tautua.markdownpapers.ast.Document in project ceylon by eclipse.

the class Markdown method extractBetween.

private static Document extractBetween(SectionsMarkdownVisitor v, Node parent, Header header1, Header header2) {
    /*Node parent1 = header1 != null ? header1.jjtGetParent() : null;
        Node parent2 = header2 != null ? header2.jjtGetParent() : null;
        /*if (parent1 != null
                && parent2 != null
                && parent1 != parent2) {
            throw new RuntimeException();
        }* /
        Node parent = parent1 != null ? parent1 : parent2;*/
    Document doc = new Document(Parser.JJTDOCUMENT);
    final int numChildren = parent.jjtGetNumChildren();
    final int start = header1 != null ? Markdown.getIndexInParent(header1) + 1 : 0;
    final int end = header2 != null ? Markdown.getIndexInParent(header2) - 1 : numChildren - 1;
    for (int nodeIndex = start; nodeIndex <= end; nodeIndex++) {
        Node child = parent.jjtGetChild(nodeIndex);
        child.jjtSetParent(doc);
        doc.jjtAddChild(child, nodeIndex - start);
    }
    // to each document
    for (ResourceDefinition rd : v.resourceDefinitions) {
        ResourceDefinition copy = copy(rd);
        copy.jjtSetParent(doc);
        doc.jjtAddChild(copy, doc.jjtGetNumChildren());
    }
    return doc;
}
Also used : ResourceDefinition(org.tautua.markdownpapers.ast.ResourceDefinition) Node(org.tautua.markdownpapers.ast.Node) SimpleNode(org.tautua.markdownpapers.ast.SimpleNode) Document(org.tautua.markdownpapers.ast.Document)

Example 5 with Document

use of org.tautua.markdownpapers.ast.Document in project ceylon by eclipse.

the class PlaintextMarkdownVisitor method visit.

@Override
public void visit(Header node) {
    final Node parent = node.jjtGetParent();
    for (int ii = 0; ii < parent.jjtGetNumChildren(); ii++) {
        if (parent.jjtGetChild(ii) == node) {
            if (ii == 0) {
                continue;
            }
        }
    }
    switch(node.getLevel()) {
        case 1:
        case 2:
            out.setIndent(0);
            break;
        case 3:
            out.setIndent(3);
            break;
        case 4:
            out.setIndent(5);
            break;
        case 5:
            out.setIndent(6);
            break;
        case 6:
        default:
            out.setIndent(7);
            break;
    }
    if (!(node.jjtGetParent() instanceof Document && Markdown.getIndexInParent(node) == 0)) {
        out.newline();
    }
    switch(node.getLevel()) {
        case 1:
            int col = out.getColumn();
            this.headerLevel = node.getLevel();
            node.childrenAccept(this);
            this.headerLevel = -1;
            int num = out.getColumn() - col;
            out.newline();
            for (int ii = 0; ii < num; ii++) {
                out.append("=");
            }
            out.newline();
            out.setIndent(8);
            break;
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
        default:
            this.headerLevel = node.getLevel();
            node.childrenAccept(this);
            this.headerLevel = -1;
            out.newline();
            out.setIndent(8);
            break;
    }
    out.newline();
}
Also used : Node(org.tautua.markdownpapers.ast.Node) Document(org.tautua.markdownpapers.ast.Document)

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