hr.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """Horizontal rule
  2. At least 3 of these characters on a line * - _
  3. """
  4. import logging
  5. from ..common.utils import isStrSpace
  6. from .state_block import StateBlock
  7. LOGGER = logging.getLogger(__name__)
  8. def hr(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
  9. LOGGER.debug("entering hr: %s, %s, %s, %s", state, startLine, endLine, silent)
  10. pos = state.bMarks[startLine] + state.tShift[startLine]
  11. maximum = state.eMarks[startLine]
  12. if state.is_code_block(startLine):
  13. return False
  14. try:
  15. marker = state.src[pos]
  16. except IndexError:
  17. return False
  18. pos += 1
  19. # Check hr marker
  20. if marker not in ("*", "-", "_"):
  21. return False
  22. # markers can be mixed with spaces, but there should be at least 3 of them
  23. cnt = 1
  24. while pos < maximum:
  25. ch = state.src[pos]
  26. pos += 1
  27. if ch != marker and not isStrSpace(ch):
  28. return False
  29. if ch == marker:
  30. cnt += 1
  31. if cnt < 3:
  32. return False
  33. if silent:
  34. return True
  35. state.line = startLine + 1
  36. token = state.push("hr", "hr", 0)
  37. token.map = [startLine, state.line]
  38. token.markup = marker * (cnt + 1)
  39. return True