This repository was archived by the owner on Jul 30, 2023. It is now read-only.
forked from googlearchive/py-gfm
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtasklist.py
331 lines (253 loc) · 10.4 KB
/
tasklist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
"""
:mod:`gfm.tasklist` -- Task list support
========================================
The :mod:`gfm.tasklist` module provides GitHub-like support for task lists.
Those are normal lists with a checkbox-like syntax at the beginning of items
that will be converted to actual checkbox inputs. Nested lists are supported.
Example syntax::
- [x] milk
- [ ] eggs
- [x] chocolate
- [ ] if possible:
1. [ ] solve world peace
2. [ ] solve world hunger
.. NOTE::
GitHub has support for updating the Markdown source text by toggling the
checkbox (by clicking on it). This is not supported by this extension, as it
requires server-side processing that is out of scope of a Markdown parser.
Available configuration options
-------------------------------
================== ============== ======== ===========
Name Type Default Description
================== ============== ======== ===========
``unordered`` bool ``True`` Set to ``False`` to disable parsing of unordered lists.
``ordered`` bool ``True`` Set to ``False`` to disable parsing of ordered lists.
``max_depth`` integer ∞ Set to a positive integer to stop parsing nested task
lists that are deeper than this limit.
``list_attrs`` dict, callable ``{}`` Attributes to be added to the ``<ul>`` or ``<ol>`` element
containing the items.
``item_attrs`` dict, callable ``{}`` Attributes to be added to the ``<li>`` element containing
the checkbox. See `Item attributes`_.
``checkbox_attrs`` dict, callable ``{}`` Attributes to be added to the checkbox element.
See `Checkbox attributes`_.
================== ============== ======== ===========
List attributes
***************
If option ``list_attrs`` is a *dict*, the key-value pairs will be applied to
the ``<ul>`` (resp. ``<ol>``) unordered (resp. ordered) list element, that is
the parent element of the ``<li>`` elements.
.. warning::
These attributes are applied to all nesting levels of lists, that is,
to both the root lists and their potential sub-lists, recursively.
You can control this behavior by using a *callable* instead (see below).
If option ``list_attrs`` is a *callable*, it should be a function that respects
the following prototype::
def function(list, depth: int) -> dict:
where:
- ``list`` is the ``<ul>`` or ``<ol>`` element;
- ``depth`` is the depth of this list relative to its root list (root lists have
a depth of 1).
The returned *dict* items will be applied as HTML attributes to the list
element.
.. note::
Thanks to this feature, you could apply attributes to root lists only.
Take this code sample::
import markdown
from gfm import TaskListExtension
def list_attr_cb(list, depth):
if depth == 1:
return {'class': 'tasklist'}
return {}
tl_ext = TaskListExtension(list_attrs=list_attr_cb)
print(markdown.markdown(\"""
- [x] some thing
- [ ] some other
- [ ] sub thing
- [ ] sub other
\""", extensions=[tl_ext]))
In this example, only the root list will have the ``tasklist`` class
attribute, not the one containing “sub” items.
Item attributes
***************
If option ``item_attrs`` is a *dict*, the key-value pairs will be applied to
the ``<li>`` element as its HTML attributes.
Example::
item_attrs={'class': 'list-item'}
will result in::
<li class="list-item">...</li>
If option ``item_attrs`` is a *callable*, it should be a function that
respects the following prototype::
def function(parent, element, checkbox) -> dict:
where:
- ``parent`` is the ``<li>`` parent element;
- ``element`` is the ``<li>`` element;
- ``checkbox`` is the generated ``<input type="checkbox">`` element.
The returned *dict* items will be applied as HTML attributes to the ``<li>``
element containing the checkbox.
Checkbox attributes
*******************
If option ``checkbox_attrs`` is a *dict*, the key-value pairs will be applied to
the ``<input type="checkbox">`` element as its HTML attributes.
Example::
checkbox_attrs={'class': 'list-cb'}
will result in::
<li><input type="checkbox" class="list-cb"> ...</li>
If option ``checkbox_attrs`` is a *callable*, it should be a function that
respects the following prototype::
def function(parent, element) -> dict:
where:
- ``parent`` is the ``<li>`` parent element;
- ``element`` is the ``<li>`` element.
The returned *dict* items will be applied as HTML attributes to the checkbox
element.
Typical usage
-------------
.. testcode::
import markdown
from gfm import TaskListExtension
print(markdown.markdown(\"""
- [x] milk
- [ ] eggs
- [x] chocolate
- not a checkbox
\""", extensions=[TaskListExtension()]))
.. testoutput::
<ul>
<li><input checked="checked" disabled="disabled" type="checkbox" /> milk</li>
<li><input disabled="disabled" type="checkbox" /> eggs</li>
<li><input checked="checked" disabled="disabled" type="checkbox" /> chocolate</li>
<li>not a checkbox</li>
</ul>
"""
import markdown
from functools import reduce
from markdown.treeprocessors import Treeprocessor
import xml.etree.ElementTree as etree
def _to_list(obj):
if isinstance(obj, str):
return [obj]
return list(obj)
class TaskListProcessor(Treeprocessor):
def __init__(self, ext):
super(TaskListProcessor, self).__init__()
self.ext = ext
def run(self, root):
ordered = self.ext.getConfig("ordered")
unordered = self.ext.getConfig("unordered")
if not ordered and not unordered:
return root
checked_pattern = _to_list(self.ext.getConfig("checked"))
unchecked_pattern = _to_list(self.ext.getConfig("unchecked"))
if not checked_pattern and not unchecked_pattern:
return root
prefix_length = reduce(
max, (len(e) for e in checked_pattern + unchecked_pattern)
)
item_attrs = self.ext.getConfig("item_attrs")
list_attrs = self.ext.getConfig("list_attrs")
base_cb_attrs = self.ext.getConfig("checkbox_attrs")
max_depth = self.ext.getConfig("max_depth")
if not max_depth:
max_depth = float("inf")
lists = set()
stack = [(root, None, 0)]
while stack:
el, parent, depth = stack.pop()
if (
parent
and el.tag == "li"
and (parent.tag == "ul" and unordered or parent.tag == "ol" and ordered)
):
depth += 1
text = (el.text or "").lstrip()
lower_text = text[:prefix_length].lower()
found = False
checked = False
for p in checked_pattern:
if lower_text.startswith(p):
found = True
checked = True
text = text[len(p) :]
break
else:
for p in unchecked_pattern:
if lower_text.startswith(p):
found = True
text = text[len(p) :]
break
if found:
# Add root <ol> or <ul> element to the list set
if list_attrs:
lists.add((parent, depth))
# Checkbox attributes
attrs = {"type": "checkbox", "disabled": "disabled"}
if checked:
attrs["checked"] = "checked"
# Give user a chance to update checkbox attributes
attrs.update(
base_cb_attrs(parent, el)
if callable(base_cb_attrs)
else base_cb_attrs
)
checkbox = etree.Element("input", attrs)
checkbox.tail = text
# Prepend checkbox to <li>
el.text = ""
el.insert(0, checkbox)
# Give user a chance to update <li> attributes
for k, v in (
item_attrs(parent, el, checkbox)
if callable(item_attrs)
else item_attrs
).items():
el.set(k, v)
if depth < max_depth:
for child in el:
stack.append((child, el, depth))
for list, depth in lists:
for k, v in (
list_attrs(list, depth) if callable(list_attrs) else list_attrs
).items():
list.set(k, v)
return root
class TaskListExtension(markdown.Extension):
"""
An extension that supports GitHub task lists. Both ordered and unordered
lists are supported and can be separately enabled. Nested lists are
supported.
Example::
- [x] milk
- [ ] eggs
- [x] chocolate
- [ ] if possible:
1. [ ] solve world peace
2. [ ] solve world hunger
"""
def __init__(self, **kwargs):
self.config = {
"ordered": [True, "Enable parsing of ordered lists"],
"unordered": [True, "Enable parsing of unordered lists"],
"checked": ["[x]", "The checked state pattern"],
"unchecked": ["[ ]", "The unchecked state pattern"],
"max_depth": [0, "Maximum list nesting depth (None for " "unlimited)"],
"list_attrs": [
{},
"Additional attribute dict (or callable) to "
"add to the <ul> or <ol> element",
],
"item_attrs": [
{},
"Additional attribute dict (or callable) to " "add to the <li> element",
],
"checkbox_attrs": [
{},
"Additional attribute dict (or callable) "
"to add to the checkbox <input> element",
],
}
super().__init__(**kwargs)
def extendMarkdown(self, md):
md.treeprocessors.register(TaskListProcessor(self), "gfm-tasklist", 100)