Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions commonmark/src/main/java/org/commonmark/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public static Builder builder() {
return new Builder();
}

private Node processParsedDocument(Node document) {
return postProcess(document);
}

/**
* Parse the specified input text into a tree of nodes.
* <p>
Expand All @@ -74,11 +78,12 @@ public static Builder builder() {
* @param input the text to parse - must not be null
* @return the root node
*/

public Node parse(String input) {
Objects.requireNonNull(input, "input must not be null");
DocumentParser documentParser = createDocumentParser();
Node document = documentParser.parse(input);
return postProcess(document);
Objects.requireNonNull(input);
DocumentParser documentParser = createDocumentParser();

return processParsedDocument(documentParser.parse(input));
}

/**
Expand All @@ -100,10 +105,10 @@ public Node parse(String input) {
* @throws IOException when reading throws an exception
*/
public Node parseReader(Reader input) throws IOException {
Objects.requireNonNull(input, "input must not be null");
Objects.requireNonNull(input);
DocumentParser documentParser = createDocumentParser();
Node document = documentParser.parse(input);
return postProcess(document);
return processParsedDocument(documentParser.parse(input));

}

private DocumentParser createDocumentParser() {
Expand Down Expand Up @@ -147,13 +152,18 @@ public Parser build() {
public Builder extensions(Iterable<? extends Extension> extensions) {
Objects.requireNonNull(extensions, "extensions must not be null");
for (Extension extension : extensions) {
applyExtension(extension);
}
return this;
}
private void applyExtension(Extension extension){
if (extension instanceof ParserExtension) {
ParserExtension parserExtension = (ParserExtension) extension;
parserExtension.extend(this);
}
}
return this;
}



/**
* Describe the list of markdown features the parser will recognize and parse.
Expand Down Expand Up @@ -217,12 +227,15 @@ public Builder includeSourceSpans(IncludeSourceSpans includeSourceSpans) {
* @return {@code this}
*/
public Builder maxOpenBlockParsers(int maxOpenBlockParsers) {
if (maxOpenBlockParsers < 0) {
throw new IllegalArgumentException("maxOpenBlockParsers must be >= 0");
}
validateMaxOpenBlockParsers(maxOpenBlockParsers);
this.maxOpenBlockParsers = maxOpenBlockParsers;
return this;
}
private void validateMaxOpenBlockParsers(int value){
if (value < 0) {
throw new IllegalArgumentException("maxOpenBlockParsers must be >= 0");
}
}

/**
* Add a custom block parser factory.
Expand Down Expand Up @@ -335,11 +348,10 @@ public Builder inlineParserFactory(InlineParserFactory inlineParserFactory) {
}

private InlineParserFactory getInlineParserFactory() {
if (inlineParserFactory != null) {
return inlineParserFactory;
} else {
return InlineParserImpl::new;
}
return inlineParserFactory != null
? inlineParserFactory :
InlineParserImpl::new;

}
}

Expand Down