heading.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """ Atex heading (#, ##, ...) """
  2. from __future__ import annotations
  3. import logging
  4. from ..common.utils import isStrSpace
  5. from .state_block import StateBlock
  6. LOGGER = logging.getLogger(__name__)
  7. def heading(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
  8. LOGGER.debug("entering heading: %s, %s, %s, %s", state, startLine, endLine, silent)
  9. pos = state.bMarks[startLine] + state.tShift[startLine]
  10. maximum = state.eMarks[startLine]
  11. if state.is_code_block(startLine):
  12. return False
  13. ch: str | None = state.src[pos]
  14. if ch != "#" or pos >= maximum:
  15. return False
  16. # count heading level
  17. level = 1
  18. pos += 1
  19. try:
  20. ch = state.src[pos]
  21. except IndexError:
  22. ch = None
  23. while ch == "#" and pos < maximum and level <= 6:
  24. level += 1
  25. pos += 1
  26. try:
  27. ch = state.src[pos]
  28. except IndexError:
  29. ch = None
  30. if level > 6 or (pos < maximum and not isStrSpace(ch)):
  31. return False
  32. if silent:
  33. return True
  34. # Let's cut tails like ' ### ' from the end of string
  35. maximum = state.skipSpacesBack(maximum, pos)
  36. tmp = state.skipCharsStrBack(maximum, "#", pos)
  37. if tmp > pos and isStrSpace(state.src[tmp - 1]):
  38. maximum = tmp
  39. state.line = startLine + 1
  40. token = state.push("heading_open", "h" + str(level), 1)
  41. token.markup = "########"[:level]
  42. token.map = [startLine, state.line]
  43. token = state.push("inline", "", 0)
  44. token.content = state.src[pos:maximum].strip()
  45. token.map = [startLine, state.line]
  46. token.children = []
  47. token = state.push("heading_close", "h" + str(level), -1)
  48. token.markup = "########"[:level]
  49. return True