Example #1
0
    def __process_inline_link_body(source_text, new_index):
        """
        Given that an inline link has been identified, process it's body.
        """

        LOGGER.debug("process_inline_link_body>>%s<<", source_text[new_index:])
        inline_link = ""
        pre_inline_link = ""
        inline_title = ""
        pre_inline_title = ""
        new_index, _ = ParserHelper.extract_any_whitespace(
            source_text, new_index)
        LOGGER.debug("new_index>>%s>>source_text[]>>%s>", str(new_index),
                     source_text[new_index:])
        if not ParserHelper.is_character_at_index(
                source_text, new_index, LinkHelper.__link_format_inline_end):
            (
                inline_link,
                pre_inline_link,
                new_index,
                _,
            ) = LinkHelper.__parse_link_destination(source_text, new_index)
            if new_index != -1:
                LOGGER.debug("before ws>>%s<", source_text[new_index:])
                new_index, _ = ParserHelper.extract_any_whitespace(
                    source_text, new_index)
                LOGGER.debug("after ws>>%s>", source_text[new_index:])
                if ParserHelper.is_character_at_index_not(
                        source_text, new_index,
                        LinkHelper.__link_format_inline_end):
                    (
                        inline_title,
                        pre_inline_title,
                        new_index,
                    ) = LinkHelper.__parse_link_title(source_text, new_index)
                if new_index != -1:
                    new_index, _ = ParserHelper.extract_any_whitespace(
                        source_text, new_index)
        LOGGER.debug(
            "inline_link>>%s>>inline_title>>%s>new_index>%s>",
            str(inline_link),
            str(inline_title),
            str(new_index),
        )
        if new_index != -1:
            if ParserHelper.is_character_at_index(
                    source_text, new_index,
                    LinkHelper.__link_format_inline_end):
                new_index += 1
            else:
                new_index = -1
        LOGGER.debug(
            "process_inline_link_body>>inline_link>>%s>>inline_title>>%s>new_index>%s>",
            str(inline_link),
            str(inline_title),
            str(new_index),
        )
        return inline_link, pre_inline_link, inline_title, pre_inline_title, new_index
def test_is_character_at_index_not_without_whitespace():
    """
    Make sure that a string without any characters at the index is handled properly.
    """

    # Arrange
    input_string = "this is a test"
    start_index = 0
    valid_character = "b"
    expected_output = True

    # Act
    actual_output = ParserHelper.is_character_at_index_not(
        input_string, start_index, valid_character)

    # Assert
    assert expected_output == actual_output
def test_is_character_at_index_not_with_character_at_end():
    """
    Make sure that a string with one of the characters at the index is handled properly.
    """

    # Arrange
    input_string = "this is a test!"
    start_index = len(input_string) - 1
    valid_character = "!"
    expected_output = False

    # Act
    actual_output = ParserHelper.is_character_at_index_not(
        input_string, start_index, valid_character)

    # Assert
    assert expected_output == actual_output
def test_is_character_at_index_not_with_whitespace():
    """
    Make sure that a string with one of the characters present at the index is handled properly.
    """

    # Arrange
    input_string = "a"
    start_index = 0
    valid_character = "a"
    expected_output = False

    # Act
    actual_output = ParserHelper.is_character_at_index_not(
        input_string, start_index, valid_character)

    # Assert
    assert expected_output == actual_output
def test_is_character_at_index_not_with_high_index():
    """
    Make sure that a string with a high index is handled properly.
    """

    # Arrange
    input_string = "this is a test"
    start_index = len(input_string)
    valid_character = "a"
    expected_output = False

    # Act
    actual_output = ParserHelper.is_character_at_index_not(
        input_string, start_index, valid_character)

    # Assert
    assert expected_output == actual_output
def test_is_character_at_index_not_with_empty_string():
    """
    Make sure that an empty string is handled properly.
    """

    # Arrange
    input_string = ""
    start_index = 0
    valid_character = "a"
    expected_output = False

    # Act
    actual_output = ParserHelper.is_character_at_index_not(
        input_string, start_index, valid_character)

    # Assert
    assert expected_output == actual_output
    def __count_block_quote_starts(
        line_to_parse,
        start_index,
        stack_bq_count,
        is_top_of_stack_fenced_code_block,
    ):
        """
        Having detected a block quote character (">") on a line, continue to consume
        and count while the block quote pattern is there.
        """

        this_bq_count = 0
        last_block_quote_index = -1
        adjusted_line = line_to_parse
        if stack_bq_count == 0 and is_top_of_stack_fenced_code_block:
            start_index -= 1
        else:
            this_bq_count += 1
            start_index += 1
            last_block_quote_index = start_index

            LOGGER.debug(
                "stack_bq_count--%s--is_top_of_stack_fenced_code_block--%s",
                str(stack_bq_count),
                str(is_top_of_stack_fenced_code_block),
            )

            while True:
                if ParserHelper.is_character_at_index_whitespace(
                        adjusted_line, start_index):
                    if adjusted_line[start_index] == "\t":
                        adjusted_tab_length = ParserHelper.calculate_length(
                            "\t", start_index=start_index)
                        LOGGER.debug("adj--%s--",
                                     adjusted_line.replace("\t", "\\t"))
                        adjusted_line = (adjusted_line[0:start_index] +
                                         "".rjust(adjusted_tab_length) +
                                         adjusted_line[start_index + 1:])
                        LOGGER.debug("--%s--",
                                     adjusted_line.replace("\t", "\\t"))
                    start_index += 1

                if is_top_of_stack_fenced_code_block and (this_bq_count >=
                                                          stack_bq_count):
                    break

                if start_index == len(
                        adjusted_line
                ) or ParserHelper.is_character_at_index_not(
                        adjusted_line,
                        start_index,
                        BlockQuoteProcessor.__block_quote_character,
                ):
                    break
                this_bq_count += 1
                start_index += 1
                last_block_quote_index = start_index

            LOGGER.debug(
                "__count_block_quote_starts--%s--%s--",
                str(start_index),
                adjusted_line.replace("\t", "\\t"),
            )
        return this_bq_count, start_index, adjusted_line, last_block_quote_index