Esempio n. 1
0
    def append_text(
        string_to_append_to: str,
        text_to_append: str,
        alternate_escape_map: Optional[Dict[str, str]] = None,
        add_text_signature: bool = True,
    ) -> str:
        """
        Append the text to the given string, doing any needed encoding as we go.
        """

        if not alternate_escape_map:
            alternate_escape_map = InlineHelper.__html_character_escape_map
        key_map = "".join(alternate_escape_map.keys())
        start_index, text_parts = 0, [string_to_append_to]
        next_index = ParserHelper.index_any_of(text_to_append, key_map,
                                               start_index)
        while next_index != -1:
            escaped_part = alternate_escape_map[text_to_append[next_index]]
            text_parts.extend([
                text_to_append[start_index:next_index],
                ParserHelper.create_replacement_markers(
                    text_to_append[next_index], escaped_part)
                if add_text_signature else escaped_part,
            ])

            start_index = next_index + 1
            next_index = ParserHelper.index_any_of(text_to_append, key_map,
                                                   start_index)

        if start_index < len(text_to_append):
            text_parts.append(text_to_append[start_index:])

        return "".join(text_parts)
Esempio n. 2
0
    def handle_backslashes(source_text, add_text_signature=True):
        """
        Handle the processing of backslashes for anything other than the text
        blocks, which have additional needs for parsing.
        """

        start_index = 0
        current_string = ""
        next_index = ParserHelper.index_any_of(
            source_text, InlineHelper.__valid_backslash_sequence_starts,
            start_index)
        while next_index != -1:
            current_string = current_string + source_text[
                start_index:next_index]
            current_char = source_text[next_index]

            inline_request = InlineRequest(source_text, next_index)
            LOGGER.debug("handle_backslashes>>%s>>", current_char)
            if current_char == InlineHelper.backslash_character:
                inline_response = InlineHelper.handle_inline_backslash(
                    inline_request, add_text_signature=add_text_signature)
                new_string = inline_response.new_string
                new_index = inline_response.new_index
            else:
                assert (source_text[next_index] ==
                        InlineHelper.character_reference_start_character)
                inline_response = InlineHelper.handle_character_reference(
                    inline_request)
                new_string = inline_response.new_string
                new_index = inline_response.new_index

            LOGGER.debug("handle_backslashes<<%s<<%s", new_string,
                         str(new_index))
            current_string = current_string + new_string
            start_index = new_index
            next_index = ParserHelper.index_any_of(
                source_text, InlineHelper.__valid_backslash_sequence_starts,
                start_index)

        if start_index < len(source_text):
            current_string = current_string + source_text[start_index:]
        return current_string
Esempio n. 3
0
    def handle_backslashes(source_text: str) -> str:
        """
        Handle the processing of backslashes for anything other than the text
        blocks, which have additional needs for parsing.
        """

        start_index, string_parts = 0, []
        next_index = ParserHelper.index_any_of(
            source_text, InlineHelper.__valid_backslash_sequence_starts,
            start_index)
        while next_index != -1:
            string_parts.append(source_text[start_index:next_index])
            current_char = source_text[next_index]

            inline_request = InlineRequest(source_text, next_index)
            POGGER.debug("handle_backslashes>>$>>", current_char)
            if current_char == InlineHelper.backslash_character:
                inline_response = InlineHelper.handle_inline_backslash(
                    inline_request, add_text_signature=False)
            else:
                assert (source_text[next_index] ==
                        InlineHelper.character_reference_start_character)
                inline_response = InlineHelper.handle_character_reference(
                    inline_request)
            new_string, new_index = (
                inline_response.new_string,
                inline_response.new_index,
            )
            assert new_string is not None
            assert new_index is not None
            POGGER.debug("handle_backslashes<<$<<$", new_string, new_index)
            string_parts.append(new_string)
            start_index = new_index
            next_index = ParserHelper.index_any_of(
                source_text, InlineHelper.__valid_backslash_sequence_starts,
                start_index)

        if start_index < len(source_text):
            string_parts.append(source_text[start_index:])
        return "".join(string_parts)
Esempio n. 4
0
    def append_text(
        string_to_append_to,
        text_to_append,
        alternate_escape_map=None,
        add_text_signature=True,
    ):
        """
        Append the text to the given string, doing any needed encoding as we go.
        """

        if not alternate_escape_map:
            alternate_escape_map = InlineHelper.__html_character_escape_map

        start_index = 0
        next_index = ParserHelper.index_any_of(text_to_append,
                                               alternate_escape_map.keys(),
                                               start_index)
        while next_index != -1:

            string_part = text_to_append[start_index:next_index]
            if add_text_signature:
                string_part += (InlineHelper.__alert_character +
                                text_to_append[next_index] +
                                InlineHelper.__alert_character)
            string_part += alternate_escape_map[text_to_append[next_index]]
            if add_text_signature:
                string_part += InlineHelper.__alert_character

            string_to_append_to += string_part

            start_index = next_index + 1
            next_index = ParserHelper.index_any_of(text_to_append,
                                                   alternate_escape_map.keys(),
                                                   start_index)

        if start_index < len(text_to_append):
            string_to_append_to = string_to_append_to + text_to_append[
                start_index:]

        return string_to_append_to
Esempio n. 5
0
    def __complete_inline_loop(
        source_text,
        new_index,
        end_string,
        whitespace_to_add,
        current_string,
        current_string_unresolved,
        new_string_unresolved,
        new_string,
        original_string,
    ):
        LOGGER.debug(
            "__complete_inline_loop--current_string>>%s>>",
            current_string.replace("\n", "\\n"),
        )
        LOGGER.debug("__complete_inline_loop--new_string>>%s>>",
                     new_string.replace("\n", "\\n"))
        LOGGER.debug(
            "__complete_inline_loop--new_string_unresolved>>%s>>",
            str(new_string_unresolved).replace("\n", "\\n"),
        )
        LOGGER.debug(
            "__complete_inline_loop--original_string>>%s>>",
            str(original_string).replace("\b", "\\b").replace("\a",
                                                              "\\a").replace(
                                                                  "\n", "\\n"),
        )

        if original_string is not None:
            assert not new_string_unresolved or new_string_unresolved == original_string
            current_string += "\a" + original_string + "\a"

        LOGGER.debug(
            "__complete_inline_loop--current_string>>%s>>",
            str(current_string).replace("\b", "\\b").replace("\a",
                                                             "\\a").replace(
                                                                 "\n", "\\n"),
        )
        current_string = InlineHelper.append_text(current_string, new_string)
        LOGGER.debug(
            "__complete_inline_loop--current_string>>%s>>",
            str(current_string).replace("\b", "\\b").replace("\a",
                                                             "\\a").replace(
                                                                 "\n", "\\n"),
        )

        if original_string is not None:
            current_string += "\a"

        LOGGER.debug(
            "__complete_inline_loop--current_string>>%s>>",
            str(current_string).replace("\b", "\\b").replace("\a",
                                                             "\\a").replace(
                                                                 "\n", "\\n"),
        )
        if new_string_unresolved:
            current_string_unresolved += new_string_unresolved
        else:
            current_string_unresolved = InlineHelper.append_text(
                current_string_unresolved, new_string)

        LOGGER.debug(
            "__complete_inline_loop--current_string_unresolved>>%s>>",
            str(current_string_unresolved).replace("\b", "\\b").replace(
                "\a", "\\a").replace("\n", "\\n"),
        )

        if whitespace_to_add is not None:
            end_string = InlineHelper.modify_end_string(
                end_string, whitespace_to_add)

        start_index = new_index
        next_index = ParserHelper.index_any_of(
            source_text,
            InlineProcessor.__valid_inline_text_block_sequence_starts,
            start_index,
        )
        return (
            start_index,
            next_index,
            end_string,
            current_string,
            current_string_unresolved,
        )
Esempio n. 6
0
    def __process_inline_text_block(
        source_text,
        starting_whitespace="",
        whitespace_to_recombine=None,
        is_setext=False,
    ):
        """
        Process a text block for any inline items.
        """

        inline_blocks = []
        start_index = 0
        if whitespace_to_recombine and " " in whitespace_to_recombine:
            source_text = InlineProcessor.__recombine_with_whitespace(
                source_text, whitespace_to_recombine)
        else:
            whitespace_to_recombine = None

        current_string = ""
        current_string_unresolved = ""
        end_string = ""

        inline_response = InlineResponse()

        next_index = ParserHelper.index_any_of(
            source_text,
            InlineProcessor.__valid_inline_text_block_sequence_starts,
            start_index,
        )
        LOGGER.debug("__process_inline_text_block>>is_setext>>%s",
                     str(is_setext))
        LOGGER.debug(
            "__process_inline_text_block>>%s>>%s",
            source_text.replace("\n", "\\n"),
            str(start_index),
        )
        while next_index != -1:

            inline_response.clear_fields()
            reset_current_string = False
            whitespace_to_add = None

            LOGGER.debug("__process_inline_text_block>>%s>>%s",
                         str(start_index), str(next_index))
            remaining_line = source_text[start_index:next_index]

            inline_request = InlineRequest(
                source_text,
                next_index,
                inline_blocks,
                remaining_line,
                current_string_unresolved,
            )
            if source_text[
                    next_index] in InlineProcessor.__inline_character_handlers:
                LOGGER.debug("handler(before)>>%s<<", source_text[next_index])
                proc_fn = InlineProcessor.__inline_character_handlers[
                    source_text[next_index]]
                inline_response = proc_fn(inline_request)
                LOGGER.debug("handler(after)>>%s<<", source_text[next_index])
            else:
                assert source_text[next_index] == "\n"
                LOGGER.debug(
                    "end_string(before)>>%s<<",
                    str(end_string).replace("\n",
                                            "\\n").replace("\x02", "\\x02"),
                )
                (
                    inline_response.new_string,
                    whitespace_to_add,
                    inline_response.new_index,
                    inline_response.new_tokens,
                    remaining_line,
                    end_string,
                    current_string,
                ) = InlineHelper.handle_line_end(next_index, remaining_line,
                                                 end_string, current_string)
                LOGGER.debug(
                    "handle_line_end>>new_tokens>>%s<<",
                    str(inline_response.new_tokens).replace(
                        "\n", "\\n").replace("\x02", "\\x02"),
                )
                if not inline_response.new_tokens:
                    end_string = InlineProcessor.__add_recombined_whitespace(
                        bool(whitespace_to_recombine),
                        source_text,
                        inline_response,
                        end_string,
                        is_setext,
                    )
                LOGGER.debug(
                    "handle_line_end>>%s<<",
                    source_text[inline_response.new_index:].replace(
                        "\n", "\\n").replace("\x02", "\\x02"),
                )
                LOGGER.debug(
                    "end_string(after)>>%s<<",
                    str(end_string).replace("\n",
                                            "\\n").replace("\x02", "\\x02"),
                )

            LOGGER.debug(
                "new_string-->%s<--",
                str(inline_response.new_string).replace("\n", "\\n"),
            )
            LOGGER.debug("new_index-->%s<--", str(inline_response.new_index))
            LOGGER.debug(
                "new_tokens-->%s<--",
                str(inline_response.new_tokens).replace("\n", "\\n"),
            )
            LOGGER.debug(
                "new_string_unresolved-->%s<--",
                str(inline_response.new_string_unresolved).replace(
                    "\n", "\\n"),
            )
            LOGGER.debug(
                "consume_rest_of_line-->%s<--",
                str(inline_response.consume_rest_of_line),
            )
            LOGGER.debug(
                "original_string-->%s<--",
                str(inline_response.original_string).replace("\n", "\\n"),
            )

            if inline_response.consume_rest_of_line:
                inline_response.new_string = ""
                reset_current_string = True
                inline_response.new_tokens = None
            else:
                current_string = InlineHelper.append_text(
                    current_string, remaining_line)
                current_string_unresolved = InlineHelper.append_text(
                    current_string_unresolved, remaining_line)

            LOGGER.debug(
                "current_string>>%s<<",
                str(current_string).replace("\n",
                                            "\\n").replace("\x02", "\\x02"),
            )
            LOGGER.debug(
                "current_string_unresolved>>%s<<",
                str(current_string_unresolved).replace("\n", "\\n").replace(
                    "\x02", "\\x02"),
            )
            if inline_response.new_tokens:
                if current_string:
                    # assert end_string is None
                    inline_blocks.append(
                        TextMarkdownToken(
                            current_string,
                            starting_whitespace,
                            end_whitespace=end_string,
                        ))
                    reset_current_string = True
                    starting_whitespace = ""
                    end_string = None

                inline_blocks.extend(inline_response.new_tokens)

            if reset_current_string:
                current_string = ""
                current_string_unresolved = ""

            (
                start_index,
                next_index,
                end_string,
                current_string,
                current_string_unresolved,
            ) = InlineProcessor.__complete_inline_loop(
                source_text,
                inline_response.new_index,
                end_string,
                whitespace_to_add,
                current_string,
                current_string_unresolved,
                inline_response.new_string_unresolved,
                inline_response.new_string,
                inline_response.original_string,
            )
            LOGGER.debug(
                "<<current_string<<%s<<%s<<",
                str(len(current_string)),
                current_string.replace("\b", "\\b").replace("\a",
                                                            "\\a").replace(
                                                                "\n", "\\n"),
            )
            LOGGER.debug(
                "<<current_string_unresolved<<%s<<%s<<",
                str(len(current_string_unresolved)),
                current_string_unresolved.replace("\b", "\\b").replace(
                    "\a", "\\a").replace("\n", "\\n"),
            )

        LOGGER.debug("<<__complete_inline_block_processing<<")
        return InlineProcessor.__complete_inline_block_processing(
            inline_blocks,
            source_text,
            start_index,
            current_string,
            end_string,
            starting_whitespace,
            is_setext,
        )
Esempio n. 7
0
def bob1():
    search_string = "1234567890z"
    search_for = "abcdefghijklmnopqrstuvwxyz"
    ParserHelper.index_any_of(search_string, search_for)