diff --git a/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java b/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java index 83eaff6e7..aeab876d4 100644 --- a/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java +++ b/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java @@ -41,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<>(); @@ -49,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) { @@ -62,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 ca4a25aad..7d46dfb0d 100644 --- a/commonmark/src/main/java/org/commonmark/parser/Parser.java +++ b/commonmark/src/main/java/org/commonmark/parser/Parser.java @@ -45,22 +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); - for (PostProcessor postProcessor : postProcessors) { - document = postProcessor.process(document); - } - return document; + return postProcess(document); } - public Node parse(Reader input) throws IOException { + 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 index c0bffd2ba..cc2dda1fc 100644 --- a/commonmark/src/test/java/org/commonmark/test/ParserTest.java +++ b/commonmark/src/test/java/org/commonmark/test/ParserTest.java @@ -17,7 +17,7 @@ public void ioReaderTest() throws IOException { InputStream input1 = ParserTest.class.getResourceAsStream("/spec.txt"); Node document1; try (InputStreamReader reader = new InputStreamReader(input1)) { - document1 = parser.parse(reader); + document1 = parser.parseReader(reader); } InputStream input2 = ParserTest.class.getResourceAsStream("/spec.txt");