PageRenderTime 224ms CodeModel.GetById 111ms app.highlight 7ms RepoModel.GetById 104ms app.codeStats 0ms

/wiki/templatetags/restructuredtext.py

http://django-wikiapp.googlecode.com/
Python | 106 lines | 100 code | 4 blank | 2 comment | 4 complexity | ff1645f3c55d35f21189b3e54f67d72c MD5 | raw file
  1from django import template
  2from django.conf import settings
  3
  4register = template.Library()
  5
  6@register.filter
  7def flatpagehist_diff_previous(self):
  8    return self.diff_previous()
  9
 10@register.filter
 11def restructuredparts(value, **overrides):
 12    """return the restructured text parts"""
 13    try:
 14        from docutils.core import publish_parts
 15    except ImportError:
 16        if settings.DEBUG:
 17            raise template.TemplateSyntaxError, "Error in {% restructuredtext %} filter: The Python docutils library isn't installed."
 18        return value
 19    else:
 20        docutils_settings = dict(getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {}))
 21        docutils_settings.update(overrides)
 22        if 'halt_level' not in docutils_settings:
 23            docutils_settings['halt_level'] = 6
 24        return publish_parts(source=value, writer_name="html4css1", settings_overrides=docutils_settings)
 25
 26@register.filter
 27def restructuredtext(value, **overrides):
 28    """The django version of this markup filter has an issue when only one title or subtitle is supplied in that

 29    they are dropped from the markup. This is due to the use of 'fragment' instead of something like 'html_body'.

 30    We do not want to use 'html_body' either due to some header/footer stuff we want to prevent, but we want to

 31    keep the title and subtitle. So we include them if present."""
 32    parts = restructuredparts(value, **overrides)
 33    if not isinstance(parts, dict):
 34        return value
 35    return parts["html_body"]
 36
 37@register.filter
 38def restructuredtext_has_errors(value, do_raise=False):
 39    ## RED_FLAG: need to catch the explicit exceptions and not a catch all...

 40    try:
 41        restructuredparts(value, halt_level=2, traceback=1)
 42        return False
 43    except:
 44        if do_raise:
 45            raise
 46    return True
 47
 48class ReStructuredTextNode(template.Node):
 49    def __init__(self, nodelist):
 50        self.nodelist = nodelist
 51
 52    def render(self, context):
 53        return restructuredtext(self.nodelist.render(context))
 54
 55@register.tag("restructuredtext")
 56def rest_tag(parser, token):
 57    """

 58    Render the ReStructuredText into html. Will pre-render template code first.

 59

 60    Example:

 61    ::

 62

 63        {% restructuredtext %}

 64            ===================================

 65            To: {{ send_to }}

 66            ===================================

 67            {% include "email_form.rst" %}

 68        {% endrestructuredtext %}

 69

 70    """
 71    nodelist = parser.parse(('endrestructuredtext',))
 72    parser.delete_first_token()
 73    return ReStructuredTextNode(nodelist)
 74
 75@register.inclusion_tag("restructuredtext/dynamic.html", takes_context=True)
 76def rstflatpage(context):
 77    """

 78    The core content of the restructuredtext flatpage with history, editing,

 79    etc. for use in your 'flatpages/default.html' or custom template.

 80

 81    Example:

 82    ::

 83

 84        <html><head><title>{{ flatpage.title }}</title></head>

 85              <body>{% load restructuredtext %}{% rstflatpage %}</body>

 86        </html>

 87

 88

 89    This will inject one of 6 different page contents:

 90

 91    * Just the flatpage.content for normal flatpages (so templates can be

 92      used with normal flatpages).

 93    * A custom 404 page (with optional create/restore form).

 94    * View current (with optional edit/history/delete form).

 95    * View version (with optional history/current/revert form).

 96    * Edit form (with preview/save/cancel).

 97    * History listing.

 98    """
 99    return context
100
101@register.inclusion_tag("restructuredtext/feeds.html", takes_context=True)
102def rstflatpage_feeds(context):
103    """

104    Optionally inserts the history feeds

105    """
106    return context