|
From: Waylan L. <wa...@gm...> - 2012-01-18 19:33:58
|
On Wed, Jan 18, 2012 at 2:28 PM, andrew thornton
<ath...@gm...> wrote:
> On Wed, Jan 18, 2012 at 2:03 PM, Waylan Limberg <wa...@gm...> wrote:
>> On Wed, Jan 18, 2012 at 1:12 PM, andrew thornton
>> <ath...@gm...> wrote:
>>>
>>> Thank you for your reply,
>>>
>>> I have been trying to use htmlStash within the Galleria class, but
>>> this isn't working. I have however been able to use ET to return the
>>> errors. Here is what I was trying:
>>>
>>> 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:
>>> placeholder =
>>> self.markdown.htmlStash.store(render_to_string('galleria.html', {
>>> 'images': images }))
>>> block.clear()
>>> block.tag = 'p'
>>> block.text = placeholder
>>> else:
>>> b = markdown.util.etree.Element('b')
>>> b.text = 'There are no images for the given ID'
>>> return b
>>>
>>>
>>> Should I be using markdown.treeprocessors.Treeprocessor instead of the
>>> inlinepatterns? Can I use it in this way?
>>>
>>
>> You're not returning the placeholder. Try removing the 3 lines with
>> "block" in them and add `return placeholder` in their place. I'd also
>> suggest adding `safe=True` to the call to `htmlStash.store`. Like
>> this:
>>
>> if images:
>> placeholder = self.markdown.htmlStash.store(
>> render_to_string('galleria.html', {'images': images }),
>> safe=True
>> )
>> return placeholder
>> else:
>> b = markdown.util.etree.Element('b')
>> b.text = 'There are no images for the given ID'
>> return b
>>
>> Perhaps I should have pointed out that the CodeHilite extension uses a
>> Blockprocessor, thus the access to the parent "block". That won't work
>> in an inlinepattern (which has no access to the parent). However, if
>> your custom markup is the only thing in a paragraph (on a line by
>> itself), just return the placeholder and you should be done (see below
>> for why). If that doesn't work for you then either a Blockprocessor or
>> Treeprocessor would be the way to go - although it will take more work
>> (code) on your part to detect your custom markup.
>>
>> As a side note, when markdown swaps the htmlStash back in, if a
>> paragraph contains only a placeholder (with nothing else, not even
>> white space, i.e., `<p>some_placeholder</p>`) _and_ the stashed html
>> is block level, then the entire paragraph (`<p>` tag and all) gets
>> replaced with the stashed html. However, in all other situations, only
>> the placeholder itself gets replaced with the stashed html. If your
>> gallery is wrapped in div tags, you probably want the first behavior.
>> Perhaps the relevant code [1] will help you understand what's gong on.
>>
>> [1]: https://github.com/waylan/Python-Markdown/blob/master/markdown/postprocessors.py#L60
>>
>> --
>> ----
>> \X/ /-\ `/ |_ /-\ |\|
>> Waylan Limberg
>
> This worked great. Thank you for your help.
Your welcome.
--
----
\X/ /-\ `/ |_ /-\ |\|
Waylan Limberg
|