Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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.

You can see some examples in the django code itself. For example in defaulttags.py.

share|improve this answer
    
Hey there jcollado, thanks for your reply. This escaped output is being returned outside of django, and in fact when I use render_to_string on my template with my data in the shell it works great. Let me know if I am missing something. –  ajt Jan 18 '12 at 18:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.