def plain_mtext(text: str, split=False) -> Union[List[str], str]: chars = [] # split text into chars, in reversed order for efficient pop() raw_chars = list(reversed(text)) while len(raw_chars): char = raw_chars.pop() if char == '\\': # is a formatting command try: char = raw_chars.pop() except IndexError: break # premature end of text - just ignore if char in '\\{}': chars.append(char) elif char in ONE_CHAR_COMMANDS: if char == 'P': # new line chars.append('\n') elif char == 'N': # new column chars.append( ' ' ) # until columns are supported, better to at least remove the escape character else: pass # discard other commands else: # more character commands are terminated by ';' stacking = char == 'S' # stacking command surrounds user data first_char = char search_chars = raw_chars.copy() try: while char != ';': # end of format marker char = search_chars.pop() if stacking and char != ';': # append user data of stacking command chars.append(char) raw_chars = search_chars except IndexError: # premature end of text - just ignore chars.append('\\') chars.append(first_char) elif char in '{}': # grouping pass # discard group markers elif char == '%': # special characters if len(raw_chars) and raw_chars[-1] == '%': raw_chars.pop() # discard next '%' if len(raw_chars): special_char = raw_chars.pop() # replace or discard formatting code chars.append(SPECIAL_CHARS_ENCODING.get(special_char, "")) else: # char is just a single '%' chars.append(char) else: # char is what it is, a character chars.append(char) result = "".join(chars) return result.split('\n') if split else result
def plain_text(text: str) -> str: chars = [] raw_chars = list(reversed(validator.fix_one_line_text(caret_decode(text)))) while len(raw_chars): char = raw_chars.pop() if char == '%': # special characters if len(raw_chars) and raw_chars[-1] == '%': raw_chars.pop() # discard next '%' if len(raw_chars): special_char = raw_chars.pop() # replace or discard formatting code chars.append(SPECIAL_CHARS_ENCODING.get(special_char, '')) else: # char is just a single '%' chars.append(char) else: # char is what it is, a character chars.append(char) return "".join(chars)