-
Notifications
You must be signed in to change notification settings - Fork 844
/
Copy pathmailto.py
41 lines (27 loc) · 1.16 KB
/
mailto.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
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import codecs
from pelican import signals
from pelican.generators import Generator
from linker import linker
def encode_mailto_link(mailto):
return 'mailto/' + codecs.encode(mailto, 'rot_13') + '/'
class MailtoLinker(linker.LinkerBase):
commands = ['mailto']
def link(self, link):
mailto = link.path
link.path = '/' + encode_mailto_link(mailto) # a.href for JS parsing
link.context['mailtos'].add(mailto) # remember mail address for fallback
class MailtoFallbackGenerator(Generator):
def generate_context(self):
self.context['mailtos'] = set() # populated on {mailto} link processing
def generate_output(self, writer):
for mailto in self.context['mailtos']:
save_as = encode_mailto_link(mailto) + 'index.html'
writer.write_file(save_as, self.get_template('mailto_fallback'),
self.context, mailto=mailto)
def return_mailto_fallback_generator(generators):
return MailtoFallbackGenerator
def register():
linker.register()
signals.get_generators.connect(return_mailto_fallback_generator)