本文介绍了如何使python-markdown另外“urlify”格式化纯文本时的链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Markdown是将纯文本格式化为漂亮的HTML的好工具,但不会将纯文本链接自动转换为URL。像这样:
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?
推荐答案
我无法获取superjoe30的正则表达式进行编译,所以我调整了他的解决方案,将纯文本(在Markdown文本中)转换为与Markdown兼容。
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.
修改的过滤器:
urlfinder = re.compile('^(http:\/\/\S+)')
urlfinder2 = re.compile('\s(http:\/\/\S+)')
@register.filter('urlify_markdown')
def urlify_markdown(value):
value = urlfinder.sub(r'<\1>', value)
return urlfinder2.sub(r' <\1>', value)
在模板中:
<div>
{{ content|urlify_markdown|markdown}}
</div>
这篇关于如何使python-markdown另外“urlify”格式化纯文本时的链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!