codekingpro/portable-devtools
114k
1"""Proceess '\n'."""2from ..common.utils import charStrAt, isStrSpace3from .state_inline import StateInline4 5 6def newline(state: StateInline, silent: bool) -> bool:7 pos = state.pos8 9 if state.src[pos] != "\n":10 return False11 12 pmax = len(state.pending) - 113 maximum = state.posMax14 15 # ' \n' -> hardbreak16 # Lookup in pending chars is bad practice! Don't copy to other rules!17 # Pending string is stored in concat mode, indexed lookups will cause18 # 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 - 124 while ws >= 1 and charStrAt(state.pending, ws - 1) == " ":25 ws -= 126 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 += 137 38 # skip heading spaces for next line39 while pos < maximum and isStrSpace(state.src[pos]):40 pos += 141 42 state.pos = pos43 return True44 