|
From: andrew t. <ath...@gm...> - 2012-01-18 16:44:07
|
Hello,
I am kind of new to python, but I needed the ability to insert a
gallery into my django project. 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 django's 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.
Thanks
|