A fork of hyde, the static site generation. Some patches will be pushed upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

35 lines
1.1 KiB

  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