? How to change the default behavior in the marking filter so that it conve...">

How to make django markdown filter convert carriage return to <br/">?

How to change the default behavior in the marking filter so that it converts a new line into a br tag?

+5
source share
4 answers

I don't think mess with newline syntax is a good idea ...

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

If you want to insert the <br />break tag with Markdown, you end the line with two or more spaces, then type return.

, , <br />, " - <br />" Markdown. Markdowns - - .

Django, ? .


, ...

linebreaksbr.

{{ value|markdown|linebreaksbr }}

, linebreaksbr, \n <br />. , , , , .

,

+5

EDIT: 2011 Python Markdown.

Markdown, , , :

"""
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)

mdx_nl2br.py PYTHONPATH. Django :

{{ value|markdown:"nl2br" }}

, - :

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

.

+5

You can write a custom filter that causes markdowns, and then replace on your output.

0
source

There seems to be a filter linebreaksthat converts characters \nto <br>or <p>.
See linebreaks or linebreaksbr .

0
source

All Articles