Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/markdown_it/rules_inline/emphasis.py: 100%
50 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:07 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:07 +0000
1# Process *this* and _that_
2#
4from .state_inline import Delimiter, StateInline
7def tokenize(state: StateInline, silent: bool):
8 """Insert each marker as a separate text token, and add it to delimiter list"""
9 start = state.pos
10 marker = state.srcCharCode[start]
12 if silent:
13 return False
15 # /* _ */ /* * */
16 if marker != 0x5F and marker != 0x2A:
17 return False
19 scanned = state.scanDelims(state.pos, marker == 0x2A)
21 for i in range(scanned.length):
22 token = state.push("text", "", 0)
23 token.content = chr(marker)
24 state.delimiters.append(
25 Delimiter(
26 marker=marker,
27 length=scanned.length,
28 jump=i,
29 token=len(state.tokens) - 1,
30 end=-1,
31 open=scanned.can_open,
32 close=scanned.can_close,
33 )
34 )
36 state.pos += scanned.length
38 return True
41def _postProcess(state, delimiters):
42 i = len(delimiters) - 1
43 while i >= 0:
44 startDelim = delimiters[i]
46 # /* _ */ /* * */
47 if startDelim.marker != 0x5F and startDelim.marker != 0x2A:
48 i -= 1
49 continue
51 # Process only opening markers
52 if startDelim.end == -1:
53 i -= 1
54 continue
56 endDelim = delimiters[startDelim.end]
58 # If the previous delimiter has the same marker and is adjacent to this one,
59 # merge those into one strong delimiter.
60 #
61 # `<em><em>whatever</em></em>` -> `<strong>whatever</strong>`
62 #
63 isStrong = (
64 i > 0
65 and delimiters[i - 1].end == startDelim.end + 1
66 and delimiters[i - 1].token == startDelim.token - 1
67 and delimiters[startDelim.end + 1].token == endDelim.token + 1
68 and delimiters[i - 1].marker == startDelim.marker
69 )
71 ch = chr(startDelim.marker)
73 token = state.tokens[startDelim.token]
74 token.type = "strong_open" if isStrong else "em_open"
75 token.tag = "strong" if isStrong else "em"
76 token.nesting = 1
77 token.markup = ch + ch if isStrong else ch
78 token.content = ""
80 token = state.tokens[endDelim.token]
81 token.type = "strong_close" if isStrong else "em_close"
82 token.tag = "strong" if isStrong else "em"
83 token.nesting = -1
84 token.markup = ch + ch if isStrong else ch
85 token.content = ""
87 if isStrong:
88 state.tokens[delimiters[i - 1].token].content = ""
89 state.tokens[delimiters[startDelim.end + 1].token].content = ""
90 i -= 1
92 i -= 1
95def postProcess(state: StateInline):
96 """Walk through delimiter list and replace text tokens with tags."""
97 _postProcess(state, state.delimiters)
99 for token in state.tokens_meta:
100 if token and "delimiters" in token:
101 _postProcess(state, token["delimiters"])