PageRenderTime 29ms CodeModel.GetById 13ms RepoModel.GetById 2ms app.codeStats 0ms

/wiki/templatetags/restructuredtext.py

http://django-wikiapp.googlecode.com/
Python | 106 lines | 100 code | 4 blank | 2 comment | 5 complexity | ff1645f3c55d35f21189b3e54f67d72c MD5 | raw file
  1. from django import template
  2. from django.conf import settings
  3. register = template.Library()
  4. @register.filter
  5. def flatpagehist_diff_previous(self):
  6. return self.diff_previous()
  7. @register.filter
  8. def restructuredparts(value, **overrides):
  9. """return the restructured text parts"""
  10. try:
  11. from docutils.core import publish_parts
  12. except ImportError:
  13. if settings.DEBUG:
  14. raise template.TemplateSyntaxError, "Error in {% restructuredtext %} filter: The Python docutils library isn't installed."
  15. return value
  16. else:
  17. docutils_settings = dict(getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {}))
  18. docutils_settings.update(overrides)
  19. if 'halt_level' not in docutils_settings:
  20. docutils_settings['halt_level'] = 6
  21. return publish_parts(source=value, writer_name="html4css1", settings_overrides=docutils_settings)
  22. @register.filter
  23. def restructuredtext(value, **overrides):
  24. """The django version of this markup filter has an issue when only one title or subtitle is supplied in that
  25. they are dropped from the markup. This is due to the use of 'fragment' instead of something like 'html_body'.
  26. We do not want to use 'html_body' either due to some header/footer stuff we want to prevent, but we want to
  27. keep the title and subtitle. So we include them if present."""
  28. parts = restructuredparts(value, **overrides)
  29. if not isinstance(parts, dict):
  30. return value
  31. return parts["html_body"]
  32. @register.filter
  33. def restructuredtext_has_errors(value, do_raise=False):
  34. ## RED_FLAG: need to catch the explicit exceptions and not a catch all...
  35. try:
  36. restructuredparts(value, halt_level=2, traceback=1)
  37. return False
  38. except:
  39. if do_raise:
  40. raise
  41. return True
  42. class ReStructuredTextNode(template.Node):
  43. def __init__(self, nodelist):
  44. self.nodelist = nodelist
  45. def render(self, context):
  46. return restructuredtext(self.nodelist.render(context))
  47. @register.tag("restructuredtext")
  48. def rest_tag(parser, token):
  49. """
  50. Render the ReStructuredText into html. Will pre-render template code first.
  51. Example:
  52. ::
  53. {% restructuredtext %}
  54. ===================================
  55. To: {{ send_to }}
  56. ===================================
  57. {% include "email_form.rst" %}
  58. {% endrestructuredtext %}
  59. """
  60. nodelist = parser.parse(('endrestructuredtext',))
  61. parser.delete_first_token()
  62. return ReStructuredTextNode(nodelist)
  63. @register.inclusion_tag("restructuredtext/dynamic.html", takes_context=True)
  64. def rstflatpage(context):
  65. """
  66. The core content of the restructuredtext flatpage with history, editing,
  67. etc. for use in your 'flatpages/default.html' or custom template.
  68. Example:
  69. ::
  70. <html><head><title>{{ flatpage.title }}</title></head>
  71. <body>{% load restructuredtext %}{% rstflatpage %}</body>
  72. </html>
  73. This will inject one of 6 different page contents:
  74. * Just the flatpage.content for normal flatpages (so templates can be
  75. used with normal flatpages).
  76. * A custom 404 page (with optional create/restore form).
  77. * View current (with optional edit/history/delete form).
  78. * View version (with optional history/current/revert form).
  79. * Edit form (with preview/save/cancel).
  80. * History listing.
  81. """
  82. return context
  83. @register.inclusion_tag("restructuredtext/feeds.html", takes_context=True)
  84. def rstflatpage_feeds(context):
  85. """
  86. Optionally inserts the history feeds
  87. """
  88. return context