-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtopic_box.py
144 lines (128 loc) · 4.11 KB
/
topic_box.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
"""
Sphinx directive for HTML Topic Box Components.
"""
from docutils import nodes
from docutils.parsers.rst import Directive, directives
from .utils import generate_template, is_url
class TopicBox(Directive):
has_content = True
option_spec = {
"title": directives.unchanged_required,
"link": directives.path,
"link_target": directives.path,
"anchor": directives.path,
"icon": directives.path,
"icon_color": directives.path,
"image": directives.path,
"class": directives.unchanged,
}
def run(self):
class_name = "topic-box"
container_class_name = self.options.get("class", "").replace(",", " ")
link = self.options.get("link")
link_target = self.options.get("link_target", "auto")
link_template = """
<div class="{class_name} {container_class_name}">
<div class="card">
"""
if link:
target_attr = ""
if link_target == "_blank":
target_attr = 'target="_blank"'
elif link_target == "auto" and is_url(link):
target_attr = 'target="_blank"'
link_template = f"""
<div class="cell {class_name} {container_class_name}">
<a class="card" href="{link}" {target_attr}>
"""
html_tag_open = generate_template(
link_template,
class_name=class_name,
container_class_name=container_class_name,
link=link,
)
html_tag_close = "</a></div>" if link else "</div></div>"
icon = self.options.get("icon")
icon_color = self.options.get("icon_color", "#23263b")
image = self.options.get("image")
html_icon = (
generate_template(
"""
<div class="{class_name}__icon">
<i class="{icon}"></i>
</div>
""",
class_name=class_name,
icon=icon,
icon_color=icon_color,
)
if icon
else generate_template(
"""
<div class="{class_name}__icon"">
<img src="{image}"/>
</div>
""",
class_name=class_name,
image=image,
)
if image
else ""
)
anchor = self.options.get("anchor")
anchor = (
generate_template(
"""
<div class="{class_name}__anchor">{anchor}
<i class="fa fa-external-link" aria-hidden="true"></i>
</div>
"""
if target_attr == 'target="_blank"'
else """
<div class="{class_name}__anchor">{anchor}</div>
""",
class_name=class_name,
anchor=anchor,
)
if anchor
else ""
)
html0 = generate_template(
"""
{html_tag_open}
<div class="{class_name}__head">
{html_icon}
<h3 class="{class_name}__title">{title}</h3>
</div>
<div class="{class_name}__body">
""",
class_name=class_name,
html_tag_open=html_tag_open,
html_icon=html_icon,
title=self.options.get("title", ""),
)
html1 = generate_template(
"""
{anchor}
</div>
{html_tag_close}
""",
anchor=anchor,
class_name=class_name,
html_tag_close=html_tag_close,
)
description_node = nodes.container()
if self.state:
self.state.nested_parse(self.content, 0, description_node)
return [
nodes.raw(text=html0, format="html"),
description_node,
nodes.raw(text=html1, format="html"),
]
def setup(app):
app.add_directive("topic-box", TopicBox)
return {
"version": "0.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
}