newline.py 1.3 KB

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