如何使 django 的 markdown 过滤器将回车符转换为

发布于 2024-08-12 10:06:39 字数 47 浏览 5 评论 0 原文

如何更改 markdown 过滤器中的默认行为,以便将换行符转换为 br 标记?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

终止放荡 2024-08-19 10:06:39

我不认为搞乱换行语法是一个好主意......

我同意 Henrik 的评论。来自 markdown 文档

当您确实想使用 Markdown 插入
中断标记时,可以用两个或多个空格结束一行,然后输入 return。

是的,这需要更多的努力来创建
,但简单地说“每个换行符都是
” 规则不适用于 Markdown。当您使用硬中断来格式化 Markdown 的电子邮件样式块引用和多段落列表项时,效果最佳,而且看起来更好。

您是否看过其他 Django 标记选项、纺织和重组文本?它们的语法可能更适合您。


但如果你仍然想...

一个粗略且现成的方法是链接 markdown 和 linebreaksbr 过滤器。

{{ value|markdown|linebreaksbr }}

这会运行 markdown 过滤器,然后运行 ​​linebreaksbr 过滤器,它将 \n 替换为
。您可能会遇到太多的换行符,但这可能比太少的换行符对您更好。

如果您有更好的解决方案,您可以

  1. 编写一个自定义过滤器,正如约翰在他的回答中建议的那样。

  2. 深入了解 Django 使用的 python-markdown 库,以及< a href="http://www.freewisdom.org/projects/python-markdown/Writing_Extensions" rel="nofollow noreferrer">编写一个扩展来实现您所需的换行语法。然后,您可以将扩展与过滤器一起使用

    {{ value|markdown:"linebreakextension" }}

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

窝囊感情。 2024-08-19 10:06:39

编辑:截至 2011 年 6 月底,以下扩展现已包含在 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)

这是文档中使用和编写扩展的起点

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.

梦幻之岛 2024-08-19 10:06:39

您可以通过在 extras 设置中添加 "break-on-newline": True 来覆盖默认的 MARKDOWN_DEUX_STYLES:

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

python-markdown2 的文档

break-on-newline:将单个换行符替换为
确实如此。

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.

苍风燃霜 2024-08-19 10:06:39

您可以编写一个调用 markdown 的 自定义过滤器,然后对其输出进行替换

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

月下凄凉 2024-08-19 10:06:39

似乎有一个 linebreaks 过滤器,可将 \n 字符转换为

>.
请参阅 换行linebreaksbr

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文