mailto.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import codecs
  4. from pelican import signals
  5. from pelican.generators import Generator
  6. from linker import linker
  7. def encode_mailto_link(mailto):
  8. return 'mailto/' + codecs.encode(mailto, 'rot_13') + '/'
  9. class MailtoLinker(linker.LinkerBase):
  10. commands = ['mailto']
  11. def link(self, link):
  12. mailto = link.path
  13. link.path = '/' + encode_mailto_link(mailto) # a.href for JS parsing
  14. link.context['mailtos'].add(mailto) # remember mail address for fallback
  15. class MailtoFallbackGenerator(Generator):
  16. def generate_context(self):
  17. self.context['mailtos'] = set() # populated on {mailto} link processing
  18. def generate_output(self, writer):
  19. for mailto in self.context['mailtos']:
  20. save_as = encode_mailto_link(mailto) + 'index.html'
  21. writer.write_file(save_as, self.get_template('mailto_fallback'),
  22. self.context, mailto=mailto)
  23. def return_mailto_fallback_generator(generators):
  24. return MailtoFallbackGenerator
  25. def register():
  26. linker.register()
  27. signals.get_generators.connect(return_mailto_fallback_generator)