1"""
2Process escaped chars and hardbreaks
3"""
4
5from ..common.utils import isStrSpace
6from .state_inline import StateInline
7
8
9def escape(state: StateInline, silent: bool) -> bool:
10 """Process escaped chars and hardbreaks."""
11 pos = state.pos
12 maximum = state.posMax
13
14 if state.src[pos] != "\\":
15 return False
16
17 pos += 1
18
19 # '\' at the end of the inline block
20 if pos >= maximum:
21 return False
22
23 ch1 = state.src[pos]
24 ch1_ord = ord(ch1)
25 if ch1 == "\n":
26 if not silent:
27 state.push("hardbreak", "br", 0)
28 pos += 1
29 # skip leading whitespaces from next line
30 while pos < maximum:
31 ch = state.src[pos]
32 if not isStrSpace(ch):
33 break
34 pos += 1
35
36 state.pos = pos
37 return True
38
39 escapedStr = state.src[pos]
40
41 if ch1_ord >= 0xD800 and ch1_ord <= 0xDBFF and pos + 1 < maximum:
42 ch2 = state.src[pos + 1]
43 ch2_ord = ord(ch2)
44 if ch2_ord >= 0xDC00 and ch2_ord <= 0xDFFF:
45 escapedStr += ch2
46 pos += 1
47
48 origStr = "\\" + escapedStr
49
50 if not silent:
51 token = state.push("text_special", "", 0)
52 token.content = escapedStr if ch1 in _ESCAPED else origStr
53 token.markup = origStr
54 token.info = "escape"
55
56 state.pos = pos + 1
57 return True
58
59
60_ESCAPED = {
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 "~",
93}