org.commonmark.parser.block.ParserState Java Examples

The following examples show how to use org.commonmark.parser.block.ParserState. 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: ListItemParser.java    From 1Rramp-Android with MIT License 6 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState state) {
    if (state.isBlank()) {
        if (block.getFirstChild() == null) {
            // Blank line after empty list item
            return BlockContinue.none();
        } else {
            return BlockContinue.atIndex(state.getNextNonSpaceIndex());
        }
    }

    if (state.getIndent() >= contentIndent) {
        return BlockContinue.atColumn(state.getColumn() + contentIndent);
    } else {
        return BlockContinue.none();
    }
}
 
Example #2
Source File: TableBlockParser.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {
  CharSequence line = state.getLine();
  CharSequence previousLine = matchedBlockParser.getParagraphContent();
  if (previousLine != null && previousLine.toString().contains("|")) {
    line = line.subSequence(state.getIndex(), line.length());
    ImmutableList<Alignment> columnAlignments = parseHeaderDivider(line);
    if (!columnAlignments.isEmpty()) {
      return BlockStart.of(new TableBlockParser(previousLine, columnAlignments))
          .atIndex(state.getIndex())
          .replaceActiveBlockParser();
    }
  }

  return BlockStart.none();
}
 
Example #3
Source File: ListItemParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState state) {
    if (state.isBlank()) {
        if (block.getFirstChild() == null) {
            // Blank line after empty list item
            return BlockContinue.none();
        } else {
            Block activeBlock = state.getActiveBlockParser().getBlock();
            // If the active block is a code block, blank lines in it should not affect if the list is tight.
            hadBlankLine = activeBlock instanceof Paragraph || activeBlock instanceof ListItem;
            return BlockContinue.atIndex(state.getNextNonSpaceIndex());
        }
    }

    if (state.getIndent() >= contentIndent) {
        return BlockContinue.atColumn(state.getColumn() + contentIndent);
    } else {
        return BlockContinue.none();
    }
}
 
Example #4
Source File: JLatexMathBlockParserTest.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@NonNull
private static ParserState createState(@NonNull String line) {

    final ParserState state = mock(ParserState.class);

    int i = 0;
    for (int length = line.length(); i < length; i++) {
        if (' ' != line.charAt(i)) {
            // previous is the last space
            break;
        }
    }

    when(state.getIndent()).thenReturn(i);
    when(state.getNextNonSpaceIndex()).thenReturn(i);
    when(state.getLine()).thenReturn(line);

    return state;
}
 
Example #5
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 #6
Source File: JLatexMathBlockParserLegacy.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
public BlockStart tryStart(ParserState state, MatchedBlockParser matchedBlockParser) {

    final CharSequence line = state.getLine();
    final int length = line != null
            ? line.length()
            : 0;

    if (length > 1) {
        if ('$' == line.charAt(0)
                && '$' == line.charAt(1)) {
            return BlockStart.of(new JLatexMathBlockParserLegacy())
                    .atIndex(state.getIndex() + 2);
        }
    }

    return BlockStart.none();
}
 
Example #7
Source File: TaskListBlockParser.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState parserState) {

    final BlockContinue blockContinue;

    final String line = line(parserState);

    final int currentIndent = parserState.getIndent();
    if (currentIndent > indent) {
        indent += 2;
    } else if (currentIndent < indent && indent > 1) {
        indent -= 2;
    }

    if (line != null
            && line.length() > 0
            && PATTERN.matcher(line).matches()) {
        blockContinue = BlockContinue.atIndex(parserState.getIndex());
    } else {
        // @since 2.0.0, previously called `BlockContinue.finished()`
        //  that was swallowing non-matching lines
        blockContinue = BlockContinue.none();
    }

    return blockContinue;
}
 
Example #8
Source File: JLatexMathBlockParserLegacy.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState parserState) {

    if (isClosed) {
        return BlockContinue.finished();
    }

    return BlockContinue.atIndex(parserState.getIndex());
}
 
Example #9
Source File: TaskListBlockParser.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Nullable
private static String line(@NonNull ParserState state) {
    final CharSequence lineRaw = state.getLine();
    return lineRaw != null
            ? lineRaw.toString()
            : null;
}
 
Example #10
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 #11
Source File: JLatexMathBlockParserTest.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Test
public void factory_indentBlock() {
    // when state indent is greater than block -> nono

    final ParserState state = mock(ParserState.class);
    when(state.getIndent()).thenReturn(Parsing.CODE_BLOCK_INDENT);

    // hm, interesting, `BlockStart.none()` actually returns null
    final BlockStart start = factory.tryStart(state, null);
    assertNull(start);
}
 
Example #12
Source File: JLatexMathBlockParserTest.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Test
public void factory_noMatch() {

    for (String line : NO_MATCH) {
        final ParserState state = createState(line);

        assertNull(factory.tryStart(state, null));
    }
}
 
Example #13
Source File: JLatexMathBlockParserTest.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Test
public void factory_match() {

    for (String line : MATCH) {
        final ParserState state = createState(line);

        final BlockStart start = factory.tryStart(state, null);
        assertNotNull(start);

        // hm...
        final BlockStartImpl impl = (BlockStartImpl) start;
        assertEquals(quote(line), line.length() + 1, impl.getNewIndex());
    }
}
 
Example #14
Source File: JLatexMathBlockParserTest.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Test
public void finish_noMatch() {
    for (String line : NO_MATCH) {
        final ParserState state = createState(line);
        // doesn't matter
        final int count = 2;
        final JLatexMathBlockParser parser = new JLatexMathBlockParser(count);
        final BlockContinueImpl impl = (BlockContinueImpl) parser.tryContinue(state);
        assertFalse(quote(line), impl.isFinalize());
    }
}
 
Example #15
Source File: ParagraphParser.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState state) {
    if (!state.isBlank()) {
        return BlockContinue.atIndex(state.getIndex());
    } else {
        return BlockContinue.none();
    }
}
 
Example #16
Source File: ParagraphParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState state) {
    if (!state.isBlank()) {
        return BlockContinue.atIndex(state.getIndex());
    } else {
        return BlockContinue.none();
    }
}
 
Example #17
Source File: TableBlockParser.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState state) {
  if (state.getLine().toString().contains("|")
      || CAPTION_LINE.matcher(state.getLine()).matches()) {
    return BlockContinue.atIndex(state.getIndex());
  } else {
    return BlockContinue.none();
  }
}
 
Example #18
Source File: DocumentBlockParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState state) {
    return BlockContinue.atIndex(state.getIndex());
}
 
Example #19
Source File: DocumentBlockParser.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState state) {
    return BlockContinue.atIndex(state.getIndex());
}