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

1# lheading (---, ==) 

2import logging 

3 

4from ..ruler import Ruler 

5from .state_block import StateBlock 

6 

7LOGGER = logging.getLogger(__name__) 

8 

9 

10def lheading(state: StateBlock, startLine: int, endLine: int, silent: bool): 

11 LOGGER.debug("entering lheading: %s, %s, %s, %s", state, startLine, endLine, silent) 

12 

13 level = None 

14 nextLine = startLine + 1 

15 ruler: Ruler = state.md.block.ruler 

16 terminatorRules = ruler.getRules("paragraph") 

17 

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 

21 

22 oldParentType = state.parentType 

23 state.parentType = "paragraph" # use paragraph to match terminatorRules 

24 

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 

32 

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] 

37 

38 if pos < maximum: 

39 marker = state.srcCharCode[pos] 

40 

41 # /* - */ /* = */ 

42 if marker == 0x2D or marker == 0x3D: 

43 pos = state.skipChars(pos, marker) 

44 pos = state.skipSpaces(pos) 

45 

46 # /* = */ 

47 if pos >= maximum: 

48 level = 1 if marker == 0x3D else 2 

49 break 

50 

51 # quirk for blockquotes, this line should already be checked by that rule 

52 if state.sCount[nextLine] < 0: 

53 nextLine += 1 

54 continue 

55 

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 

64 

65 nextLine += 1 

66 

67 if not level: 

68 # Didn't find valid underline 

69 return False 

70 

71 content = state.getLines(startLine, nextLine, state.blkIndent, False).strip() 

72 

73 state.line = nextLine + 1 

74 

75 token = state.push("heading_open", "h" + str(level), 1) 

76 token.markup = chr(marker) 

77 token.map = [startLine, state.line] 

78 

79 token = state.push("inline", "", 0) 

80 token.content = content 

81 token.map = [startLine, state.line - 1] 

82 token.children = [] 

83 

84 token = state.push("heading_close", "h" + str(level), -1) 

85 token.markup = chr(marker) 

86 

87 state.parentType = oldParentType 

88 

89 return True