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

1# fences (``` lang, ~~~ lang) 

2import logging 

3 

4from .state_block import StateBlock 

5 

6LOGGER = logging.getLogger(__name__) 

7 

8 

9def fence(state: StateBlock, startLine: int, endLine: int, silent: bool): 

10 LOGGER.debug("entering fence: %s, %s, %s, %s", state, startLine, endLine, silent) 

11 

12 haveEndMarker = False 

13 pos = state.bMarks[startLine] + state.tShift[startLine] 

14 maximum = state.eMarks[startLine] 

15 

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 

19 

20 if pos + 3 > maximum: 

21 return False 

22 

23 marker = state.srcCharCode[pos] 

24 

25 # /* ~ */ /* ` */ 

26 if marker != 0x7E and marker != 0x60: 

27 return False 

28 

29 # scan marker length 

30 mem = pos 

31 pos = state.skipChars(pos, marker) 

32 

33 length = pos - mem 

34 

35 if length < 3: 

36 return False 

37 

38 markup = state.src[mem:pos] 

39 params = state.src[pos:maximum] 

40 

41 # /* ` */ 

42 if marker == 0x60: 

43 if chr(marker) in params: 

44 return False 

45 

46 # Since start is found, we can report success here in validation mode 

47 if silent: 

48 return True 

49 

50 # search end of block 

51 nextLine = startLine 

52 

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 

59 

60 pos = mem = state.bMarks[nextLine] + state.tShift[nextLine] 

61 maximum = state.eMarks[nextLine] 

62 

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 

68 

69 if state.srcCharCode[pos] != marker: 

70 continue 

71 

72 if state.sCount[nextLine] - state.blkIndent >= 4: 

73 # closing fence should be indented less than 4 spaces 

74 continue 

75 

76 pos = state.skipChars(pos, marker) 

77 

78 # closing code fence must be at least as long as the opening one 

79 if pos - mem < length: 

80 continue 

81 

82 # make sure tail has spaces only 

83 pos = state.skipSpaces(pos) 

84 

85 if pos < maximum: 

86 continue 

87 

88 haveEndMarker = True 

89 # found! 

90 break 

91 

92 # If a fence has heading spaces, they should be removed from its inner block 

93 length = state.sCount[startLine] 

94 

95 state.line = nextLine + (1 if haveEndMarker else 0) 

96 

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] 

102 

103 return True