1"""Proceess '\n'."""
2from ..common.utils import charStrAt, isStrSpace
3from .state_inline import StateInline
4
5
6def newline(state: StateInline, silent: bool) -> bool:
7 pos = state.pos
8
9 if state.src[pos] != "\n":
10 return False
11
12 pmax = len(state.pending) - 1
13 maximum = state.posMax
14
15 # ' \n' -> hardbreak
16 # Lookup in pending chars is bad practice! Don't copy to other rules!
17 # Pending string is stored in concat mode, indexed lookups will cause
18 # conversion to flat mode.
19 if not silent:
20 if pmax >= 0 and charStrAt(state.pending, pmax) == " ":
21 if pmax >= 1 and charStrAt(state.pending, pmax - 1) == " ":
22 # Find whitespaces tail of pending chars.
23 ws = pmax - 1
24 while ws >= 1 and charStrAt(state.pending, ws - 1) == " ":
25 ws -= 1
26 state.pending = state.pending[:ws]
27
28 state.push("hardbreak", "br", 0)
29 else:
30 state.pending = state.pending[:-1]
31 state.push("softbreak", "br", 0)
32
33 else:
34 state.push("softbreak", "br", 0)
35
36 pos += 1
37
38 # skip heading spaces for next line
39 while pos < maximum and isStrSpace(state.src[pos]):
40 pos += 1
41
42 state.pos = pos
43 return True