Hobhouse
Hobhouse

Reputation: 16283

How do I make django's markdown filter transform a carriage return to <br />?

How can I change the default behavior in the markdown filter so that it transforms a newline to a br tag?

Upvotes: 5

Views: 5818

Answers (5)

Andrei
Andrei

Reputation: 1373

You can override default MARKDOWN_DEUX_STYLES with adding "break-on-newline": True in extras settings :

MARKDOWN_DEUX_STYLES = {
    "default": {
        "extras": {
            "code-friendly": None,
            "break-on-newline": True,
        },
        "safe_mode": "escape",
    }
}

Documentation of python-markdown2:

break-on-newline: Replace single new line characters with
when True.

Upvotes: 1

Brian Neal
Brian Neal

Reputation: 32379

EDIT: As of the end of June, 2011, the extension below is now included with Python Markdown.

Here is a Markdown extension that I wrote and am currently testing on my site to do exactly what you want:

"""
A python-markdown extension to treat newlines as hard breaks; like
StackOverflow and GitHub flavored Markdown do.

"""
import markdown


BR_RE = r'\n'

class Nl2BrExtension(markdown.Extension):

    def extendMarkdown(self, md, md_globals):
        br_tag = markdown.inlinepatterns.SubstituteTagPattern(BR_RE, 'br')
        md.inlinePatterns.add('nl', br_tag, '_end')


def makeExtension(configs=None):
    return Nl2BrExtension(configs)

I put this in a file called mdx_nl2br.py and put it on my PYTHONPATH. You can then use it in a Django template like this:

{{ value|markdown:"nl2br" }}

If you'd like to use it in regular code, you can do something like this:

import markdown
md = markdown.Markdown(safe_mode=True, extensions=['nl2br'])
converted_text = md.convert(text)

Here is the starting point in the docs for using and writing extensions.

Upvotes: 5

Alasdair
Alasdair

Reputation: 308779

I don't think messing around with the newline syntax is a good idea ...

I agree with Henrik's comment. From the markdown docs:

When you do want to insert a <br /> break tag using Markdown, you end a line with two or more spaces, then type return.

Yes, this takes a tad more effort to create a <br />, but a simplistic “every line break is a <br />” rule wouldn’t work for Markdown. Markdown’s email-style blockquoting and multi-paragraph list items work best — and look better — when you format them with hard breaks.

Have you looked at the other Django markup options, textile and restructuredtext? Their syntax might suit you better.


but if you still want to ...

A rough and ready method is to chain the markdown and linebreaksbr filters.

{{ value|markdown|linebreaksbr }}

This runs the markdown filter, then the linebreaksbr filter, which replaces \n with <br />. You'll probably end up with too many linebreaks, but that might be better for you than too few.

If you a better solution than that, you could

  1. Write a custom filter, as John suggests in his answer.

  2. Dive into the the python-markdown library, which Django uses, and write an extension that implements your desired newline syntax. You would then use the extension with the filter

    {{ value|markdown:"linebreakextension" }}

Upvotes: 5

Simon Callan
Simon Callan

Reputation: 3130

There appears to be a linebreaks filter that converts \n characters to either <br> or <p>.
See linebreaks or linebreaksbr.

Upvotes: 0

John Paulett
John Paulett

Reputation: 15824

You could write a custom filter that calls markdown, then does replace on its output.

Upvotes: 0

Related Questions