Java Code Examples for org.commonmark.internal.util.Parsing#CODE_BLOCK_INDENT

The following examples show how to use org.commonmark.internal.util.Parsing#CODE_BLOCK_INDENT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JLatexMathBlockParser.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState parserState) {
    final int nextNonSpaceIndex = parserState.getNextNonSpaceIndex();
    final CharSequence line = parserState.getLine();
    final int length = line.length();

    // check for closing
    if (parserState.getIndent() < Parsing.CODE_BLOCK_INDENT) {
        if (consume(DOLLAR, line, nextNonSpaceIndex, length) == signs) {
            // okay, we have our number of signs
            // let's consume spaces until the end
            if (Parsing.skip(SPACE, line, nextNonSpaceIndex + signs, length) == length) {
                return BlockContinue.finished();
            }
        }
    }

    return BlockContinue.atIndex(parserState.getIndex());
}
 
Example 2
Source File: HeadingParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
    if (state.getIndent() >= Parsing.CODE_BLOCK_INDENT) {
        return BlockStart.none();
    }

    CharSequence line = state.getLine();
    int nextNonSpace = state.getNextNonSpaceIndex();
    HeadingParser atxHeading = getAtxHeading(line, nextNonSpace);
    if (atxHeading != null) {
        return BlockStart.of(atxHeading).atIndex(line.length());
    }

    int setextHeadingLevel = getSetextHeadingLevel(line, nextNonSpace);
    if (setextHeadingLevel > 0) {
        CharSequence paragraph = matchedBlockParser.getParagraphContent();
        if (paragraph != null) {
            String content = paragraph.toString();
            return BlockStart.of(new HeadingParser(setextHeadingLevel, content))
                    .atIndex(line.length())
                    .replaceActiveBlockParser();
        }
    }

    return BlockStart.none();
}
 
Example 3
Source File: FencedCodeBlockParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState state) {
    int nextNonSpace = state.getNextNonSpaceIndex();
    int newIndex = state.getIndex();
    CharSequence line = state.getLine();
    boolean closing = state.getIndent() < Parsing.CODE_BLOCK_INDENT && isClosing(line, nextNonSpace);
    if (closing) {
        // closing fence - we're at end of line, so we can finalize now
        return BlockContinue.finished();
    } else {
        // skip optional spaces of fence indent
        int i = block.getFenceIndent();
        int length = line.length();
        while (i > 0 && newIndex < length && line.charAt(newIndex) == ' ') {
            newIndex++;
            i--;
        }
    }
    return BlockContinue.atIndex(newIndex);
}
 
Example 4
Source File: IndentedCodeBlockParser.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
    // An indented code block cannot interrupt a paragraph.
    if (state.getIndent() >= Parsing.CODE_BLOCK_INDENT && !state.isBlank() && !(state.getActiveBlockParser().getBlock() instanceof Paragraph)) {
        return BlockStart.of(new IndentedCodeBlockParser()).atColumn(state.getColumn() + Parsing.CODE_BLOCK_INDENT);
    } else {
        return BlockStart.none();
    }
}
 
Example 5
Source File: IndentedCodeBlockParser.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState state) {
    if (state.getIndent() >= Parsing.CODE_BLOCK_INDENT) {
        return BlockContinue.atColumn(state.getColumn() + Parsing.CODE_BLOCK_INDENT);
    } else if (state.isBlank()) {
        return BlockContinue.atIndex(state.getNextNonSpaceIndex());
    } else {
        return BlockContinue.none();
    }
}
 
Example 6
Source File: ListBlockParser.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
    BlockParser matched = matchedBlockParser.getMatchedBlockParser();

    if (state.getIndent() >= Parsing.CODE_BLOCK_INDENT && !(matched instanceof ListBlockParser)) {
        return BlockStart.none();
    }
    int markerIndex = state.getNextNonSpaceIndex();
    int markerColumn = state.getColumn() + state.getIndent();
    boolean inParagraph = matchedBlockParser.getParagraphContent() != null;
    ListData listData = parseListMarker(state.getLine(), markerIndex, markerColumn, inParagraph);
    if (listData == null) {
        return BlockStart.none();
    }

    int newColumn = listData.contentColumn;
    ListItemParser listItemParser = new ListItemParser(newColumn - state.getColumn());

    // prepend the list block if needed
    if (!(matched instanceof ListBlockParser) ||
            !(listsMatch((ListBlock) matched.getBlock(), listData.listBlock))) {

        ListBlockParser listBlockParser = new ListBlockParser(listData.listBlock);
        listBlockParser.setTight(true);

        return BlockStart.of(listBlockParser, listItemParser).atColumn(newColumn);
    } else {
        return BlockStart.of(listItemParser).atColumn(newColumn);
    }
}
 
Example 7
Source File: ListBlockParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
    BlockParser matched = matchedBlockParser.getMatchedBlockParser();

    if (state.getIndent() >= Parsing.CODE_BLOCK_INDENT) {
        return BlockStart.none();
    }
    int markerIndex = state.getNextNonSpaceIndex();
    int markerColumn = state.getColumn() + state.getIndent();
    boolean inParagraph = matchedBlockParser.getParagraphContent() != null;
    ListData listData = parseList(state.getLine(), markerIndex, markerColumn, inParagraph);
    if (listData == null) {
        return BlockStart.none();
    }

    int newColumn = listData.contentColumn;
    ListItemParser listItemParser = new ListItemParser(newColumn - state.getColumn());

    // prepend the list block if needed
    if (!(matched instanceof ListBlockParser) ||
            !(listsMatch((ListBlock) matched.getBlock(), listData.listBlock))) {

        ListBlockParser listBlockParser = new ListBlockParser(listData.listBlock);
        // We start out with assuming a list is tight. If we find a blank line, we set it to loose later.
        listData.listBlock.setTight(true);

        return BlockStart.of(listBlockParser, listItemParser).atColumn(newColumn);
    } else {
        return BlockStart.of(listItemParser).atColumn(newColumn);
    }
}
 
Example 8
Source File: JLatexMathBlockParser.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {

    // let's define the spec:
    //  * 0-3 spaces before are allowed (Parsing.CODE_BLOCK_INDENT = 4)
    //  * 2+ subsequent `$` signs
    //  * any optional amount of spaces
    //  * new line
    //  * block is closed when the same amount of opening signs is met

    final int indent = state.getIndent();

    // check if it's an indented code block
    if (indent >= Parsing.CODE_BLOCK_INDENT) {
        return BlockStart.none();
    }

    final int nextNonSpaceIndex = state.getNextNonSpaceIndex();
    final CharSequence line = state.getLine();
    final int length = line.length();

    final int signs = consume(DOLLAR, line, nextNonSpaceIndex, length);

    // 2 is minimum
    if (signs < 2) {
        return BlockStart.none();
    }

    // consume spaces until the end of the line, if any other content is found -> NONE
    if (Parsing.skip(SPACE, line, nextNonSpaceIndex + signs, length) != length) {
        return BlockStart.none();
    }

    return BlockStart.of(new JLatexMathBlockParser(signs))
            .atIndex(length + 1);
}
 
Example 9
Source File: NoticeBlockParser.java    From maven-confluence-plugin with Apache License 2.0 5 votes vote down vote up
private static Optional<Matcher> isStartedMarker(ParserState state, int index) {
    final CharSequence line = state.getLine();

    return ( state.getIndent() < Parsing.CODE_BLOCK_INDENT && index < line.length() ) ?
        ofNullable(pattern.matcher(line)).filter( m -> m.matches() ) :
        Optional.empty();
}
 
Example 10
Source File: IndentedCodeBlockParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState state) {
    if (state.getIndent() >= Parsing.CODE_BLOCK_INDENT) {
        return BlockContinue.atColumn(state.getColumn() + Parsing.CODE_BLOCK_INDENT);
    } else if (state.isBlank()) {
        return BlockContinue.atIndex(state.getNextNonSpaceIndex());
    } else {
        return BlockContinue.none();
    }
}
 
Example 11
Source File: IndentedCodeBlockParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
    // An indented code block cannot interrupt a paragraph.
    if (state.getIndent() >= Parsing.CODE_BLOCK_INDENT && !state.isBlank() && !(state.getActiveBlockParser().getBlock() instanceof Paragraph)) {
        return BlockStart.of(new IndentedCodeBlockParser()).atColumn(state.getColumn() + Parsing.CODE_BLOCK_INDENT);
    } else {
        return BlockStart.none();
    }
}
 
Example 12
Source File: FencedCodeBlockParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
    int indent = state.getIndent();
    if (indent >= Parsing.CODE_BLOCK_INDENT) {
        return BlockStart.none();
    }

    int nextNonSpace = state.getNextNonSpaceIndex();
    FencedCodeBlockParser blockParser = checkOpener(state.getLine(), nextNonSpace, indent);
    if (blockParser != null) {
        return BlockStart.of(blockParser).atIndex(nextNonSpace + blockParser.block.getFenceLength());
    } else {
        return BlockStart.none();
    }
}
 
Example 13
Source File: BlockQuoteParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static boolean isMarker(ParserState state, int index) {
    CharSequence line = state.getLine();
    return state.getIndent() < Parsing.CODE_BLOCK_INDENT && index < line.length() && line.charAt(index) == '>';
}
 
Example 14
Source File: ListBlockParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * Parse a list marker and return data on the marker or null.
 */
private static ListData parseList(CharSequence line, final int markerIndex, final int markerColumn,
                                  final boolean inParagraph) {
    ListMarkerData listMarker = parseListMarker(line, markerIndex);
    if (listMarker == null) {
        return null;
    }
    ListBlock listBlock = listMarker.listBlock;

    int indexAfterMarker = listMarker.indexAfterMarker;
    int markerLength = indexAfterMarker - markerIndex;
    // marker doesn't include tabs, so counting them as columns directly is ok
    int columnAfterMarker = markerColumn + markerLength;
    // the column within the line where the content starts
    int contentColumn = columnAfterMarker;

    // See at which column the content starts if there is content
    boolean hasContent = false;
    int length = line.length();
    for (int i = indexAfterMarker; i < length; i++) {
        char c = line.charAt(i);
        if (c == '\t') {
            contentColumn += Parsing.columnsToNextTabStop(contentColumn);
        } else if (c == ' ') {
            contentColumn++;
        } else {
            hasContent = true;
            break;
        }
    }

    if (inParagraph) {
        // If the list item is ordered, the start number must be 1 to interrupt a paragraph.
        if (listBlock instanceof OrderedList && ((OrderedList) listBlock).getStartNumber() != 1) {
            return null;
        }
        // Empty list item can not interrupt a paragraph.
        if (!hasContent) {
            return null;
        }
    }

    if (!hasContent || (contentColumn - columnAfterMarker) > Parsing.CODE_BLOCK_INDENT) {
        // If this line is blank or has a code block, default to 1 space after marker
        contentColumn = columnAfterMarker + 1;
    }

    return new ListData(listBlock, contentColumn);
}
 
Example 15
Source File: NoticeBlockParser.java    From maven-confluence-plugin with Apache License 2.0 4 votes vote down vote up
private static boolean isMarker(ParserState state, int index) {
    CharSequence line = state.getLine();
    return state.getIndent() < Parsing.CODE_BLOCK_INDENT && index < line.length() && line.charAt(index) == '>';
}
 
Example 16
Source File: BlockQuoteParser.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
private static boolean isMarker(ParserState state, int index) {
    CharSequence line = state.getLine();
    return state.getIndent() < Parsing.CODE_BLOCK_INDENT && index < line.length() && line.charAt(index) == '>';
}
 
Example 17
Source File: ListBlockParser.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
/**
 * Parse a list marker and return data on the marker or null.
 */
private static ListData parseListMarker(CharSequence line, final int markerIndex, final int markerColumn,
                                        final boolean inParagraph) {
    CharSequence rest = line.subSequence(markerIndex, line.length());
    Matcher matcher = MARKER.matcher(rest);
    if (!matcher.find()) {
        return null;
    }

    ListBlock listBlock = createListBlock(matcher);

    int markerLength = matcher.end() - matcher.start();
    int indexAfterMarker = markerIndex + markerLength;
    // marker doesn't include tabs, so counting them as columns directly is ok
    int columnAfterMarker = markerColumn + markerLength;
    // the column within the line where the content starts
    int contentColumn = columnAfterMarker;

    // See at which column the content starts if there is content
    boolean hasContent = false;
    for (int i = indexAfterMarker; i < line.length(); i++) {
        char c = line.charAt(i);
        if (c == '\t') {
            contentColumn += Parsing.columnsToNextTabStop(contentColumn);
        } else if (c == ' ') {
            contentColumn++;
        } else {
            hasContent = true;
            break;
        }
    }

    if (inParagraph) {
        // If the list item is ordered, the start number must be 1 to interrupt a paragraph.
        if (listBlock instanceof OrderedList && ((OrderedList) listBlock).getStartNumber() != 1) {
            return null;
        }
        // Empty list item can not interrupt a paragraph.
        if (!hasContent) {
            return null;
        }
    }

    if (!hasContent || (contentColumn - columnAfterMarker) > Parsing.CODE_BLOCK_INDENT) {
        // If this line is blank or has a code block, default to 1 space after marker
        contentColumn = columnAfterMarker + 1;
    }

    return new ListData(listBlock, contentColumn);
}