diff --git a/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java b/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java index 9121d6d49..aeab876d4 100644 --- a/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java +++ b/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java @@ -1,5 +1,8 @@ package org.commonmark.internal; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.Reader; import org.commonmark.internal.util.Parsing; import org.commonmark.internal.util.Substring; import org.commonmark.node.*; @@ -38,6 +41,7 @@ public class DocumentParser implements ParserState { private final List blockParserFactories; private final InlineParserImpl inlineParser; + private final DocumentBlockParser documentBlockParser; private List activeBlockParsers = new ArrayList<>(); private Set allBlockParsers = new HashSet<>(); @@ -46,6 +50,9 @@ public class DocumentParser implements ParserState { public DocumentParser(List blockParserFactories, InlineParserImpl inlineParser) { this.blockParserFactories = blockParserFactories; this.inlineParser = inlineParser; + + this.documentBlockParser = new DocumentBlockParser(); + activateBlockParser(this.documentBlockParser); } public static List calculateBlockParserFactories(List customBlockParserFactories) { @@ -59,9 +66,6 @@ public static List calculateBlockParserFactories(List blockParsers) { return true; } + private Document finalizeAndProcess() { + finalizeBlocks(this.activeBlockParsers); + this.processInlines(); + return this.documentBlockParser.getBlock(); + } + private static class MatchedBlockParserImpl implements MatchedBlockParser { private final BlockParser matchedBlockParser; diff --git a/commonmark/src/main/java/org/commonmark/parser/Parser.java b/commonmark/src/main/java/org/commonmark/parser/Parser.java index d2c34f5c2..7d46dfb0d 100644 --- a/commonmark/src/main/java/org/commonmark/parser/Parser.java +++ b/commonmark/src/main/java/org/commonmark/parser/Parser.java @@ -1,5 +1,7 @@ package org.commonmark.parser; +import java.io.IOException; +import java.io.Reader; import org.commonmark.Extension; import org.commonmark.internal.DocumentParser; import org.commonmark.internal.InlineParserImpl; @@ -43,12 +45,23 @@ public Node parse(String input) { InlineParserImpl inlineParser = new InlineParserImpl(specialCharacters, delimiterCharacters, delimiterProcessors); DocumentParser documentParser = new DocumentParser(blockParserFactories, inlineParser); Node document = documentParser.parse(input); + return postProcess(document); + } + + public Node parseReader(Reader input) throws IOException { + InlineParserImpl inlineParser = new InlineParserImpl(specialCharacters, delimiterCharacters, delimiterProcessors); + DocumentParser documentParser = new DocumentParser(blockParserFactories, inlineParser); + Node document = documentParser.parse(input); + return postProcess(document); + } + + private Node postProcess(Node document) { for (PostProcessor postProcessor : postProcessors) { document = postProcessor.process(document); } return document; } - + public static class Builder { private final List blockParserFactories = new ArrayList<>(); private final List delimiterProcessors = new ArrayList<>(); diff --git a/commonmark/src/test/java/org/commonmark/test/ParserTest.java b/commonmark/src/test/java/org/commonmark/test/ParserTest.java new file mode 100644 index 000000000..cc2dda1fc --- /dev/null +++ b/commonmark/src/test/java/org/commonmark/test/ParserTest.java @@ -0,0 +1,37 @@ +package org.commonmark.test; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import org.commonmark.html.HtmlRenderer; +import org.commonmark.node.Node; +import org.commonmark.parser.Parser; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class ParserTest { + @Test + public void ioReaderTest() throws IOException { + Parser parser = Parser.builder().build(); + + InputStream input1 = ParserTest.class.getResourceAsStream("/spec.txt"); + Node document1; + try (InputStreamReader reader = new InputStreamReader(input1)) { + document1 = parser.parseReader(reader); + } + + InputStream input2 = ParserTest.class.getResourceAsStream("/spec.txt"); + StringBuilder sb = new StringBuilder(); + try (InputStreamReader reader = new InputStreamReader(input2)) { + int ch; + while ((ch = reader.read()) != -1){ + sb.append((char)ch); + } + } + + Node document2 = parser.parse(sb.toString()); + + HtmlRenderer renderer = HtmlRenderer.builder().escapeHtml(true).build(); + assertEquals(renderer.render(document2), renderer.render(document1)); + } +}