Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get an empty tag using markdown.extensions.attr_list?

I am attempting to add microdata to my generated HTML and have found the attr_list extension. It does almost everything I need.

Here is the example code I'm using:

>>> text = """This is a paragraph.
... {: itemscope itemtype="http://schema.org/Movie"}
... """
>>> markdown.markdown(text, extensions=['markdown.extensions.attr_list'])
u'<p itemscope="itemscope" itemtype="http://schema.org/Movie">This is a paragraph.</p>'

The one problem I'm having is the itemscope="itemscope". According to the examples provided by schema.org, it should just be:

<p itemscope itemtype="http://schema.org/Movie">This is a paragraph.</p>

The closest I've gotten is

text = """This is a paragraph.
... {: itemscope="" itemtype="http://schema.org/Movie"}
... """

Which generates output as

u'<p itemscope="" itemtype="http://schema.org/Movie">This is a paragraph.</p>'

Is there a way to keep this as a naked tag (just itemscope without the equals) using this extension?

like image 272
Andy Avatar asked May 24 '26 02:05

Andy


1 Answers

Use the html output_format (which you probably want anyway), rather than the default xhtml format:

t = """This is a paragraph.
... { itemscope itemtype="http://schema.org/Movie"}
... """
>>> markdown.markdown(t, extensions=['attr_list'], output_format="html")
u'<p itemscope itemtype="http://schema.org/Movie">This is a paragraph.</p>'

As Markdown was first developed back when XHTML was the new hotness, the rules and reference implementation both expect XHTML formatted output. As Python-Markdown is an old-school Markdown parser, it also defaults to XHTML as the default output format (as the XHTML spec simply references the HTML4 spec, see that for details).

The non-default html output-format has more recently been updated to output HTML5 and uses the minimized form.

By the way, you don't need to include the colon in your attribute lists (see my example above). More recently it was made optional to be compatible with other implementations.

like image 160
Waylan Avatar answered May 27 '26 06:05

Waylan