Generic markup converter

 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

  1. MarkupField by jpt 5 years, 2 months ago
  2. typygmentdown by ubernostrum 6 years, 8 months ago
  3. MarkupTextField by myles 5 years, 5 months ago
  4. Render markdown to a separate model field by mindcruzer 1 year, 7 months ago
  5. Markup Selection in Admin by jonathan 6 years, 8 months ago

Comments

(Forgotten your password?)