- Author:
- ubernostrum
- Posted:
- March 13, 2007
- Language:
- Python
- Version:
- Pre .96
- Score:
- 13 (after 13 ratings)
I'm a big fan of Markdown, and often set up models to automatically apply it to certain fields before saving. But that's not really flexible, because if I then distribute the code someone else might want to use reStructuredText or Textile or whatever, and then they have to hack my code.
So here's a function which looks for a setting called MARKUP_FILTER
and, based on what it finds there (see the docstring for what it looks at), chooses a text-to-HTML conversion function and applies it to a piece of text. Since Textile, Markdown and reStructuredText all support various useful options, it has the ability to pick up arbitrary keyword arguments and pass them through.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | def apply_markup_filter(text):
"""
Applies a text-to-HTML conversion function to a piece of text and
returns the generated HTML.
The function to use is derived from the value of the setting
``MARKUP_FILTER``, which should be a 2-tuple:
* The first element should be the name of a markup filter --
e.g.,"markdown" -- to apply. If no markup filter is desired,
set this to None.
* The second element should be a dictionary of keyword
arguments which will be passed to the markup function. If no
extra arguments are desired, set this to an empty
dictionary; some arguments may still be inferred as needed,
however.
So, for example, to use Markdown with safe mode turned on (safe
mode removes raw HTML), put this in your settings file::
MARKUP_FILTER = ('markdown', { 'safe_mode': True })
Currently supports Textile, Markdown and reStructuredText, using
names identical to the template filters found in
``django.contrib.markup``.
"""
from django.conf import settings
markup_func_name, markup_kwargs = settings.MARKUP_FILTER
if markup_func_name is None: # No processing is needed.
return text
if markup_func_name not in ('textile', 'markdown', 'restructuredtext'):
raise ValueError("'%s' is not a valid value for the first element of MARKUP_FILTER; acceptable values are 'textile', 'markdown', 'restructuredtext' and None" % markup_func_name)
if markup_func_name == 'textile':
import textile
if 'encoding' not in markup_kwargs:
markup_kwargs.update(encoding=settings.DEFAULT_CHARSET)
if 'output' not in markup_kwargs:
markup_kwargs.update(output=settings.DEFAULT_CHARSET)
return textile.textile(text, **markup_kwargs)
elif markup_func_name == 'markdown':
import markdown
return markdown.markdown(text, **markup_kwargs)
elif markup_func_name == 'restructuredtext':
from docutils import core
if 'settings_overrides' not in markup_kwargs:
markup_kwargs.update(settings_overrides=getattr(settings, "RESTRUCTUREDTEXT_FILTER_SETTINGS", {}))
if 'writer_name' not in markup_kwargs:
markup_kwargs.update(writer_name='html4css1')
parts = core.publish_parts(source=text, **markup_kwargs)
return parts['fragment']
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.