-
Notifications
You must be signed in to change notification settings - Fork 0
/
gio_customblocks.py
159 lines (133 loc) · 4.46 KB
/
gio_customblocks.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
# -*- coding: utf8 -*-
from customblocks.utils import Markdown as cbMarkdown
from customblocks.utils import E as cbE
defined_custom_blocks = {}
# Helpers
# Decorator to queue a named callback to be registered
def customblock(name):
def _customblock(callback):
defined_custom_blocks[name] = callback
return callback
return _customblock
def filter_key_prefixes(prefix_blacklist, object):
"""
>>> filter_key_prefixes(['a_'], {'a_b': 1, 'b_c': 2})
{'b_c': 2}
"""
filtered = filter(
lambda kv: not any(
kv[0].startswith(pre) for pre in prefix_blacklist
),
object.items()
)
return dict(filtered)
# Blocks
@customblock('pre')
def cb_pre(ctx, *args, **kwargs):
slugargs = ['-'.join(arg.split()) for arg in args]
return cbE(
f"pre.pre-wrap",
{'_class': ' '.join(slugargs)},
ctx.content,
**kwargs
)
@customblock('aside')
def cb_aside(ctx, title=None, *args, **kwargs):
slugargs = ['-'.join(arg.split()) for arg in args]
return cbE(
f"aside.cb.{title}",
{'_class': ' '.join(slugargs)},
cbE(
f"div", {'class': 'aside-header'},
cbE(f"span", {'class': 'icon'}),
cbE(f"span", {'class': 'type'}),
),
cbMarkdown(ctx.content, ctx.parser),
**kwargs
)
@customblock('blockquote')
def cb_blockquote(ctx, *args, **kwargs):
slugargs = ['-'.join(arg.split()) for arg in args]
return cbE(
f"blockquote",
{'class': ' '.join(slugargs)},
cbMarkdown(ctx.content, ctx.parser),
**kwargs
)
@customblock('spoiler')
def cb_spoiler(ctx, desc=None, *args, **kwargs):
slugargs = ['-'.join(arg.split()) for arg in args]
return cbE(
f"div.spoiler-wrapper", {'_class': ' '.join(slugargs)},
cbE(f"button", {
'type': 'button',
'class': 'spoiler-button',
'onclick': f"this.setAttribute('open', !(this.getAttribute('open') == 'true'))"
}, desc),
cbE(
f"div.spoiler-content",
cbMarkdown(ctx.content, ctx.parser)
),
**kwargs
)
@customblock('imessage')
def cb_imessage(ctx, name=None, image=None, *args, **kwargs):
slugargs = ['-'.join(arg.split()) for arg in args]
body_etree = cbE(
"blockquote",
{'class': ' '.join(['imessage'] + slugargs)},
(
cbE(
'div',
{'class': 'phhead'},
(cbE('img', {'src': image}) if image else None),
(name if name else None)
)
if image or name else None
),
cbMarkdown(ctx.content, ctx.parser),
**kwargs
)
for root in body_etree.findall('ul'):
for author_group in root.findall('li'):
author = author_group.text
lowered = author.lower()
author_group.set('data-author', lowered)
if lowered not in ['you', 'them']:
author_group.insert(
0,
cbE('span', {'class': 'author'}, author)
)
author_group.text = ''
return body_etree
@customblock('discord')
def cb_discord(ctx, name=None, image=None, *args, **kwargs):
slugargs = ['-'.join(arg.split()) for arg in args]
body_etree = cbE(
"blockquote",
{'class': ' '.join(['discord'] + slugargs)},
cbMarkdown(ctx.content, ctx.parser),
**filter_key_prefixes(['color_', 'avatar_'], kwargs)
)
for root in body_etree.findall('ul'):
for author_group in root.findall('li'):
author_group.set('data-author', author_group.text.lower())
style_str = ""
color = kwargs.get(f'color_{author_group.text}')
if color:
style_str += f"--role-color: {color}; "
avatar = kwargs.get(f'avatar_{author_group.text}')
if avatar:
style_str += f"--icon: url({avatar}); "
if style_str:
author_group.set('style', style_str)
return body_etree
def pelican_init(pelican_object):
def registerCustomBlock(name, callback):
pelican_object.settings['MARKDOWN']['extension_configs']['customblocks']['generators'][name] = callback
for (name, callback) in defined_custom_blocks.items():
registerCustomBlock(name, callback)
def register():
"""Plugin registration"""
from pelican import signals
signals.initialized.connect(pelican_init)