Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/markdown_it/rules_block/fence.py: 100%
57 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:15 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:15 +0000
1# fences (``` lang, ~~~ lang)
2import logging
4from .state_block import StateBlock
6LOGGER = logging.getLogger(__name__)
9def fence(state: StateBlock, startLine: int, endLine: int, silent: bool) -> bool:
10 LOGGER.debug("entering fence: %s, %s, %s, %s", state, startLine, endLine, silent)
12 haveEndMarker = False
13 pos = state.bMarks[startLine] + state.tShift[startLine]
14 maximum = state.eMarks[startLine]
16 if state.is_code_block(startLine):
17 return False
19 if pos + 3 > maximum:
20 return False
22 marker = state.src[pos]
24 if marker not in ("~", "`"):
25 return False
27 # scan marker length
28 mem = pos
29 pos = state.skipCharsStr(pos, marker)
31 length = pos - mem
33 if length < 3:
34 return False
36 markup = state.src[mem:pos]
37 params = state.src[pos:maximum]
39 if marker == "`" and marker in params:
40 return False
42 # Since start is found, we can report success here in validation mode
43 if silent:
44 return True
46 # search end of block
47 nextLine = startLine
49 while True:
50 nextLine += 1
51 if nextLine >= endLine:
52 # unclosed block should be autoclosed by end of document.
53 # also block seems to be autoclosed by end of parent
54 break
56 pos = mem = state.bMarks[nextLine] + state.tShift[nextLine]
57 maximum = state.eMarks[nextLine]
59 if pos < maximum and state.sCount[nextLine] < state.blkIndent:
60 # non-empty line with negative indent should stop the list:
61 # - ```
62 # test
63 break
65 try:
66 if state.src[pos] != marker:
67 continue
68 except IndexError:
69 break
71 if state.is_code_block(nextLine):
72 continue
74 pos = state.skipCharsStr(pos, marker)
76 # closing code fence must be at least as long as the opening one
77 if pos - mem < length:
78 continue
80 # make sure tail has spaces only
81 pos = state.skipSpaces(pos)
83 if pos < maximum:
84 continue
86 haveEndMarker = True
87 # found!
88 break
90 # If a fence has heading spaces, they should be removed from its inner block
91 length = state.sCount[startLine]
93 state.line = nextLine + (1 if haveEndMarker else 0)
95 token = state.push("fence", "code", 0)
96 token.info = params
97 token.content = state.getLines(startLine + 1, nextLine, length, True)
98 token.markup = markup
99 token.map = [startLine, state.line]
101 return True