-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmonition_icons.py
51 lines (40 loc) · 1.51 KB
/
admonition_icons.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
import re
from docutils.nodes import Admonition, admonition, raw, title, warning, note, tip
def setup(app):
app.connect('doctree-resolved', doctree_resolved)
app.connect('doctree-read', doctree_read)
return {'version': '0.1'}
ICONS_CLASSES = {
"note": "sticky-note",
"warning": "exclamation-triangle",
"tip": "lightbulb",
"example": "binoculars",
"examples": "binoculars",
"question": "question",
"try-it": "puzzle-piece",
"fun-fact": "rocket"
}
TEMPLATE = '<i class="fas fa-%s" aria-hidden="true"></i>'
# Convert builtin admonitions to generic ones, for easier icon insertion
def doctree_read(app, doctree):
for node in doctree.traverse(is_builtin_admonition):
new_node = admonition()
name = node.__class__.__name__.title()
title_node = title(name, name)
new_node += title_node
for child in node.children:
new_node += child
node.remove(child)
node.replace_self(new_node)
def is_builtin_admonition(node):
return node.__class__ in [warning, note, tip]
# insert icons
def doctree_resolved(app, doctree, document):
for node in doctree.traverse(title):
if isinstance(node.parent, Admonition):
node_title = node.children[0].astext()
slug = node_title.lower().replace(' ', '-')
slug = re.sub(r'[^a-z\-]', '', slug)
if slug in ICONS_CLASSES:
icon = TEMPLATE % ICONS_CLASSES[slug]
node.insert(0, raw(icon, icon, format='html'))