Disabling HTML Encoding within Output of Custom Python Markdown Extension
Question: I have been working on creating a python markdown extension that will insert
an image gallery within my django project when a custom tag is used. The
actual extension is working, but the HTML that the extension returns is all
encoded. Here is the extension that I am using:

    
    
    #!/usr/bin/env python
    from django.template.loader import render_to_string
    from main.models import *
    
    import markdown
    
    version = "0.1.0"
    
    class GalleriaExtension(markdown.Extension):
        def __init__(self, configs):
            self.config = {
            }
    
            # Override defaults with user settings
            for key, value in configs:
                self.setConfig(key, value)
    
        def add_inline(self, md, name, klass, re):
            pattern = klass(re)
            pattern.md = md
            pattern.ext = self
            md.inlinePatterns.add(name, pattern, "<reference")
    
        def extendMarkdown(self, md, md_globals):
            self.add_inline(md, 'galleria', Galleria,
                r'\[\[(G|g)allery (?P<superpage_id>\w+)\]\]')
    
    class Galleria(markdown.inlinepatterns.Pattern):
        def handleMatch(self, m):
            try:
                images = SuperPage.objects.get(id=m.group('superpage_id')).superpageimage_set.all()
            except:
                images = None
            if images:
                rendered = render_to_string('galleria.html', { 'images': images })
            else:
                rendered = '<b>There are no images for the given ID</b>'
            return rendered 
    
    def makeExtension(configs=None) :
        return GalleriaExtension(configs=configs)
    

I ensured that the `render_to_string` was actually returning html that was not
encoded. From the shell here is an example of the output:

Output from `render_to_string`:

    
    
    >>> from django.template.loader import render_to_string
    >>> images = SuperPage.objects.get(id=8).superpageimage_set.all()
    >>> render_to_string('galleria.html', { 'images': images })
    u'<div class=\'galleria_std\'>\n    <div class=\'gallery\' >\n      <div id=\'stage_gallery\' >\n'
    

Here is output from `markdown` extension that is encoded:

    
    
    >>>markdown.markdown('test [[gallery 8]] test', ['galleria'])
    u'<p>test &lt;div class=\'galleria_std\'&gt;\n    &lt;div class=\'gallery\' &gt;\n'
    

How can I make `rendered` return HTML mark up instead of encoded markup when
using the markdown extension? Also, I would appreciate any pointers on coding
this differently (syntax, layout, etc). I appreciate it.


Answer: If what you're looking for is a way to avoid the contents rendered by your
custom tag be autoescaped, then you can mark the string as safe with
[django.utils.safestring.mark_safe](https://docs.djangoproject.com/en/1.3/ref/utils/#django.utils.safestring.mark_safe).

You can see some examples in the django code itself. For example in
[`defaulttags.py`](https://code.djangoproject.com/browser/django/trunk/django/template/defaulttags.py).

