paragraph.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """Paragraph."""
  2. import logging
  3. from .state_block import StateBlock
  4. LOGGER = logging.getLogger(__name__)
  5. def paragraph(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
  6. LOGGER.debug(
  7. "entering paragraph: %s, %s, %s, %s", state, startLine, endLine, silent
  8. )
  9. nextLine = startLine + 1
  10. ruler = state.md.block.ruler
  11. terminatorRules = ruler.getRules("paragraph")
  12. endLine = state.lineMax
  13. oldParentType = state.parentType
  14. state.parentType = "paragraph"
  15. # jump line-by-line until empty one or EOF
  16. while nextLine < endLine:
  17. if state.isEmpty(nextLine):
  18. break
  19. # this would be a code block normally, but after paragraph
  20. # it's considered a lazy continuation regardless of what's there
  21. if state.sCount[nextLine] - state.blkIndent > 3:
  22. nextLine += 1
  23. continue
  24. # quirk for blockquotes, this line should already be checked by that rule
  25. if state.sCount[nextLine] < 0:
  26. nextLine += 1
  27. continue
  28. # Some tags can terminate paragraph without empty line.
  29. terminate = False
  30. for terminatorRule in terminatorRules:
  31. if terminatorRule(state, nextLine, endLine, True):
  32. terminate = True
  33. break
  34. if terminate:
  35. break
  36. nextLine += 1
  37. content = state.getLines(startLine, nextLine, state.blkIndent, False).strip()
  38. state.line = nextLine
  39. token = state.push("paragraph_open", "p", 1)
  40. token.map = [startLine, state.line]
  41. token = state.push("inline", "", 0)
  42. token.content = content
  43. token.map = [startLine, state.line]
  44. token.children = []
  45. token = state.push("paragraph_close", "p", -1)
  46. state.parentType = oldParentType
  47. return True