Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/mdit_py_plugins/tasklists/__init__.py: 74%

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

70 statements  

1"""Builds task/todo lists out of markdown lists with items starting with [ ] or [x]""" 

2 

3# Ported by Wolmar Nyberg Åkerström from https://github.com/revin/markdown-it-task-lists 

4# ISC License 

5# Copyright (c) 2016, Revin Guillen 

6# 

7# Permission to use, copy, modify, and/or distribute this software for any 

8# purpose with or without fee is hereby granted, provided that the above 

9# copyright notice and this permission notice appear in all copies. 

10# 

11# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 

12# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 

13# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 

14# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 

15# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 

16# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 

17# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 

18from __future__ import annotations 

19 

20import re 

21from uuid import uuid4 

22 

23from markdown_it import MarkdownIt 

24from markdown_it.common.utils import escapeHtml 

25from markdown_it.rules_core import StateCore 

26from markdown_it.token import Token 

27 

28# Regex string to match a whitespace character, as specified in 

29# https://github.github.com/gfm/#whitespace-character 

30# (spec version 0.29-gfm (2019-04-06)) 

31_GFM_WHITESPACE_RE = r"[ \t\n\v\f\r]" 

32 

33 

34def tasklists_plugin( 

35 md: MarkdownIt, 

36 enabled: bool = False, 

37 label: bool = False, 

38 label_after: bool = False, 

39) -> None: 

40 """Plugin for building task/todo lists out of markdown lists with items starting with [ ] or [x] 

41 .. Nothing else 

42 

43 For example:: 

44 - [ ] An item that needs doing 

45 - [x] An item that is complete 

46 

47 The rendered HTML checkboxes are disabled; to change this, pass a truthy value into the enabled 

48 property of the plugin options. 

49 

50 :param enabled: True enables the rendered checkboxes 

51 :param label: True wraps the rendered list items in a <label> element for UX purposes, 

52 :param label_after: True adds the <label> element after the checkbox. 

53 """ 

54 disable_checkboxes = not enabled 

55 use_label_wrapper = label 

56 use_label_after = label_after 

57 

58 def fcn(state: StateCore) -> None: 

59 tokens = state.tokens 

60 for i in range(2, len(tokens) - 1): 

61 if is_todo_item(tokens, i): 

62 todoify(tokens[i]) 

63 tokens[i - 2].attrSet( 

64 "class", 

65 "task-list-item" + (" enabled" if not disable_checkboxes else ""), 

66 ) 

67 tokens[parent_token(tokens, i - 2)].attrSet( 

68 "class", "contains-task-list" 

69 ) 

70 

71 md.core.ruler.after("inline", "github-tasklists", fcn) 

72 

73 def parent_token(tokens: list[Token], index: int) -> int: 

74 target_level = tokens[index].level - 1 

75 for i in range(1, index + 1): 

76 if tokens[index - i].level == target_level: 

77 return index - i 

78 return -1 

79 

80 def is_todo_item(tokens: list[Token], index: int) -> bool: 

81 return ( 

82 is_inline(tokens[index]) 

83 and is_paragraph(tokens[index - 1]) 

84 and is_list_item(tokens[index - 2]) 

85 and starts_with_todo_markdown(tokens[index]) 

86 ) 

87 

88 def todoify(token: Token) -> None: 

89 assert token.children is not None 

90 token.children.insert(0, make_checkbox(token)) 

91 token.children[1].content = token.children[1].content[3:] 

92 token.content = token.content[3:] 

93 

94 if use_label_wrapper: 

95 if use_label_after: 

96 token.children.pop() 

97 

98 # Replaced number generator from original plugin with uuid. 

99 checklist_id = f"task-item-{uuid4()}" 

100 token.children[0].content = ( 

101 token.children[0].content[0:-1] + f' id="{checklist_id}">' 

102 ) 

103 token.children.append(after_label(token.content, checklist_id)) 

104 else: 

105 token.children.insert(0, begin_label()) 

106 token.children.append(end_label()) 

107 

108 def make_checkbox(token: Token) -> Token: 

109 checkbox = Token("html_inline", "", 0) 

110 disabled_attr = 'disabled="disabled"' if disable_checkboxes else "" 

111 # starts_with_todo_markdown already matched "[ ]", "[x]" or "[X]" followed by 

112 # any GFM whitespace, so only the character inside the brackets matters here 

113 if token.content[1] == " ": 

114 checkbox.content = ( 

115 '<input class="task-list-item-checkbox" ' 

116 f'{disabled_attr} type="checkbox">' 

117 ) 

118 else: 

119 checkbox.content = ( 

120 '<input class="task-list-item-checkbox" checked="checked" ' 

121 f'{disabled_attr} type="checkbox">' 

122 ) 

123 return checkbox 

124 

125 def begin_label() -> Token: 

126 token = Token("html_inline", "", 0) 

127 token.content = "<label>" 

128 return token 

129 

130 def end_label() -> Token: 

131 token = Token("html_inline", "", 0) 

132 token.content = "</label>" 

133 return token 

134 

135 def after_label(content: str, checkbox_id: str) -> Token: 

136 token = Token("html_inline", "", 0) 

137 token.content = ( 

138 '<label class="task-list-item-label" ' 

139 f'for="{checkbox_id}">{escapeHtml(content)}</label>' 

140 ) 

141 token.attrs = {"for": checkbox_id} 

142 return token 

143 

144 def is_inline(token: Token) -> bool: 

145 return token.type == "inline" 

146 

147 def is_paragraph(token: Token) -> bool: 

148 return token.type == "paragraph_open" 

149 

150 def is_list_item(token: Token) -> bool: 

151 return token.type == "list_item_open" 

152 

153 def starts_with_todo_markdown(token: Token) -> bool: 

154 # leading whitespace in a list item is already trimmed off by markdown-it 

155 return re.match(rf"\[[ xX]]{_GFM_WHITESPACE_RE}+", token.content) is not None