Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/markdown_it/rules_inline/fragments_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

19 statements  

1from .state_inline import StateInline 

2 

3 

4def fragments_join(state: StateInline) -> None: 

5 """ 

6 Clean up tokens after emphasis and strikethrough postprocessing: 

7 merge adjacent text nodes into one and re-calculate all token levels 

8 

9 This is necessary because initially emphasis delimiter markers (``*, _, ~``) 

10 are treated as their own separate text tokens. Then emphasis rule either 

11 leaves them as text (needed to merge with adjacent text) or turns them 

12 into opening/closing tags (which messes up levels inside). 

13 """ 

14 level = 0 

15 maximum = len(state.tokens) 

16 

17 curr = last = 0 

18 while curr < maximum: 

19 # re-calculate levels after emphasis/strikethrough turns some text nodes 

20 # into opening/closing tags 

21 if state.tokens[curr].nesting < 0: 

22 level -= 1 # closing tag 

23 state.tokens[curr].level = level 

24 if state.tokens[curr].nesting > 0: 

25 level += 1 # opening tag 

26 

27 if ( 

28 state.tokens[curr].type == "text" 

29 and curr + 1 < maximum 

30 and state.tokens[curr + 1].type == "text" 

31 ): 

32 # collapse two adjacent text nodes 

33 state.tokens[curr + 1].content = ( 

34 state.tokens[curr].content + state.tokens[curr + 1].content 

35 ) 

36 else: 

37 if curr != last: 

38 state.tokens[last] = state.tokens[curr] 

39 last += 1 

40 curr += 1 

41 

42 if curr != last: 

43 del state.tokens[last:]