/hyde/ext/plugins/textlinks.py

http://github.com/hyde/hyde · Python · 35 lines · 22 code · 3 blank · 10 comment · 0 complexity · 3049bcd3807dc7130b07ccc13cf5b84c MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. """
  3. Textlinks plugin
  4. """
  5. import re
  6. from hyde.plugin import Plugin
  7. class TextlinksPlugin(Plugin):
  8. """
  9. The plugin class for syntax text replacement.
  10. """
  11. def __init__(self, site):
  12. super(TextlinksPlugin, self).__init__(site)
  13. def begin_text_resource(self, resource, text):
  14. """
  15. Replace content url pattern [[/abc/def]])
  16. with
  17. {{ content_url('/abc/def') }} or equivalent and
  18. Replace media url pattern [[!!/abc/def]]
  19. with
  20. {{ media_url('/abc/def') }} or equivalent.
  21. """
  22. if not resource.uses_template:
  23. return text
  24. content_link = re.compile('\[\[([^\]^!][^\]]*)\]\]', re.UNICODE|re.MULTILINE)
  25. media_link = re.compile('\[\[\!\!([^\]]*)\]\]', re.UNICODE|re.MULTILINE)
  26. def replace_content(match):
  27. return self.template.get_content_url_statement(match.groups(1)[0])
  28. def replace_media(match):
  29. return self.template.get_media_url_statement(match.groups(1)[0])
  30. text = content_link.sub(replace_content, text)
  31. text = media_link.sub(replace_media, text)
  32. return text