Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public void visit(Code code) {
}
});

String finalString = "";
StringBuilder sb = new StringBuilder();
for (String word : wordList) {
finalString += word;
sb.append(word);
}
finalString = finalString.trim().toLowerCase();
String finalString = sb.toString().trim().toLowerCase();

attributes.put("id", idGenerator.generateId(finalString));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,46 @@ public BlockQuote getBlock() {
return block;
}

private static int calculateNewColumn(ParserState state, int nextNonSpace) {
int newColumn = state.getColumn() + state.getIndent() + 1;

if (Characters.isSpaceOrTab(state.getLine().getContent(), nextNonSpace + 1)) {
newColumn++;
}

return newColumn;
}

@Override
public BlockContinue tryContinue(ParserState state) {
int nextNonSpace = state.getNextNonSpaceIndex();
if (isMarker(state, nextNonSpace)) {
int newColumn = state.getColumn() + state.getIndent() + 1;
// optional following space or tab
if (Characters.isSpaceOrTab(state.getLine().getContent(), nextNonSpace + 1)) {
newColumn++;
}
return BlockContinue.atColumn(newColumn);
} else {
return BlockContinue.none();
return BlockContinue.atColumn(
calculateNewColumn(state, nextNonSpace));
}
}

return BlockContinue.none();
}


private static boolean isMarker(ParserState state, int index) {
CharSequence line = state.getLine().getContent();
return state.getIndent() < Parsing.CODE_BLOCK_INDENT && index < line.length() && line.charAt(index) == '>';
}



public static class Factory extends AbstractBlockParserFactory {
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
int nextNonSpace = state.getNextNonSpaceIndex();
if (isMarker(state, nextNonSpace)) {
int newColumn = state.getColumn() + state.getIndent() + 1;
// optional following space or tab
if (Characters.isSpaceOrTab(state.getLine().getContent(), nextNonSpace + 1)) {
newColumn++;
}
return BlockStart.of(new BlockQuoteParser()).atColumn(newColumn);
} else {
return BlockStart.none();
return BlockStart.of(new BlockQuoteParser())
.atColumn(BlockQuoteParser.calculateNewColumn(state, nextNonSpace));
}

return BlockStart.none();
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ int getReplaceParagraphLines() {
}

@Override
public BlockStart atIndex(int newIndex) {
this.newIndex = newIndex;
public BlockStart atIndex(int targetIndex) {
this.newIndex = targetIndex;
return this;
}

@Override
public BlockStart atColumn(int newColumn) {
this.newColumn = newColumn;
public BlockStart atColumn(int targetColumn) {
this.newColumn = targetColumn;
return this;
}

Expand Down
14 changes: 9 additions & 5 deletions commonmark/src/main/java/org/commonmark/internal/Delimiter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ public class Delimiter implements DelimiterRun {
public Delimiter previous;
public Delimiter next;

public Delimiter(List<Text> characters, char delimiterChar, boolean canOpen, boolean canClose, Delimiter previous) {
this.characters = characters;
this.delimiterChar = delimiterChar;
public Delimiter(List<Text> delimiterTexts,
char delimiterMarker,
boolean canOpen,
boolean canClose,
Delimiter previous) {

this.characters = delimiterTexts;
this.delimiterChar = delimiterMarker;
this.canOpen = canOpen;
this.canClose = canClose;
this.previous = previous;
this.originalLength = characters.size();
this.originalLength = delimiterTexts.size();
}

@Override
public boolean canOpen() {
return canOpen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,20 @@ public class DocumentParser implements ParserState {

private final List<OpenBlockParser> openBlockParsers = new ArrayList<>();
private final List<BlockParser> allBlockParsers = new ArrayList<>();

public DocumentParser(List<BlockParserFactory> blockParserFactories, InlineParserFactory inlineParserFactory,
List<InlineContentParserFactory> inlineContentParserFactories, List<DelimiterProcessor> delimiterProcessors,
List<LinkProcessor> linkProcessors, Set<Character> linkMarkers,
IncludeSourceSpans includeSourceSpans, int maxOpenBlockParsers) {
this.blockParserFactories = blockParserFactories;
this.inlineParserFactory = inlineParserFactory;
this.inlineContentParserFactories = inlineContentParserFactories;
this.delimiterProcessors = delimiterProcessors;
this.linkProcessors = linkProcessors;
this.linkMarkers = linkMarkers;
this.includeSourceSpans = includeSourceSpans;
this.maxOpenBlockParsers = maxOpenBlockParsers;
private static final int TAB_SIZE = 4;
private static final int CODE_INDENT = 4;
private static final int NO_INDEX = -1;
private static final int NO_COLUMN = -1;

public DocumentParser(DocumentParserConfig config) {
this.blockParserFactories = config.getBlockParserFactories();
this.inlineParserFactory = config.getInlineParserFactory();
this.inlineContentParserFactories = config.getInlineContentParserFactories();
this.delimiterProcessors = config.getDelimiterProcessors();
this.linkProcessors = config.getLinkProcessors();
this.linkMarkers = config.getLinkMarkers();
this.includeSourceSpans = config.getIncludeSourceSpans();
this.maxOpenBlockParsers = config.getMaxOpenBlockParsers();

this.documentBlockParser = new DocumentBlockParser();
activateBlockParser(new OpenBlockParser(documentBlockParser, 0));
Expand Down Expand Up @@ -199,6 +200,13 @@ public BlockParser getActiveBlockParser() {
* Analyze a line of text and update the document appropriately. We parse markdown text by calling this on each
* line of input, then finalizing the document.
*/
private boolean shouldFinalizeBlock(BlockContinueImpl blockContinue) {
return blockContinue.isFinalize();
}
private void handleBlockFinalization(int index) {
addSourceSpans();
closeBlockParsers(openBlockParsers.size() - index);
}
private void parseLine(String ln, int inputIndex) {
setLine(ln, inputIndex);

Expand All @@ -214,14 +222,14 @@ private void parseLine(String ln, int inputIndex) {
if (result instanceof BlockContinueImpl) {
BlockContinueImpl blockContinue = (BlockContinueImpl) result;
openBlockParser.sourceIndex = getIndex();
if (blockContinue.isFinalize()) {
addSourceSpans();
closeBlockParsers(openBlockParsers.size() - i);
if (shouldFinalizeBlock(blockContinue)) {
handleBlockFinalization(i);
return;

} else {
if (blockContinue.getNewIndex() != -1) {
if (blockContinue.getNewIndex() != NO_INDEX) {
setNewIndex(blockContinue.getNewIndex());
} else if (blockContinue.getNewColumn() != -1) {
} else if (blockContinue.getNewColumn() != NO_COLUMN) {
setNewColumn(blockContinue.getNewColumn());
}
matches++;
Expand All @@ -245,7 +253,7 @@ private void parseLine(String ln, int inputIndex) {
findNextNonSpace();

// this is a little performance optimization:
if (isBlank() || (indent < Parsing.CODE_BLOCK_INDENT && Characters.isLetter(this.line.getContent(), nextNonSpace))) {
if (isBlank() || (indent < CODE_INDENT && Characters.isLetter(this.line.getContent(), nextNonSpace))) {
setNewIndex(nextNonSpace);
break;
}
Expand Down Expand Up @@ -359,7 +367,7 @@ private void findNextNonSpace() {
continue;
case '\t':
i++;
cols += (4 - (cols % 4));
cols += (TAB_SIZE - (cols % TAB_SIZE));
continue;
}
blank = false;
Expand Down Expand Up @@ -604,3 +612,4 @@ private static class OpenBlockParser {
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.commonmark.internal;

import org.commonmark.parser.IncludeSourceSpans;
import org.commonmark.parser.InlineParserFactory;
import org.commonmark.parser.beta.InlineContentParserFactory;
import org.commonmark.parser.beta.LinkProcessor;
import org.commonmark.parser.block.BlockParserFactory;
import org.commonmark.parser.delimiter.DelimiterProcessor;

import java.util.List;
import java.util.Set;

public class DocumentParserConfig {

private final List<BlockParserFactory> blockParserFactories;
private final InlineParserFactory inlineParserFactory;
private final List<InlineContentParserFactory> inlineContentParserFactories;
private final List<DelimiterProcessor> delimiterProcessors;
private final List<LinkProcessor> linkProcessors;
private final Set<Character> linkMarkers;
private final IncludeSourceSpans includeSourceSpans;
private final int maxOpenBlockParsers;

public DocumentParserConfig(
List<BlockParserFactory> blockParserFactories,
InlineParserFactory inlineParserFactory,
List<InlineContentParserFactory> inlineContentParserFactories,
List<DelimiterProcessor> delimiterProcessors,
List<LinkProcessor> linkProcessors,
Set<Character> linkMarkers,
IncludeSourceSpans includeSourceSpans,
int maxOpenBlockParsers) {

this.blockParserFactories = blockParserFactories;
this.inlineParserFactory = inlineParserFactory;
this.inlineContentParserFactories = inlineContentParserFactories;
this.delimiterProcessors = delimiterProcessors;
this.linkProcessors = linkProcessors;
this.linkMarkers = linkMarkers;
this.includeSourceSpans = includeSourceSpans;
this.maxOpenBlockParsers = maxOpenBlockParsers;
}

// getters
public List<BlockParserFactory> getBlockParserFactories() {
return blockParserFactories;
}

public InlineParserFactory getInlineParserFactory() {
return inlineParserFactory;
}

public List<InlineContentParserFactory> getInlineContentParserFactories() {
return inlineContentParserFactories;
}

public List<DelimiterProcessor> getDelimiterProcessors() {
return delimiterProcessors;
}

public List<LinkProcessor> getLinkProcessors() {
return linkProcessors;
}

public Set<Character> getLinkMarkers() {
return linkMarkers;
}

public IncludeSourceSpans getIncludeSourceSpans() {
return includeSourceSpans;
}

public int getMaxOpenBlockParsers() {
return maxOpenBlockParsers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class FencedCodeBlockParser extends AbstractBlockParser {
private final FencedCodeBlock block = new FencedCodeBlock();
private final char fenceChar;
private final int openingFenceLength;
private static final int MIN_FENCE_LENGTH = 3;

private String firstLine;
private StringBuilder otherLines = new StringBuilder();
Expand Down Expand Up @@ -106,13 +107,13 @@ private static FencedCodeBlockParser checkOpener(CharSequence line, int index, i
break loop;
}
}
if (backticks >= 3 && tildes == 0) {
if (backticks >= MIN_FENCE_LENGTH && tildes == 0) {
// spec: If the info string comes after a backtick fence, it may not contain any backtick characters.
if (Characters.find('`', line, index + backticks) != -1) {
return null;
}
return new FencedCodeBlockParser('`', backticks, indent);
} else if (tildes >= 3 && backticks == 0) {
} else if (tildes >= MIN_FENCE_LENGTH && backticks == 0) {
// spec: Info strings for tilde code blocks can contain backticks and tildes
return new FencedCodeBlockParser('~', tildes, indent);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class HeadingParser extends AbstractBlockParser {

private final Heading block = new Heading();
private final SourceLines content;
private static final int MAX_HEADING_LEVEL = 6;

public HeadingParser(int level, SourceLines content) {
block.setLevel(level);
Expand Down Expand Up @@ -76,7 +77,7 @@ private static HeadingParser getAtxHeading(SourceLine line) {
Scanner scanner = Scanner.of(SourceLines.of(line));
int level = scanner.matchMultiple('#');

if (level == 0 || level > 6) {
if (level == 0 || level > MAX_HEADING_LEVEL) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class HtmlBlockParser extends AbstractBlockParser {

private static final String OPENTAG = "<" + TAGNAME + ATTRIBUTE + "*" + "\\s*/?>";
private static final String CLOSETAG = "</" + TAGNAME + "\\s*[>]";
private static final int CODE_BLOCK_INDENT = 4;
private static final int MAX_HTML_BLOCK_TYPE = 7;

private static final Pattern[][] BLOCK_PATTERNS = new Pattern[][]{
{null, null}, // not used (no type 0)
Expand Down Expand Up @@ -124,8 +126,8 @@ public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockPar
int nextNonSpace = state.getNextNonSpaceIndex();
CharSequence line = state.getLine().getContent();

if (state.getIndent() < 4 && line.charAt(nextNonSpace) == '<') {
for (int blockType = 1; blockType <= 7; blockType++) {
if (state.getIndent() < CODE_BLOCK_INDENT && line.charAt(nextNonSpace) == '<') {
for (int blockType = 1; blockType <= MAX_HTML_BLOCK_TYPE; blockType++) {
// Type 7 can not interrupt a paragraph (not even a lazy one)
if (blockType == 7 && (
matchedBlockParser.getMatchedBlockParser().getBlock() instanceof Paragraph ||
Expand Down
Loading