title: Get python-markdown extension to output html tags with a specific class attribute in Django?
body: <p>I initially thought that <a href="https://stackoverflow.com/questions/3853620/how-to-pass-class-attribute-and-value-to-markdown-syntax">how to pass class attribute and value to markdown syntax</a> and <a href="https://stackoverflow.com/questions/975264/how-do-i-set-an-html-class-attribute-in-markdown">How do I set an HTML class attribute in Markdown?</a> asked the question that I want to ask here, but I want to know how to write an python-markdown <strong>extension</strong> that outputs an html span element with specific class attribute in Django.</p>
  
  <p>I've written the following extension (file saved as "mdx_debatesyntax.py"):</p>
  
  <pre><code>import markdown
  from markdown.inlinepatterns import Pattern
  
  PREMISE_RE = r'(\[p )(.*?)\]'
  
  class AttrTagPattern(Pattern):
      """
      Return element of type `tag` with a text attribute of group(3) 
      of a Pattern and with the html attributes defined with the constructor.
  
      """
      def __init__ (self, pattern, tag, attrs):
          Pattern.__init__(self, pattern)
          self.tag = tag
          self.attrs = attrs
  
      def handleMatch(self, m):
          el = markdown.util.etree.Element(self.tag)
          el.text = m.group(3)
          for (key,val) in self.attrs.items():
              el.set(key,val)
          return el
  
  class DebateSyntaxExtension(markdown.Extension):
      def extendMarkdown(self, md, md_globals):
          premise_tag = AttrTagPattern(PREMISE_RE, 'span',{'class':'premise'})
          md.inlinePatterns.add('premise', premise_tag, '_begin')
  
  def makeExtension(configs=None):
      return DebateSyntaxExtension(configs=configs)
  </code></pre>
  
  <p>And when I test this directly (not through Django) it works:</p>
  
  <pre><code>import markdown
  md = markdown.Markdown(extensions=['debatesyntax'])
  md.convert('This is a [p test]!')
  </code></pre>
  
  <p>Returns </p>
  
  <pre><code>u'&lt;p&gt;This is a &lt;span class="premise"&gt;test&lt;/span&gt;!&lt;/p&gt;'
  </code></pre>
  
  <p>However, when I run this through Django, the attribute class gets stripped, giving me:</p>
  
  <pre><code>&lt;p&gt;This is a &lt;span&gt;test&lt;/span&gt;!&lt;/p&gt;
  </code></pre>
  
  <p>How do I prevent the class attribute (or other attributes) from being stripped?</p>
  
  <p><strong>Resolution</strong></p>
  
  <p>The Django app that I was modifying (OSQA), was manually "sanitizing" the HTML. The AttrTagPattern class code does, after all, work.</p>