andrewrk andrewrk - 4 years ago 128
Markdown Question

How do I get python-markdown to additionally "urlify" links when formatting plain text?

Markdown is a great tool for formatting plain text into pretty html, but it doesn't turn plain-text links into URLs automatically. Like this one:

How do I get markdown to add tags to URLs when I format a block of text?

Answer Source

I couldn't get superjoe30's regular expression to compile, so I adapted his solution to convert plain URLs (within Markdown text) to be Markdown compatible.

The modified filter:

urlfinder = re.compile('^(http:\/\/\S+)')
urlfinder2 = re.compile('\s(http:\/\/\S+)')
def urlify_markdown(value):
    value = urlfinder.sub(r'<\1>', value)
    return urlfinder2.sub(r' <\1>', value)

Within the template:

    {{ content|urlify_markdown|markdown}}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download