Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/markdown_it/rules_core/text_join.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

35 statements  

1"""Join raw text tokens with the rest of the text 

2 

3This is set as a separate rule to provide an opportunity for plugins 

4to run text replacements after text join, but before escape join. 

5 

6For example, `\\:)` shouldn't be replaced with an emoji. 

7""" 

8 

9from __future__ import annotations 

10 

11from ..token import Token 

12from .state_core import StateCore 

13 

14 

15def text_join(state: StateCore) -> None: 

16 """Join raw text for escape sequences (`text_special`) tokens with the rest of the text""" 

17 

18 for inline_token in state.tokens[:]: 

19 if inline_token.type != "inline": 

20 continue 

21 

22 children = inline_token.children or [] 

23 for child_token in children: 

24 # image `alt` is parsed into its own token tree 

25 if child_token.children: 

26 child_token.children = _join_text_tokens(child_token.children) 

27 inline_token.children = _join_text_tokens(children) 

28 

29 

30def _join_text_tokens(tokens: list[Token]) -> list[Token]: 

31 """Convert `text_special` to `text` and join all adjacent text tokens""" 

32 new_tokens: list[Token] = [] 

33 i = 0 

34 while i < len(tokens): 

35 token = tokens[i] 

36 if token.type == "text_special": 

37 token.type = "text" 

38 if token.type == "text" and new_tokens and new_tokens[-1].type == "text": 

39 # Collapse a run of adjacent text nodes in a single join, instead 

40 # of pairwise `a + b` concatenation. The pairwise form is O(L*k) 

41 # in the size of the run because each step rebuilds the growing 

42 # prefix; "".join is O(L). 

43 parts = [new_tokens[-1].content, token.content] 

44 i += 1 

45 while i < len(tokens): 

46 next_token = tokens[i] 

47 if next_token.type == "text_special": 

48 next_token.type = "text" 

49 if next_token.type != "text": 

50 break 

51 parts.append(next_token.content) 

52 i += 1 

53 new_tokens[-1].content = "".join(parts) 

54 else: 

55 new_tokens.append(token) 

56 i += 1 

57 return new_tokens