I'm using python markdown as a filter for Jinja2 to generate html. As part of that, I fill in table entries from the render input. Passing the input through the markdown filter always wraps the text in paragraph tags, and thus every entry in the table is wrapped in <p></p>, which I don't want.

I've read through the markdown docs and the 3rd party extension lists, but it appears there's no way to suppress this behavior other than writing my own extension. Is there no other way to suppress the paragraph tag wrapping? Or am I going about this the wrong way?

Update: Here's the filthy dirty awful hack I'm using for now:

def safe_markdown(text):
  p = '<p>'
  np = '</p>'
  md = markdown.markdown(text)
  if md.startswith(p) and md.endswith(np): #you filthy bastard
    md = md[len(p):-len(np)]
  return jinja2.Markup(md)

env = jinja2.Environment(...)
env.filters['markdown'] = safe_markdown 

Update 2 (Response to Aaron's answer):

Appreciate the help, but it's definitely the markdown causing the problem. Here's an example portion of the jinja template:

        {%- if spc.docs -%}
<td>{{ spc.docs|markdown }}</td></tr>
        {%- else -%}
<td></td></tr>
        {%- endif -%}

If spc.docs is simply 'foo' the generated html will wind up as <td><p>foo</p></td></tr> unless I use the filthy hack.

Update 3

Here's a less nasty hack, although still a hack and not really an 'answer', IMO.

def safe_markdown(text):
    md = markdown.markdown(text)
    return jinja2.Markup(md)

def safe_markdown_td(text):
    text = ''.join(['<td>', text, '</td>'])
    return safe_markdown(text)

env = jinja2.Enviro
Edit
Report