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"));
}
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));
}
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;
}
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;
}
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();
}
Aggregations