def METHOD_NAME(txt: str) -> str:
    output = ""
    cursor = 0
    active_tokens: list[Token] = []
    def _get_active_markup() -> str:
        active_markup = " ".join(tkn.markup for tkn in active_tokens)
        if active_markup == "":
            return ""
        return f"[{active_markup}]"
    for matchobj in RE_MARKUP.finditer(txt):
        start, end = matchobj.span()
        if cursor < start:
            if cursor > 0:
                output += "]"
            output += _get_active_markup()
            output += f"{txt[cursor:start]}[/]"
        *_, tags = matchobj.groups()
        output += "["
        for tag in tags.split():
            token = consume_tag(tag)
            output += f"{token.prettified_markup} "
            if Token.is_clear(token):
                active_tokens = [
                    tkn for tkn in active_tokens if not token.targets(tkn)
                ]
            else:
                active_tokens.append(token)
        output = output.rstrip()
        cursor = end
    if cursor < len(txt) - 1:
        if cursor > 0:
            output += "]"
        output += _get_active_markup()
        output += f"{txt[cursor:]}"
        if len(active_tokens) > 0:
            output += "[/]"
    if output.count("[") != output.count("]"):
        output += "]"
    return output