Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/mistune/plugins/footnotes.py: 28%

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

88 statements  

1import re 

2from typing import TYPE_CHECKING, Any, Dict, List, Match, Union 

3 

4from ..core import BlockState 

5from ..helpers import LINK_LABEL 

6from ..util import unikey 

7 

8if TYPE_CHECKING: 

9 from ..block_parser import BlockParser 

10 from ..core import BaseRenderer, InlineState 

11 from ..inline_parser import InlineParser 

12 from ..markdown import Markdown 

13 

14__all__ = ["footnotes"] 

15 

16_PARAGRAPH_SPLIT = re.compile(r"\n{2,}") 

17# https://michelf.ca/projects/php-markdown/extra/#footnotes 

18REF_FOOTNOTE = ( 

19 r"^(?P<footnote_lead> {0,4})" 

20 r"\[\^(?P<footnote_key>" + LINK_LABEL + r")]:[ \t\n]" 

21 r"(?P<footnote_text>[^\n]*(?:\n+|$)" 

22 r"(?:(?P=footnote_lead) {1,4}(?! )[^\n]*\n+)*" 

23 r")" 

24) 

25 

26INLINE_FOOTNOTE = r"\[\^(?P<footnote_key>" + LINK_LABEL + r")\]" 

27 

28 

29def parse_inline_footnote(inline: "InlineParser", m: Match[str], state: "InlineState") -> int: 

30 key = unikey(m.group("footnote_key")) 

31 ref = state.env.get("ref_footnotes") 

32 if ref and key in ref: 

33 notes = state.env.get("footnotes") 

34 if not notes: 

35 notes = [] 

36 if key not in notes: 

37 notes.append(key) 

38 state.env["footnotes"] = notes 

39 state.append_token({"type": "footnote_ref", "raw": key, "attrs": {"index": notes.index(key) + 1}}) 

40 else: 

41 state.append_token({"type": "text", "raw": m.group(0)}) 

42 return m.end() 

43 

44 

45def parse_ref_footnote(block: "BlockParser", m: Match[str], state: BlockState) -> int: 

46 ref = state.env.get("ref_footnotes") 

47 if not ref: 

48 ref = {} 

49 

50 key = unikey(m.group("footnote_key")) 

51 if key not in ref: 

52 ref[key] = m.group("footnote_text") 

53 state.env["ref_footnotes"] = ref 

54 return m.end() 

55 

56 

57def parse_footnote_item(block: "BlockParser", key: str, index: int, state: BlockState) -> Dict[str, Any]: 

58 ref = state.env.get("ref_footnotes") 

59 if not ref: 

60 raise ValueError("Missing 'ref_footnotes'.") 

61 text = ref[key] 

62 

63 lines = text.splitlines() 

64 second_line = None 

65 for second_line in lines[1:]: 

66 if second_line: 

67 break 

68 

69 if second_line: 

70 spaces = len(second_line) - len(second_line.lstrip()) 

71 pattern = re.compile(r"^ {" + str(spaces) + r",}", flags=re.M) 

72 text = pattern.sub("", text).strip() 

73 

74 footer_state = BlockState() 

75 footer_state.process(text) 

76 block.parse(footer_state) 

77 children = footer_state.tokens 

78 else: 

79 text = text.strip() 

80 children = [{"type": "paragraph", "text": text}] 

81 return {"type": "footnote_item", "children": children, "attrs": {"key": key, "index": index}} 

82 

83 

84def md_footnotes_hook( 

85 md: "Markdown", result: Union[str, List[Dict[str, Any]]], state: BlockState 

86) -> Union[str, List[Dict[str, Any]]]: 

87 notes = state.env.get("footnotes") 

88 if not notes: 

89 return result 

90 

91 children = [parse_footnote_item(md.block, k, i + 1, state) for i, k in enumerate(notes)] 

92 state = BlockState(parent=state) 

93 state.tokens = [{"type": "footnotes", "children": children}] 

94 output = md.render_state(state) 

95 return result + output # type: ignore[operator] 

96 

97 

98def render_footnote_ref(renderer: "BaseRenderer", key: str, index: int) -> str: 

99 i = str(index) 

100 html = '<sup class="footnote-ref" id="fnref-' + i + '">' 

101 return html + '<a href="#fn-' + i + '">' + i + "</a></sup>" 

102 

103 

104def render_footnotes(renderer: "BaseRenderer", text: str) -> str: 

105 return '<section class="footnotes">\n<ol>\n' + text + "</ol>\n</section>\n" 

106 

107 

108def render_footnote_item(renderer: "BaseRenderer", text: str, key: str, index: int) -> str: 

109 i = str(index) 

110 back = '<a href="#fnref-' + i + '" class="footnote">&#8617;</a>' 

111 text = text.rstrip() 

112 if text.endswith("</p>"): 

113 text = text[:-4] + back + "</p>" 

114 else: 

115 text = text + "\n" + back 

116 return '<li id="fn-' + i + '">' + text + "</li>\n" 

117 

118 

119def footnotes(md: "Markdown") -> None: 

120 """A mistune plugin to support footnotes, spec defined at 

121 https://michelf.ca/projects/php-markdown/extra/#footnotes 

122 

123 Here is an example: 

124 

125 .. code-block:: text 

126 

127 That's some text with a footnote.[^1] 

128 

129 [^1]: And that's the footnote. 

130 

131 It will be converted into HTML: 

132 

133 .. code-block:: html 

134 

135 <p>That's some text with a footnote.<sup class="footnote-ref" id="fnref-1"><a href="#fn-1">1</a></sup></p> 

136 <section class="footnotes"> 

137 <ol> 

138 <li id="fn-1"><p>And that's the footnote.<a href="#fnref-1" class="footnote">&#8617;</a></p></li> 

139 </ol> 

140 </section> 

141 

142 :param md: Markdown instance 

143 """ 

144 md.inline.register( 

145 "footnote", 

146 INLINE_FOOTNOTE, 

147 parse_inline_footnote, 

148 before="link", 

149 ) 

150 md.block.register( 

151 "ref_footnote", 

152 REF_FOOTNOTE, 

153 parse_ref_footnote, 

154 before="ref_link", 

155 ) 

156 md.after_render_hooks.append(md_footnotes_hook) 

157 

158 if md.renderer and md.renderer.NAME == "html": 

159 md.renderer.register("footnote_ref", render_footnote_ref) 

160 md.renderer.register("footnote_item", render_footnote_item) 

161 md.renderer.register("footnotes", render_footnotes)