Repository: Python-Markdown/markdown (python)

Issue:
Newlines not treated as whitespace in emphasis pattern
The following input is correctly not a list as there is no blank line between the first and second lines:

```plaintext
not a list
* list item 1
* list item 2
* list item 3
```

However, now that we have list-like syntax, the inline patterns make a mess of it and we get this output:

```html
<p>not a list
<em> list item 1
</em> list item 2
* list item 3</p>
```

It seems that `* list item 1\n*` is incorrectly being identified as `em`. However, the reference implementation renders that as:

```html
<p>not a list
* list item 1
* list item 2
* list item 3</p>
````

See [Babelmark](http://johnmacfarlane.net/babelmark2/?text=not+a+list%0A*+list+item+1%0A*+list+item+2%0A*+list+item+3) for comparison. 

If we remove the newlines:

```plaintext
not a list * list item 1 * list item 2 * list item 3
```

Then things work as they should (see [Babelmark](http://johnmacfarlane.net/babelmark2/?normalize=1&text=not+a+list+*+list+item+1+*+list+item+2+*+list+item+3)):

```html
<p>not a list * list item 1 * list item 2 * list item 3</p>
```

Which suggests that the problem is that we are not properly accounting for newlines as being whitespace somewhere in an inline pattern.

Note that Commonmark differs from Markdown here in that it recognizes the list without the blank line. In fact, the Commonmark spec [acknowledges that this behavior is different][1] from Markdown. This issue is not about that. See #338 for that discussion. Please keep this discussion on point.

Finally, this issue was first reported on [StackOverflow](https://stackoverflow.com/q/54529652/866026) by user [Minato](https://stackoverflow.com/users/3960567/minato).

[1]: https://spec.commonmark.org/0.28/#example-266

Pull request:
Emphasis treat newlines as whitespace
All whitespace characters should be treated the same by inline patterns.
Previoulsy, emphasis patterns were only accounting for spaces, but not
other whitepsace characters such as newlines. Fixes #783.

Patch:
diff --git a/docs/change_log/release-3.1.md b/docs/change_log/release-3.1.md
index 5c225cf..c5f0433 100644
--- a/docs/change_log/release-3.1.md
+++ b/docs/change_log/release-3.1.md
@@ -31,6 +31,7 @@ The following new features have been included in the release:
 
 The following bug fixes are included in the 3.1 release:
 
+* Emphasis patterns now recognize newline characters as whitespace (#783).
 * Version format had been updated to be PEP 440 compliant (#736).
 * Block level elements are defined per instance, not as class attributes
   (#731).
diff --git a/markdown/inlinepatterns.py b/markdown/inlinepatterns.py
index dc502c2..88e8ead 100644
--- a/markdown/inlinepatterns.py
+++ b/markdown/inlinepatterns.py
@@ -144,7 +144,7 @@ REFERENCE_RE = LINK_RE
 IMAGE_REFERENCE_RE = IMAGE_LINK_RE
 
 # stand-alone * or _
-NOT_STRONG_RE = r'((^| )(\*|_)( |$))'
+NOT_STRONG_RE = r'((^|\s)(\*|_)(\s|$))'
 
 # <http://www.123.com>
 AUTOLINK_RE = r'<((?:[Ff]|[Hh][Tt])[Tt][Pp][Ss]?://[^>]*)>'


Test patch:
diff --git a/tests/test_syntax/inline/test_emphasis.py b/tests/test_syntax/inline/test_emphasis.py
new file mode 100644
index 0000000..d96861b
--- /dev/null
+++ b/tests/test_syntax/inline/test_emphasis.py
@@ -0,0 +1,86 @@
+# -*- coding: utf-8 -*-
+"""
+Python Markdown
+
+A Python implementation of John Gruber's Markdown.
+
+Documentation: https://python-markdown.github.io/
+GitHub: https://github.com/Python-Markdown/markdown/
+PyPI: https://pypi.org/project/Markdown/
+
+Started by Manfred Stienstra (http://www.dwerg.net/).
+Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
+Currently maintained by Waylan Limberg (https://github.com/waylan),
+Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).
+
+Copyright 2007-2019 The Python Markdown Project (v. 1.7 and later)
+Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
+Copyright 2004 Manfred Stienstra (the original version)
+
+License: BSD (see LICENSE.md for details).
+"""
+
+from markdown.test_tools import TestCase
+
+
+class TestNotEmphasis(TestCase):
+
+    def test_standalone_asterisk(self):
+        self.assertMarkdownRenders(
+            '*',
+            '<p>*</p>'
+        )
+
+    def test_standalone_understore(self):
+        self.assertMarkdownRenders(
+            '_',
+            '<p>_</p>'
+        )
+
+    def test_standalone_asterisk_in_text(self):
+        self.assertMarkdownRenders(
+            'foo * bar',
+            '<p>foo * bar</p>'
+        )
+
+    def test_standalone_understore_in_text(self):
+        self.assertMarkdownRenders(
+            'foo _ bar',
+            '<p>foo _ bar</p>'
+        )
+
+    def test_standalone_asterisks_in_text(self):
+        self.assertMarkdownRenders(
+            'foo * bar * baz',
+            '<p>foo * bar * baz</p>'
+        )
+
+    def test_standalone_understores_in_text(self):
+        self.assertMarkdownRenders(
+            'foo _ bar _ baz',
+            '<p>foo _ bar _ baz</p>'
+        )
+
+    def test_standalone_asterisks_with_newlines(self):
+        self.assertMarkdownRenders(
+            'foo\n* bar *\nbaz',
+            '<p>foo\n* bar *\nbaz</p>'
+        )
+
+    def test_standalone_understores_with_newlines(self):
+        self.assertMarkdownRenders(
+            'foo\n_ bar _\nbaz',
+            '<p>foo\n_ bar _\nbaz</p>'
+        )
+
+    def test_standalone_asterisks_at_end(self):
+        self.assertMarkdownRenders(
+            'foo * bar *',
+            '<p>foo * bar *</p>'
+        )
+
+    def test_standalone_understores_at_begin_end(self):
+        self.assertMarkdownRenders(
+            '_ bar _',
+            '<p>_ bar _</p>'
+        )