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