Disabling images in Python Markdown

Here’s a quick snippet that shows how to disable images (or any extension) in Python Markdown:

import markdown

class MyMarkdownExtension(markdown.Extension):

	def extendMarkdown(self, md, md_globals):
		del md.inlinePatterns['image_link']
		del md.inlinePatterns['image_reference']

my_markdown_extension = MyMarkdownExtension()

raw_text = 'This is a test image: ![Google Logo](http://www.google.com/images/logos/ps_logo2a_cp.png)'

print markdown.markdown(raw_text, [my_markdown_extension], safe_mode = 'escape')

Edit:
Some processing is disabled through md.parser.blockprocessors instead.

Here are the options for each:
md.inlinePatterns:

'backtick'
'escape'
'reference'
'link'
'image_link'
'image_reference'
'short_reference'
'autolink'
'automail'
'linebreak2'
'linebreak'
'html'
'entity'
'not_strong'
'strong_em'
'strong'
'emphasis'
'emphasis2'

md.parser.blockprocessors:

'indent'
'code'
'hashheader'
'setextheader'
'hr'
'olist'
'ulist'
'quote'
'paragraph'
About these ads
  1. #1 by guest on June 7, 2011 - 1:01 pm

    Thanks, very useful.

  2. #2 by guest on June 7, 2011 - 1:59 pm

    Just a little remark:
    On python-markdown 2.0.3 you should create Markdown object to pass your own extension.


    md = markdown.Markdown(extensions=[my_markdown_extension], safe_mode='escape')
    print md.convert(raw_text)

  3. #3 by Sean Fujiwara on June 7, 2011 - 10:34 pm

    Ahh, thanks. I should have mentioned that I was using python markdown 2.1.0.

  4. #4 by Patricia Juarez (@ccsakuweb) on August 18, 2011 - 1:08 am

    Thank you very muxh, it was useful. But I would like to disable more tags of markdown.
    I want a version extended and another one minified.
    The extended would be the normal markdown, but in the minified I don’t want to let code, hr, lists, quote, paragraphs.. you know how to disable them? It would be really useful. Please help.

    • #5 by Sean Fujiwara on August 18, 2011 - 1:57 am

      These elements appear to be in the md.parser.blockprocessors object. I’ll edit the post to include all the things you can disable.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: