Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/markdown_it/rules_block/heading.py: 100%
46 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""" Atex heading (#, ##, ...) """
2from __future__ import annotations
4import logging
6from ..common.utils import isSpace
7from .state_block import StateBlock
9LOGGER = logging.getLogger(__name__)
12def heading(state: StateBlock, startLine: int, endLine: int, silent: bool):
13 LOGGER.debug("entering heading: %s, %s, %s, %s", state, startLine, endLine, silent)
15 pos = state.bMarks[startLine] + state.tShift[startLine]
16 maximum = state.eMarks[startLine]
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 ch: int | None = state.srcCharCode[pos]
24 # /* # */
25 if ch != 0x23 or pos >= maximum:
26 return False
28 # count heading level
29 level = 1
30 pos += 1
31 try:
32 ch = state.srcCharCode[pos]
33 except IndexError:
34 ch = None
35 # /* # */
36 while ch == 0x23 and pos < maximum and level <= 6:
37 level += 1
38 pos += 1
39 try:
40 ch = state.srcCharCode[pos]
41 except IndexError:
42 ch = None
44 if level > 6 or (pos < maximum and not isSpace(ch)):
45 return False
47 if silent:
48 return True
50 # Let's cut tails like ' ### ' from the end of string
52 maximum = state.skipSpacesBack(maximum, pos)
53 tmp = state.skipCharsBack(maximum, 0x23, pos) # #
54 if tmp > pos and isSpace(state.srcCharCode[tmp - 1]):
55 maximum = tmp
57 state.line = startLine + 1
59 token = state.push("heading_open", "h" + str(level), 1)
60 token.markup = "########"[:level]
61 token.map = [startLine, state.line]
63 token = state.push("inline", "", 0)
64 token.content = state.src[pos:maximum].strip()
65 token.map = [startLine, state.line]
66 token.children = []
68 token = state.push("heading_close", "h" + str(level), -1)
69 token.markup = "########"[:level]
71 return True