Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/mdit_py_plugins/front_matter/index.py: 99%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

67 statements  

1"""Process front matter.""" 

2 

3from markdown_it import MarkdownIt 

4from markdown_it.rules_block import StateBlock 

5 

6from mdit_py_plugins.utils import is_code_block 

7 

8 

9def front_matter_plugin(md: MarkdownIt) -> None: 

10 """Plugin ported from 

11 `markdown-it-front-matter <https://github.com/ParkSB/markdown-it-front-matter>`__. 

12 

13 It parses initial metadata, stored between opening/closing dashes: 

14 

15 .. code-block:: md 

16 

17 --- 

18 valid-front-matter: true 

19 --- 

20 

21 """ 

22 md.block.ruler.before( 

23 "table", 

24 "front_matter", 

25 _front_matter_rule, 

26 {"alt": ["paragraph", "reference", "blockquote", "list"]}, 

27 ) 

28 

29 

30def _front_matter_rule( 

31 state: StateBlock, startLine: int, endLine: int, silent: bool 

32) -> bool: 

33 marker_chr = "-" 

34 min_markers = 3 

35 

36 auto_closed = False 

37 start = state.bMarks[startLine] + state.tShift[startLine] 

38 maximum = state.eMarks[startLine] 

39 src_len = len(state.src) 

40 

41 # Check out the first character of the first line quickly, 

42 # this should filter out non-front matter 

43 if startLine != 0 or state.src[0] != marker_chr: 

44 return False 

45 

46 # Check out the rest of the marker string 

47 # while pos <= 3 

48 pos = start + 1 

49 while pos <= maximum and pos < src_len: 

50 if state.src[pos] != marker_chr: 

51 break 

52 pos += 1 

53 

54 marker_count = pos - start 

55 

56 if marker_count < min_markers: 

57 return False 

58 

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

60 if silent: 

61 return True 

62 

63 # Search for the end of the block 

64 nextLine = startLine 

65 

66 while True: 

67 nextLine += 1 

68 if nextLine >= endLine: 

69 # unclosed block should be autoclosed by end of document. 

70 return False 

71 

72 start = state.bMarks[nextLine] + state.tShift[nextLine] 

73 maximum = state.eMarks[nextLine] 

74 

75 if ( 

76 state.sCount[nextLine] == 0 

77 and state.src.startswith("...", start) 

78 and state.skipSpaces(start + 3) >= maximum 

79 ): 

80 # YAML document-end marker: `...` at column 0, trailing spaces allowed. 

81 # It is checked on the current line and consumed, so it is not content. 

82 auto_closed = True 

83 break 

84 

85 if start < maximum and state.sCount[nextLine] < state.blkIndent: 

86 # non-empty line with negative indent should stop the list: 

87 # - ``` 

88 # test 

89 break 

90 

91 if state.src[start] != marker_chr: 

92 continue 

93 

94 if is_code_block(state, nextLine): 

95 continue 

96 

97 pos = start + 1 

98 while pos < maximum: 

99 if state.src[pos] != marker_chr: 

100 break 

101 pos += 1 

102 

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

104 if (pos - start) < marker_count: 

105 continue 

106 

107 # make sure tail has spaces only 

108 pos = state.skipSpaces(pos) 

109 

110 if pos < maximum: 

111 continue 

112 

113 # found! 

114 auto_closed = True 

115 break 

116 

117 old_parent = state.parentType 

118 old_line_max = state.lineMax 

119 state.parentType = "container" 

120 

121 # this will prevent lazy continuations from ever going past our end marker 

122 state.lineMax = nextLine 

123 

124 token = state.push("front_matter", "", 0) 

125 token.hidden = True 

126 token.markup = marker_chr * min_markers 

127 token.content = state.src[state.bMarks[startLine + 1] : state.eMarks[nextLine - 1]] 

128 token.block = True 

129 

130 state.parentType = old_parent 

131 state.lineMax = old_line_max 

132 state.line = nextLine + (1 if auto_closed else 0) 

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

134 

135 return True