From 607f6c54715c305a4b6ecd442780f69a15f6eeb5 Mon Sep 17 00:00:00 2001 From: Vadim Sntyaev Date: Tue, 28 Jul 2015 22:32:39 +0300 Subject: [PATCH 1/2] #2 Reader to input for Parser --- .../commonmark/internal/DocumentParser.java | 24 ++++++++++++ .../java/org/commonmark/parser/Parser.java | 12 ++++++ .../java/org/commonmark/test/ParserTest.java | 37 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 commonmark/src/test/java/org/commonmark/test/ParserTest.java diff --git a/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java b/commonmark/src/main/java/org/commonmark/internal/DocumentParser.java index 9121d6d49..83eaff6e7 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.*; @@ -81,6 +84,27 @@ public Document parse(String input) { this.processInlines(); return documentBlockParser.getBlock(); } + + public Document parse(Reader input) throws IOException { + DocumentBlockParser documentBlockParser = new DocumentBlockParser(); + activateBlockParser(documentBlockParser); + + BufferedReader bufferedReader; + if (input instanceof BufferedReader) { + bufferedReader = (BufferedReader) input; + } else { + bufferedReader = new BufferedReader(input); + } + + String line; + while ((line = bufferedReader.readLine()) != null) { + incorporateLine(line); + } + + finalizeBlocks(activeBlockParsers); + this.processInlines(); + return documentBlockParser.getBlock(); + } @Override public CharSequence getLine() { diff --git a/commonmark/src/main/java/org/commonmark/parser/Parser.java b/commonmark/src/main/java/org/commonmark/parser/Parser.java index d2c34f5c2..ca4a25aad 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; @@ -48,6 +50,16 @@ public Node parse(String input) { } return document; } + + public Node parse(Reader input) throws IOException { + 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; + } public static class Builder { private final List blockParserFactories = 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..c0bffd2ba --- /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.parse(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)); + } +} From ba166e40986cbbd7dfd6368a3130a2c811b204fe Mon Sep 17 00:00:00 2001 From: Vadim Sentyaev Date: Fri, 7 Aug 2015 00:18:59 +0300 Subject: [PATCH 2/2] removed duplications and rename parse to parseReader --- .../commonmark/internal/DocumentParser.java | 24 +++++++++---------- .../java/org/commonmark/parser/Parser.java | 13 +++++----- .../java/org/commonmark/test/ParserTest.java | 2 +- 3 files changed, 20 insertions(+), 19 deletions(-) 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");