/hyde/ext/plugins/markings.py

http://github.com/hyde/hyde · Python · 85 lines · 34 code · 12 blank · 39 comment · 1 complexity · 51442150e5f8e5a2da2a7e6f4d712f98 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. """
  3. Markings plugin
  4. """
  5. from hyde.plugin import TextyPlugin
  6. class MarkingsPlugin(TextyPlugin):
  7. """
  8. The plugin class for mark text replacement.
  9. """
  10. def __init__(self, site):
  11. super(MarkingsPlugin, self).__init__(site)
  12. @property
  13. def tag_name(self):
  14. """
  15. The mark tag.
  16. """
  17. return 'mark'
  18. @property
  19. def default_open_pattern(self):
  20. """
  21. The default pattern for mark open text.
  22. """
  23. return u'^§§+\s*([A-Za-z0-9_\-]+)\s*$'
  24. @property
  25. def default_close_pattern(self):
  26. """
  27. The default pattern for mark close text.
  28. """
  29. return u'^§§+\s*/([A-Za-z0-9_\-]*)\s*$'
  30. def text_to_tag(self, match, start=True):
  31. """
  32. Replace open pattern (default:§§ CSS)
  33. with
  34. {% mark CSS %} or equivalent and
  35. Replace close pattern (default: §§ /CSS)
  36. with
  37. {% endmark %} or equivalent
  38. """
  39. return super(MarkingsPlugin, self).text_to_tag(match, start)
  40. class ReferencePlugin(TextyPlugin):
  41. """
  42. The plugin class for reference text replacement.
  43. """
  44. def __init__(self, site):
  45. super(ReferencePlugin, self).__init__(site)
  46. @property
  47. def tag_name(self):
  48. """
  49. The refer tag.
  50. """
  51. return 'refer to'
  52. @property
  53. def default_open_pattern(self):
  54. """
  55. The default pattern for mark open text.
  56. """
  57. return u'^?\s*([^\s]+)\s*as\s*([A-Za-z0-9_\-]+)\s*$'
  58. @property
  59. def default_close_pattern(self):
  60. """
  61. No close pattern.
  62. """
  63. return None
  64. def text_to_tag(self, match, start=True):
  65. """
  66. Replace open pattern (default: ? inc.md as inc)
  67. with
  68. {% refer to "inc.md" as inc %} or equivalent.
  69. """
  70. if not match.lastindex:
  71. return ''
  72. params = '"%s" as %s' % (match.groups(1)[0], match.groups(1)[1])
  73. return self.template.get_open_tag(self.tag_name, params)