Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/markdown_it/rules_block/lheading.py: 98%
55 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:07 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:07 +0000
1# lheading (---, ==)
2import logging
4from ..ruler import Ruler
5from .state_block import StateBlock
7LOGGER = logging.getLogger(__name__)
10def lheading(state: StateBlock, startLine: int, endLine: int, silent: bool):
11 LOGGER.debug("entering lheading: %s, %s, %s, %s", state, startLine, endLine, silent)
13 level = None
14 nextLine = startLine + 1
15 ruler: Ruler = state.md.block.ruler
16 terminatorRules = ruler.getRules("paragraph")
18 # if it's indented more than 3 spaces, it should be a code block
19 if state.sCount[startLine] - state.blkIndent >= 4:
20 return False
22 oldParentType = state.parentType
23 state.parentType = "paragraph" # use paragraph to match terminatorRules
25 # jump line-by-line until empty one or EOF
26 while nextLine < endLine and not state.isEmpty(nextLine):
27 # this would be a code block normally, but after paragraph
28 # it's considered a lazy continuation regardless of what's there
29 if state.sCount[nextLine] - state.blkIndent > 3:
30 nextLine += 1
31 continue
33 # Check for underline in setext header
34 if state.sCount[nextLine] >= state.blkIndent:
35 pos = state.bMarks[nextLine] + state.tShift[nextLine]
36 maximum = state.eMarks[nextLine]
38 if pos < maximum:
39 marker = state.srcCharCode[pos]
41 # /* - */ /* = */
42 if marker == 0x2D or marker == 0x3D:
43 pos = state.skipChars(pos, marker)
44 pos = state.skipSpaces(pos)
46 # /* = */
47 if pos >= maximum:
48 level = 1 if marker == 0x3D else 2
49 break
51 # quirk for blockquotes, this line should already be checked by that rule
52 if state.sCount[nextLine] < 0:
53 nextLine += 1
54 continue
56 # Some tags can terminate paragraph without empty line.
57 terminate = False
58 for terminatorRule in terminatorRules:
59 if terminatorRule(state, nextLine, endLine, True):
60 terminate = True
61 break
62 if terminate:
63 break
65 nextLine += 1
67 if not level:
68 # Didn't find valid underline
69 return False
71 content = state.getLines(startLine, nextLine, state.blkIndent, False).strip()
73 state.line = nextLine + 1
75 token = state.push("heading_open", "h" + str(level), 1)
76 token.markup = chr(marker)
77 token.map = [startLine, state.line]
79 token = state.push("inline", "", 0)
80 token.content = content
81 token.map = [startLine, state.line - 1]
82 token.children = []
84 token = state.push("heading_close", "h" + str(level), -1)
85 token.markup = chr(marker)
87 state.parentType = oldParentType
89 return True