Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:57:11+00:00 2026-06-01T08:57:11+00:00

I initially thought that how to pass class attribute and value to markdown syntax

  • 0

I initially thought that how to pass class attribute and value to markdown syntax and How do I set an HTML class attribute in Markdown? asked the question that I want to ask here, but I want to know how to write an python-markdown extension that outputs an html span element with specific class attribute in Django.

I’ve written the following extension (file saved as “mdx_debatesyntax.py”):

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)

And when I test this directly (not through Django) it works:

import markdown
md = markdown.Markdown(extensions=['debatesyntax'])
md.convert('This is a [p test]!')

Returns

u'<p>This is a <span class="premise">test</span>!</p>'

However, when I run this through Django, the attribute class gets stripped, giving me:

<p>This is a <span>test</span>!</p>

How do I prevent the class attribute (or other attributes) from being stripped?

Resolution

The Django app that I was modifying (OSQA), was manually “sanitizing” the HTML. The AttrTagPattern class code does, after all, work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:57:12+00:00Added an answer on June 1, 2026 at 8:57 am

    try

    {{ value|markdown:"debatesyntax" }}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I initially thought that 64 bit instructions would not work on OS-X 10.5. I
Initially i thought to do something like: #EXIT CODES class ExitCode(object): (USERHOME_INVALID, \ USERHOME_CANNOT_WRITE,
For example: a) int [x][y][z] vs b) int[x*y*z] Initially thought I'd go with a)
Initially I thought this was going to work, but now I understand it won't
I'm still not clear on the purpose of annotations in Java. Initially I thought
My animationDidStop method is not being called for some reason, initially I thought it
Initially I had a method in our DL that would take in the object
I was under the impression that the format() function of the PHP DateTime class
I trying to get a value off a spinner and then pass it onto
i see that asp.net mvc 2 has strongly typed helped and looking initially at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.