|
From: Waylan L. <wa...@gm...> - 2012-01-18 17:27:48
|
On Wed, Jan 18, 2012 at 11:43 AM, andrew thornton
<ath...@gm...> wrote:
> 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:
>
[snip]
> 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
Your problem is in the last few lines above. Inlinepatterns must
return ElementTree instances, not strings. Therefore, you need to
build a ET Element (or tree of elements) and return that. For example
consider this abbreviated rewrite of your code:
if images:
# build and return gallery here
else:
b = markdown.util.etree.Element('b')
b.text = 'There are no images for the given ID'
return b
Of course, this means no Django templates for your gallery without a
few workarounds.
A few approaches you might want to look at:
1) Follow the method used by the CodeHilite Extension and save the
html output in the htmlStash (marked as 'safe' so is doesn't get eat
by safe_mode) and return the placeholder. Then the placeholder will be
automatically replaced with the html after the Tree is searialized
into a string. See the code here [1].
2) Parse the html into a ET Tree and return it. But be careful here,
most parsers (including ElementTree's) fall flat on there face with
the simplest of html syntax errors. I would suggest lxml or html5lib,
both of which adds an extra dependency (lxml is the best IMO, but
requires a C lib). See this excellent comparison [2] of the options.
Just make sure the one you use will build an ET Tree.
[1]: https://github.com/waylan/Python-Markdown/blob/master/markdown/extensions/codehilite.py#L184
[2]: http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/
--
----
\X/ /-\ `/ |_ /-\ |\|
Waylan Limberg
|