Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/markdown_it/rules_block/fence.py: 100%
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# fences (``` lang, ~~~ lang)
2import logging
4from .state_block import StateBlock
6LOGGER = logging.getLogger(__name__)
9def fence(state: StateBlock, startLine: int, endLine: int, silent: 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 it's indented more than 3 spaces, it should be a code block
17 if state.sCount[startLine] - state.blkIndent >= 4:
18 return False
20 if pos + 3 > maximum:
21 return False
23 marker = state.srcCharCode[pos]
25 # /* ~ */ /* ` */
26 if marker != 0x7E and marker != 0x60:
27 return False
29 # scan marker length
30 mem = pos
31 pos = state.skipChars(pos, marker)
33 length = pos - mem
35 if length < 3:
36 return False
38 markup = state.src[mem:pos]
39 params = state.src[pos:maximum]
41 # /* ` */
42 if marker == 0x60:
43 if chr(marker) in params:
44 return False
46 # Since start is found, we can report success here in validation mode
47 if silent:
48 return True
50 # search end of block
51 nextLine = startLine
53 while True:
54 nextLine += 1
55 if nextLine >= endLine:
56 # unclosed block should be autoclosed by end of document.
57 # also block seems to be autoclosed by end of parent
58 break
60 pos = mem = state.bMarks[nextLine] + state.tShift[nextLine]
61 maximum = state.eMarks[nextLine]
63 if pos < maximum and state.sCount[nextLine] < state.blkIndent:
64 # non-empty line with negative indent should stop the list:
65 # - ```
66 # test
67 break
69 if state.srcCharCode[pos] != marker:
70 continue
72 if state.sCount[nextLine] - state.blkIndent >= 4:
73 # closing fence should be indented less than 4 spaces
74 continue
76 pos = state.skipChars(pos, marker)
78 # closing code fence must be at least as long as the opening one
79 if pos - mem < length:
80 continue
82 # make sure tail has spaces only
83 pos = state.skipSpaces(pos)
85 if pos < maximum:
86 continue
88 haveEndMarker = True
89 # found!
90 break
92 # If a fence has heading spaces, they should be removed from its inner block
93 length = state.sCount[startLine]
95 state.line = nextLine + (1 if haveEndMarker else 0)
97 token = state.push("fence", "code", 0)
98 token.info = params
99 token.content = state.getLines(startLine + 1, nextLine, length, True)
100 token.markup = markup
101 token.map = [startLine, state.line]
103 return True