markdown不能用于其他空格?

时间:2018-06-26 08:25:28

标签: regex python-3.x markdown

在尝试将md转换为html文件时,我正在使用此正则表达式替换以\n\n\t结尾的所有匹配代码。问题是使用search()时是否匹配,而使用sub()时却不匹配。需要帮助。

import markdown2
import re

code_text = """* But why did the `is` operator evaluated to `False`? Let's see with this snippet.
  ```py
  class WTF(object):
    def __init__(self): print("I ")
    def __del__(self): print("D ")
  ```

  **Output:**"""

print("PROBLEM :: \n", markdown2.markdown(code_text))

pattern = re.compile(r"```py(?P<code>.*?)```", re.DOTALL)

code_only = pattern.search(code_text).group("code")

code_only = code_only.replace("\n", "\n\t")

print("SOLUTION 1:: \n", markdown2.markdown(code_only))

###################################################
# Replacing all code occurrences in the given string

code_only = pattern.sub(lambda m: (m.group("code")).replace("\n", "\n\t"), code_text)

print("SOLUTION 2:: \n", markdown2.markdown(code_only))

输出:

PROBLEM :: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.
<code>py
class WTF(object):
def __init__(self): print("I ")
def __del__(self): print("D ")
</code></p>

<p><strong>Output:</strong></p></li>
</ul>

SOLUTION 1:: 
 <pre><code>  class WTF(object):
    def __init__(self): print("I ")
    def __del__(self): print("D ")
</code></pre>

SOLUTION 2:: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.</p>

<p>class WTF(object):
    def <strong>init</strong>(self): print("I ")
    def <strong>del</strong>(self): print("D ")</p>

<p><strong>Output:</strong></p></li>
</ul>

尝试将code_text更改为

```py
>>> a = "some_string"
>>> id(a)
140420665652016
>>> id("some" + "_" + "string") # Notice that both the ids are same.
140420665652016
```
### This is a heading
```py
print("Hello World")
```

它运行完美。

输出:

PROBLEM :: 

 <p>```py</p>

<blockquote>
  <blockquote>
    <blockquote>
      <p>a = "some<em>string"
      id(a)
      140420665652016
      id("some" + "</em>" + "string") # Notice that both the ids are same.
      140420665652016
      ```</p>

      <h3>This is a heading</h3>
    </blockquote>
  </blockquote>
</blockquote>

<p><code>py
print("Hello World")
</code></p>

SOLUTION 1:: 
 <pre><code>&gt;&gt;&gt; a = "some_string"
&gt;&gt;&gt; id(a)
140420665652016
&gt;&gt;&gt; id("some" + "_" + "string") # Notice that both the ids are same.
140420665652016
</code></pre>

SOLUTION 2:: 
 <pre><code>&gt;&gt;&gt; a = "some_string"
&gt;&gt;&gt; id(a)
140420665652016
&gt;&gt;&gt; id("some" + "_" + "string") # Notice that both the ids are same.
140420665652016
</code></pre>

<h3>This is a heading</h3>

<pre><code>print("Hello World")
</code></pre>

编辑1

尝试使用Pypi降价

输出:

PROBLEM :: 
 <ul>
<li><p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.
<code>py
class WTF(object):
def __init__(self): print("I ")
def __del__(self): print("D ")
</code></p>

<p><strong>Output:</strong></p></li>
</ul>

SOLUTION 1:: 
 <pre><code>  class WTF(object):
    def __init__(self): print("I ")
    def __del__(self): print("D ")
</code></pre>

SOLUTION 2:: 
 <ul>
<li>
<p>But why did the <code>is</code> operator evaluated to <code>False</code>? Let's see with this snippet.</p>
<p>class WTF(object):
    def <strong>init</strong>(self): print("I ")
    def <strong>del</strong>(self): print("D ")</p>
</li>
</ul>
<p><strong>Output:</strong></p>

编辑2:

sub()工作正常。我刚刚确认了这一点,但是由于代码在每行的末尾已经有\n\t,所以在运行它之后它变成了\n\t\t,现在markdown()无法正常工作。

0 个答案:

没有答案