-
Notifications
You must be signed in to change notification settings - Fork 1
/
community_card.py
173 lines (153 loc) · 5.89 KB
/
community_card.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
"""This module implements the ``community-card`` reST directive."""
import re
from inspect import cleandoc
from docutils import nodes
from docutils.parsers.rst import Directive, directives
from docutils.statemachine import StringList
COMMUNITY_CARD_TEMPLATE = cleandoc(
"""
.. raw:: html
<div class="card community-card plugin-card" id={id}>
<div class="card-header {colour} lighten-4">
<h4 class="card-header__text">{title}</h4>
</div>
<div class="card-body">
<div class="row">
<div class="col-lg-8 d-flex flex-column">
<div>
<h6>{author}</h6>
<p class="font-small">
<i class="far fa-clock pr-1"></i> {date}
</p>
</div>
<p class="plugin-card__description">
{description}
</p>
<div class="mt-auto plugin-card__read-more-wrapper">
<a
class="plugin-card__read-more text-primary d-none"
data-toggle="modal"
data-target="#{id}-modal"
>
Read More
</a>
</div>
</div>
<div class="col-lg-4 d-flex">
<div class="plugin-card__buttons">
{paper_footer}
{blog_footer}
{code_footer}
</div>
</div>
</div>
</div>
</div>
<div
class="modal fade" id="{id}-modal"
tabindex="-1"
role="dialog"
aria-labelledby="{id}-modal"
aria-hidden="true"
>
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title mt-0">{title}</h5>
<button
type="button"
class="close"
data-dismiss="modal"
aria-label="Close"
>
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{description}
</div>
<div class="modal-footer">
{paper_footer}
{blog_footer}
{code_footer}
</div>
</div>
</div>
</div>
"""
)
PAPER_FOOTER_TEMPLATE = cleandoc(
"""
<a href="{paper}" class="btn btn-info plugin-card__paper-btn" style="width: 100%;"><i class="fas fa-book"></i> Paper</a>
"""
)
BLOG_FOOTER_TEMPLATE = cleandoc(
"""
<a href="{blog}" class="btn btn-info plugin-card__blog-btn" style="width: 100%;"><i class="fas fa-newspaper"></i> Blog</a>
"""
)
CODE_FOOTER_TEMPLATE = cleandoc(
"""
<a href="{code}" class="btn btn-default plugin-card__code-btn" style="width: 100%;"><i class="fas fa-code-branch"></i></i> Code</a>
"""
)
class CommunityCardDirective(Directive):
"""Creates a community card."""
required_arguments = 0
optional_arguments = 4
option_spec = {
"title": directives.unchanged_required,
"author": directives.unchanged_required,
"paper": directives.unchanged,
"blog": directives.unchanged,
"code": directives.unchanged,
"date": directives.unchanged_required,
"colour": directives.unchanged,
}
final_argument_whitespace = False
has_content = True
add_index = False
def run(self):
description = [i if i != "" else "<br><br>" for i in self.content]
colour = self.options.get("colour", "heavy-rain-gradient")
if "paper" in self.options:
paper_footer = PAPER_FOOTER_TEMPLATE.format(paper=self.options["paper"])
else:
paper_footer = ""
if "code" in self.options:
code_footer = CODE_FOOTER_TEMPLATE.format(code=self.options["code"])
else:
code_footer = ""
if "blog" in self.options:
blog_footer = BLOG_FOOTER_TEMPLATE.format(blog=self.options["blog"])
else:
blog_footer = ""
def remove_accents(raw_text):
"""Removes common accent characters."""
raw_text = re.sub("[àáâãäå]", "a", raw_text)
raw_text = re.sub("[èéêë]", "e", raw_text)
raw_text = re.sub("[ìíîï]", "i", raw_text)
raw_text = re.sub("[òóôõö]", "o", raw_text)
raw_text = re.sub("[ùúûü]", "u", raw_text)
raw_text = re.sub("[ýÿ]", "y", raw_text)
raw_text = re.sub("[ß]", "ss", raw_text)
raw_text = re.sub("[ñ]", "n", raw_text)
return raw_text
id_ = remove_accents(self.options["author"].split(" ")[-1].lower())
id_ += self.options["date"].split("/")[-1]
id_ += self.options["title"].split(" ")[0].lower()
card_rst = COMMUNITY_CARD_TEMPLATE.format(
title=self.options["title"],
author=self.options["author"],
description=" ".join(description),
date=self.options["date"],
paper_footer=paper_footer,
code_footer=code_footer,
blog_footer=blog_footer,
colour=colour,
id=id_,
)
thumbnail = StringList(card_rst.split("\n"))
thumb = nodes.paragraph()
self.state.nested_parse(thumbnail, self.content_offset, thumb)
return [thumb]