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 <div class=\'galleria_std\'>\n <div class=\'gallery\' >\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.
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
.
I solved the problem like that:
import markdown
from markdown import Extension
from markdown.inlinepatterns import Pattern
from django.template.loader import render_to_string
from gallery.models import Gallery
class InlineGalleryExtension(Extension):
def extendMarkdown(self, markdownInstance, md_globals):
GALLERY_RE = r'\[\[gallery (?P<id>[0-9]+)\]\]'
inlineGalleryPattern = InlineGallery(GALLERY_RE, markdownInstance)
markdownInstance.inlinePatterns.add('gallery', inlineGalleryPattern, "<not_strong")
class InlineGallery(Pattern):
def handleMatch(self, m):
try:
gallery = Gallery.objects.get(id=m.group('id').strip())
except Gallery.DoesNotExist:
gallery = None
if gallery:
element = render_to_string('gallery/inline_gallery.html', {
'gallery': gallery
})
else:
element = ''
return self.markdown.htmlStash.store(self.unescape(element))
The key solution is in the last line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With