codekingpro/portable-devtools
114k
1"""Join raw text tokens with the rest of the text2 3This is set as a separate rule to provide an opportunity for plugins4to run text replacements after text join, but before escape join.5 6For example, `\\:)` shouldn't be replaced with an emoji.7"""8from __future__ import annotations9 10from ..token import Token11from .state_core import StateCore12 13 14def text_join(state: StateCore) -> None:15 """Join raw text for escape sequences (`text_special`) tokens with the rest of the text"""16 17 for inline_token in state.tokens[:]:18 if inline_token.type != "inline":19 continue20 21 # convert text_special to text and join all adjacent text nodes22 new_tokens: list[Token] = []23 for child_token in inline_token.children or []:24 if child_token.type == "text_special":25 child_token.type = "text"26 if (27 child_token.type == "text"28 and new_tokens29 and new_tokens[-1].type == "text"30 ):31 new_tokens[-1].content += child_token.content32 else:33 new_tokens.append(child_token)34 inline_token.children = new_tokens35 