将 github 风格的 markdown 正则表达式从 ruby​​ 转换为 python

发布于 2024-10-14 14:58:44 字数 1325 浏览 1 评论 0原文

我正在尝试在 python 中实现 github 风格的 markdown,但没有运气......我没有太多的正则表达式技能。

以下是来自 github 的 ruby​​ 代码:

# in very clear cases, let newlines become <br /> tags
text.gsub!(/(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+/m) do |x|
  x.gsub(/^(.+)$/, "\\1  ")
end

这是我迄今为止在 python 2.5 中提出的:

def newline_callback(matchobj):
    return re.sub(r'^(.+)$','\1 ',matchobj.group(0))     
text = re.sub(r'(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+', newline_callback, text)

似乎根本没有任何效果:-/

如果有人有一个完全有效的 Python 中的 github 风格的 markdown,除了 这个(似乎不适用于换行符),我很想听听。我真的最关心新线。

这些是来自 github 的 ruby​​ 代码的正则表达式测试:

>>> gfm_pre_filter('apple\\npear\\norange\\n\\nruby\\npython\\nerlang')
'apple  \\npear  \\norange\\n\\nruby  \\npython  \\nerlang'
>>> gfm_pre_filter('test \\n\\n\\n something')
'test \\n\\n\\n something'
>>> gfm_pre_filter('# foo\\n# bar')
'# foo\\n# bar'
>>> gfm_pre_filter('* foo\\n* bar')
'* foo\\n* bar'

I'm trying to get an implementation of github flavored markdown working in python, with no luck... I don't have much in the way of regex skills.

Here's the ruby code from github:

# in very clear cases, let newlines become <br /> tags
text.gsub!(/(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+/m) do |x|
  x.gsub(/^(.+)$/, "\\1  ")
end

And here's what I've come up with so far in python 2.5:

def newline_callback(matchobj):
    return re.sub(r'^(.+)

There just doesn't seem to be any effect at all :-/

If anyone has a fully working implementation of github flavored markdown in python, other than this one (doesn't seem to work for newlines), I'd love to hear about it. I'm really most concerned about the newlines.

These are the tests for the regex, from github's ruby code:

>>> gfm_pre_filter('apple\\npear\\norange\\n\\nruby\\npython\\nerlang')
'apple  \\npear  \\norange\\n\\nruby  \\npython  \\nerlang'
>>> gfm_pre_filter('test \\n\\n\\n something')
'test \\n\\n\\n something'
>>> gfm_pre_filter('# foo\\n# bar')
'# foo\\n# bar'
>>> gfm_pre_filter('* foo\\n* bar')
'* foo\\n* bar'
,'\1 ',matchobj.group(0)) text = re.sub(r'(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+', newline_callback, text)

There just doesn't seem to be any effect at all :-/

If anyone has a fully working implementation of github flavored markdown in python, other than this one (doesn't seem to work for newlines), I'd love to hear about it. I'm really most concerned about the newlines.

These are the tests for the regex, from github's ruby code:

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

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

发布评论

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

评论(2

快乐很简单 2024-10-21 14:58:44

该 Ruby 版本在正则表达式中具有多行修饰符,因此您需要在 python 中执行相同操作:

def newline_callback(matchobj):
    return re.sub(re.compile(r'^(.+)

这样代码将(与 Ruby 版本一样)在换行符之后添加两个空格,除非我们有两个换行符(段落)。

您提供的那些测试字符串正确吗?您链接的文件有这个,它适用于该固定代码:

"apple\npear\norange\n\nruby\npython\nerlang"
->
"apple  \npear  \norange\n\nruby  \npython  \nerlang"
, re.M),r'\1 ',matchobj.group(0)) text = re.sub(re.compile(r'(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+', re.M), newline_callback, text)

这样代码将(与 Ruby 版本一样)在换行符之后添加两个空格,除非我们有两个换行符(段落)。

您提供的那些测试字符串正确吗?您链接的文件有这个,它适用于该固定代码:

That Ruby version has multiline modifier in the regex, so you need to do the same in python:

def newline_callback(matchobj):
    return re.sub(re.compile(r'^(.+)

So that code will (like the Ruby version) add two spaces after before newline, except if we have two newlines (paragraph).

Are those test string you gave correct? That file you linked has this, and it works with that fixed code:

"apple\npear\norange\n\nruby\npython\nerlang"
->
"apple  \npear  \norange\n\nruby  \npython  \nerlang"
, re.M),r'\1 ',matchobj.group(0)) text = re.sub(re.compile(r'(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+', re.M), newline_callback, text)

So that code will (like the Ruby version) add two spaces after before newline, except if we have two newlines (paragraph).

Are those test string you gave correct? That file you linked has this, and it works with that fixed code:

岁月静好 2024-10-21 14:58:44
return re.sub(r'^(.+)
,r'\1 ',matchobj.group(0))
                       ^^^--------------------------- you forgot this. 
return re.sub(r'^(.+)
,r'\1 ',matchobj.group(0))
                       ^^^--------------------------- you forgot this. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文