def test_empty_string_with_good_index():
    """
    Make sure that an empty string is handled properly with a good index
    """

    # Arrange
    input_string = ""
    start_index = 0
    character_to_match = " "
    expected_output = (0, 0)

    # Act
    actual_output = ParserHelper.collect_backwards_while_character(
        input_string, start_index, character_to_match)

    # Assert
    assert expected_output == actual_output
def test_simple_case_from_start_without_whitespace():
    """
    Make sure that we test a simple extraction from the start of the string without whitespace.
    """

    # Arrange
    input_string = "  this is a test"
    start_index = 0
    character_to_match = " "
    expected_output = (0, 0)

    # Act
    actual_output = ParserHelper.collect_backwards_while_character(
        input_string, start_index, character_to_match)

    # Assert
    assert expected_output == actual_output
def test_empty_string_with_bad_left_index():
    """
    Make sure that an empty string is handled properly with an index that is too far to the left.
    """

    # Arrange
    input_string = ""
    start_index = -2
    character_to_match = " "
    expected_output = (None, None)

    # Act
    actual_output = ParserHelper.collect_backwards_while_character(
        input_string, start_index, character_to_match)

    # Assert
    assert expected_output == actual_output
def test_simple_case_from_end_with_eol_index():
    """
    Make sure that we test a simple extraction from the end of the string with whitespace
    """

    # Arrange
    input_string = "this is a test  "
    start_index = -1
    character_to_match = " "
    expected_output = (2, len(input_string) - 2)

    # Act
    actual_output = ParserHelper.collect_backwards_while_character(
        input_string, start_index, character_to_match)

    # Assert
    assert expected_output == actual_output
예제 #5
0
    def __prepare_for_create_atx_heading(
        parser_state: ParserState,
        position_marker: PositionMarker,
        new_tokens: List[MarkdownToken],
        non_whitespace_index: int,
    ) -> Tuple[StackToken, str, int, str, str, List[MarkdownToken]]:
        (
            old_top_of_stack,
            remaining_line,
            remove_trailing_count,
            extracted_whitespace_before_end,
        ) = (
            parser_state.token_stack[-1],
            position_marker.text_to_parse[non_whitespace_index:],
            0,
            "",
        )

        new_tokens, _ = parser_state.close_open_blocks_fn(parser_state)
        (
            end_index,
            extracted_whitespace_at_end,
        ) = ParserHelper.extract_whitespace_from_end(remaining_line)
        while (
            end_index > 0
            and remaining_line[end_index - 1] == LeafBlockProcessor.__atx_character
        ):
            end_index -= 1
            remove_trailing_count += 1
        if remove_trailing_count:
            if end_index > 0:
                if ParserHelper.is_character_at_index(
                    remaining_line, end_index - 1, " "
                ):
                    remaining_line = remaining_line[:end_index]
                    (
                        _,
                        new_non_whitespace_index,
                    ) = ParserHelper.collect_backwards_while_character(
                        remaining_line, len(remaining_line) - 1, " "
                    )
                    assert new_non_whitespace_index is not None
                    end_index = new_non_whitespace_index
                    extracted_whitespace_before_end = remaining_line[end_index:]
                    remaining_line = remaining_line[:end_index]
                else:
                    extracted_whitespace_at_end, remove_trailing_count = "", 0
            else:
                remaining_line = ""
        else:
            extracted_whitespace_at_end = remaining_line[end_index:]
            remaining_line = remaining_line[:end_index]

        return (
            old_top_of_stack,
            remaining_line,
            remove_trailing_count,
            extracted_whitespace_before_end,
            extracted_whitespace_at_end,
            new_tokens,
        )
예제 #6
0
    def handle_line_end(
        remaining_line: str,
        end_string: Optional[str],
        current_string: str,
        inline_blocks: List[MarkdownToken],
        is_setext: bool,
        line_number: int,
        column_number: int,
        coalesced_stack: List[MarkdownToken],
    ) -> Tuple[str, Optional[str], List[MarkdownToken], str, Optional[str],
               str]:
        """
        Handle the inline case of having the end of line character encountered.
        """
        new_tokens: List[MarkdownToken] = []

        POGGER.debug(">>current_string>>$>>", current_string)
        POGGER.debug(">>end_string>>$>>", end_string)
        POGGER.debug(">>remaining_line>>$>>", remaining_line)
        _, last_non_whitespace_index = ParserHelper.collect_backwards_while_character(
            remaining_line, -1, InlineHelper.__line_end_whitespace)
        POGGER.debug(">>last_non_whitespace_index>>$",
                     last_non_whitespace_index)
        removed_end_whitespace = remaining_line[last_non_whitespace_index:]
        remaining_line = remaining_line[:last_non_whitespace_index]
        POGGER.debug(">>removed_end_whitespace>>$>>", removed_end_whitespace)
        POGGER.debug(">>remaining_line>>$>>", remaining_line)

        POGGER.debug(">>current_string>>$>>", current_string)
        (
            append_to_current_string,
            removed_end_whitespace_size,
            adj_hard_column,
        ) = (
            ParserHelper.newline_character,
            len(removed_end_whitespace),
            column_number + len(remaining_line),
        )
        whitespace_to_add: Optional[str] = None
        POGGER.debug(
            ">>len(r_e_w)>>$>>rem>>$>>",
            removed_end_whitespace_size,
            remaining_line,
        )

        is_proper_hard_break = InlineHelper.__is_proper_hard_break(
            current_string, removed_end_whitespace_size)

        (
            current_string,
            whitespace_to_add,
            append_to_current_string,
            end_string,
            remaining_line,
        ) = InlineHelper.__select_line_ending(
            new_tokens,
            is_proper_hard_break,
            line_number,
            adj_hard_column,
            current_string,
            removed_end_whitespace,
            removed_end_whitespace_size,
            whitespace_to_add,
            append_to_current_string,
            end_string,
            remaining_line,
            inline_blocks,
            is_setext,
        )

        if coalesced_stack and coalesced_stack[-1].is_block_quote_start:
            block_quote_token = cast(BlockQuoteMarkdownToken,
                                     coalesced_stack[-1])
            block_quote_token.leading_text_index += 1

        return (
            append_to_current_string,
            whitespace_to_add,
            new_tokens,
            remaining_line,
            end_string,
            current_string,
        )
예제 #7
0
    def handle_line_end(next_index, remaining_line, end_string,
                        current_string):
        """
        Handle the inline case of having the end of line character encountered.
        """
        new_tokens = []

        LOGGER.debug(">>current_string>>%s>>",
                     current_string.replace("\n", "\\n"))
        LOGGER.debug(">>end_string>>%s>>",
                     str(end_string).replace("\n", "\\n"))
        LOGGER.debug(">>remaining_line>>%s>>",
                     str(remaining_line).replace("\n", "\\n"))
        _, last_non_whitespace_index = ParserHelper.collect_backwards_while_character(
            remaining_line, -1, InlineHelper.__line_end_whitespace)
        LOGGER.debug(">>last_non_whitespace_index>>%s",
                     str(last_non_whitespace_index))
        removed_end_whitespace = remaining_line[last_non_whitespace_index:]
        remaining_line = remaining_line[0:last_non_whitespace_index]
        LOGGER.debug(
            ">>removed_end_whitespace>>%s>>",
            str(removed_end_whitespace).replace("\n", "\\n"),
        )
        LOGGER.debug(">>remaining_line>>%s>>",
                     str(remaining_line).replace("\n", "\\n"))

        append_to_current_string = "\n"
        whitespace_to_add = None
        LOGGER.debug(
            ">>len(r_e_w)>>%s>>rem>>%s>>",
            str(len(removed_end_whitespace)),
            remaining_line,
        )
        if (len(removed_end_whitespace) == 0 and len(current_string) >= 1
                and current_string[len(current_string) - 1]
                == InlineHelper.backslash_character):
            new_tokens.append(
                HardBreakMarkdownToken(InlineHelper.backslash_character))
            current_string = current_string[0:-1]
        elif len(removed_end_whitespace) >= 2:
            new_tokens.append(HardBreakMarkdownToken(removed_end_whitespace))
            whitespace_to_add = ""
        else:
            end_string = InlineHelper.modify_end_string(
                end_string, removed_end_whitespace)

        LOGGER.debug(
            "<<append_to_current_string<<%s<<",
            str(append_to_current_string).replace("\n", "\\n"),
        )
        LOGGER.debug("<<whitespace_to_add<<%s<<",
                     str(whitespace_to_add).replace("\n", "\\n"))
        LOGGER.debug("<<remaining_line<<%s<<",
                     str(remaining_line).replace("\n", "\\n"))
        LOGGER.debug("<<end_string<<%s<<",
                     str(end_string).replace("\n", "\\n"))
        LOGGER.debug("<<current_string<<%s<<",
                     str(current_string).replace("\n", "\\n"))
        return (
            append_to_current_string,
            whitespace_to_add,
            next_index + 1,
            new_tokens,
            remaining_line,
            end_string,
            current_string,
        )