Beispiel #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)
Beispiel #2
0
    def __calculate_backtick_between_text(
            inline_request: InlineRequest, new_index: int,
            end_backtick_start_index: int) -> Tuple[str, str, str, str]:
        between_text = inline_request.source_text[
            new_index:end_backtick_start_index]
        original_between_text, leading_whitespace, trailing_whitespace = (
            between_text,
            "",
            "",
        )
        POGGER.debug(
            "after_collect>$>>$>>$<<",
            between_text,
            end_backtick_start_index,
            inline_request.source_text[end_backtick_start_index:],
        )
        if (len(between_text) > 2 and between_text[0] in [
                ParserHelper.space_character, ParserHelper.newline_character
        ] and between_text[-1] in [
                ParserHelper.space_character, ParserHelper.newline_character
        ]):
            stripped_between_attempt = between_text[1:-1]
            if len(stripped_between_attempt.strip()) != 0:
                leading_whitespace, trailing_whitespace = (
                    between_text[0],
                    between_text[-1],
                )
                between_text = stripped_between_attempt

        replaced_newline = ParserHelper.create_replacement_markers(
            ParserHelper.newline_character, ParserHelper.space_character)
        POGGER.debug("between_text>>$<<", between_text)
        between_text = ParserHelper.escape_special_characters(between_text)
        POGGER.debug("between_text>>$<<", between_text)
        POGGER.debug(
            "leading_whitespace>>$<<",
            leading_whitespace,
        )
        POGGER.debug(
            "trailing_whitespace>>$<<",
            trailing_whitespace,
        )
        between_text, leading_whitespace, trailing_whitespace = (
            between_text.replace(ParserHelper.newline_character,
                                 replaced_newline),
            leading_whitespace.replace(ParserHelper.newline_character,
                                       replaced_newline),
            trailing_whitespace.replace(ParserHelper.newline_character,
                                        replaced_newline),
        )
        return (
            between_text,
            original_between_text,
            leading_whitespace,
            trailing_whitespace,
        )