1"""
2Process escaped chars and hardbreaks
3"""
4from ..common.utils import isStrSpace
5from .state_inline import StateInline
6
7
8def escape(state: StateInline, silent: bool) -> bool:
9 """Process escaped chars and hardbreaks."""
10 pos = state.pos
11 maximum = state.posMax
12
13 if state.src[pos] != "\\":
14 return False
15
16 pos += 1
17
18 # '\' at the end of the inline block
19 if pos >= maximum:
20 return False
21
22 ch1 = state.src[pos]
23 ch1_ord = ord(ch1)
24 if ch1 == "\n":
25 if not silent:
26 state.push("hardbreak", "br", 0)
27 pos += 1
28 # skip leading whitespaces from next line
29 while pos < maximum:
30 ch = state.src[pos]
31 if not isStrSpace(ch):
32 break
33 pos += 1
34
35 state.pos = pos
36 return True
37
38 escapedStr = state.src[pos]
39
40 if ch1_ord >= 0xD800 and ch1_ord <= 0xDBFF and pos + 1 < maximum:
41 ch2 = state.src[pos + 1]
42 ch2_ord = ord(ch2)
43 if ch2_ord >= 0xDC00 and ch2_ord <= 0xDFFF:
44 escapedStr += ch2
45 pos += 1
46
47 origStr = "\\" + escapedStr
48
49 if not silent:
50 token = state.push("text_special", "", 0)
51 token.content = escapedStr if ch1 in _ESCAPED else origStr
52 token.markup = origStr
53 token.info = "escape"
54
55 state.pos = pos + 1
56 return True
57
58
59_ESCAPED = {
60 "!",
61 '"',
62 "#",
63 "$",
64 "%",
65 "&",
66 "'",
67 "(",
68 ")",
69 "*",
70 "+",
71 ",",
72 "-",
73 ".",
74 "/",
75 ":",
76 ";",
77 "<",
78 "=",
79 ">",
80 "?",
81 "@",
82 "[",
83 "\\",
84 "]",
85 "^",
86 "_",
87 "`",
88 "{",
89 "|",
90 "}",
91 "~",
92}