You're viewing all posts tagged with markdown

Flavoured Markdown

Markdown is good, but it has 2 features that’s hard to explain to inexperienced user:

  • it doesn’t convert urls to links automatically
  • it doesn’t convert line breaks to html <br> tags

It seems, for 80% cases this would better be done. Python Markdown supports extensions and thus can be “flavoured”.

So, there are extensaions:

Comments: 45

Automatically converting urls to html links

Here’s simple Python snippet for converting urls to links in single text line:

import re

link_re = re.compile('(\s+\(?|^)((http|ftp|https)://[-\w\#$%&~/.;:=,?@+]+)', re.IGNORECASE)

def autolinks(line):
    return link_re.sub(r'\1<a href="\2" target="_blank">\2</a>', line)

It’s simple yet smart enough:

  • doesn’t touch urls inside html links <a href=”some url”>some text</a>
  • can be used with Markdown, as it doesn’t touch Markdown links

To enable auto links replacement in Markdown, you may use autolink extension.

Tags: python markdown html  
Comments: 45