Skip to content

Commit

Permalink
removed duplications and rename parse to parseReader
Browse files Browse the repository at this point in the history
  • Loading branch information
sentyaev committed Aug 6, 2015
1 parent 607f6c5 commit ba166e4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class DocumentParser implements ParserState {

private final List<BlockParserFactory> blockParserFactories;
private final InlineParserImpl inlineParser;
private final DocumentBlockParser documentBlockParser;

private List<BlockParser> activeBlockParsers = new ArrayList<>();
private Set<BlockParser> allBlockParsers = new HashSet<>();
Expand All @@ -49,6 +50,9 @@ public class DocumentParser implements ParserState {
public DocumentParser(List<BlockParserFactory> blockParserFactories, InlineParserImpl inlineParser) {
this.blockParserFactories = blockParserFactories;
this.inlineParser = inlineParser;

this.documentBlockParser = new DocumentBlockParser();
activateBlockParser(this.documentBlockParser);
}

public static List<BlockParserFactory> calculateBlockParserFactories(List<BlockParserFactory> customBlockParserFactories) {
Expand All @@ -62,9 +66,6 @@ public static List<BlockParserFactory> calculateBlockParserFactories(List<BlockP
* The main parsing function. Returns a parsed document AST.
*/
public Document parse(String input) {
DocumentBlockParser documentBlockParser = new DocumentBlockParser();
activateBlockParser(documentBlockParser);

int lineStart = 0;
int lineBreak;
while ((lineBreak = Parsing.findLineBreak(input, lineStart)) != -1) {
Expand All @@ -80,15 +81,10 @@ public Document parse(String input) {
incorporateLine(Substring.of(input, lineStart, input.length()));
}

finalizeBlocks(activeBlockParsers);
this.processInlines();
return documentBlockParser.getBlock();
return finalizeAndProcess();
}

public Document parse(Reader input) throws IOException {
DocumentBlockParser documentBlockParser = new DocumentBlockParser();
activateBlockParser(documentBlockParser);

BufferedReader bufferedReader;
if (input instanceof BufferedReader) {
bufferedReader = (BufferedReader) input;
Expand All @@ -101,9 +97,7 @@ public Document parse(Reader input) throws IOException {
incorporateLine(line);
}

finalizeBlocks(activeBlockParsers);
this.processInlines();
return documentBlockParser.getBlock();
return finalizeAndProcess();
}

@Override
Expand Down Expand Up @@ -496,6 +490,12 @@ private boolean finalizeBlocks(List<BlockParser> 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;
Expand Down
13 changes: 7 additions & 6 deletions commonmark/src/main/java/org/commonmark/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<BlockParserFactory> blockParserFactories = new ArrayList<>();
private final List<DelimiterProcessor> delimiterProcessors = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit ba166e4

Please sign in to comment.