Repository: sphinx-gallery/sphinx-gallery

Issue:
Last word in rst used as code
If an example `.py` file ends with an rst block, Sphinx-Gallery thinks the last word of the rst block is code and tries to execute it, unless there is an empty line after the rst block.

For example:

```Python
"""
Test
####

test
"""

print('hello')

#%%
# This is a sentence.
```

Gives: 
![Screenshot_2019-10-09 Test — mwe 0 documentation](https://user-images.githubusercontent.com/23182829/66478789-7416ea00-ea9b-11e9-8324-593f335ca405.png)

If there is an empty line at the end, this does not happen. Is it expected to just add a new line to the end of the document?

MWE [here](https://github.com/lucyleeow/Sphinx-Gallery-MWE).



Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/sphinx-gallery/sphinx-gallery/pull/548?src=pr&el=h1) Report
> Merging [#548](https://codecov.io/gh/sphinx-gallery/sphinx-gallery/pull/548?src=pr&el=desc) into [master](https://codecov.io/gh/sphinx-gallery/sphinx-gallery/commit/a961d9bc83cd7b9c974f545da994c52d57781a10?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/sphinx-gallery/sphinx-gallery/pull/548/graphs/tree.svg?width=650&token=K4eytvhwU5&height=150&src=pr)](https://codecov.io/gh/sphinx-gallery/sphinx-gallery/pull/548?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #548      +/-   ##
==========================================
+ Coverage   97.44%   97.45%   +<.01%     
==========================================
  Files          29       29              
  Lines        2817     2826       +9     
==========================================
+ Hits         2745     2754       +9     
  Misses         72       72
```


| [Impacted Files](https://codecov.io/gh/sphinx-gallery/sphinx-gallery/pull/548?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [sphinx\_gallery/py\_source\_parser.py](https://codecov.io/gh/sphinx-gallery/sphinx-gallery/pull/548/diff?src=pr&el=tree#diff-c3BoaW54X2dhbGxlcnkvcHlfc291cmNlX3BhcnNlci5weQ==) | `98.86% <ø> (ø)` | :arrow_up: |
| [sphinx\_gallery/tests/test\_gen\_rst.py](https://codecov.io/gh/sphinx-gallery/sphinx-gallery/pull/548/diff?src=pr&el=tree#diff-c3BoaW54X2dhbGxlcnkvdGVzdHMvdGVzdF9nZW5fcnN0LnB5) | `98.83% <100%> (+0.04%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/sphinx-gallery/sphinx-gallery/pull/548?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/sphinx-gallery/sphinx-gallery/pull/548?src=pr&el=footer). Last update [a961d9b...931929b](https://codecov.io/gh/sphinx-gallery/sphinx-gallery/pull/548?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/README.rst b/README.rst
index 05d8af1..4ebf361 100644
--- a/README.rst
+++ b/README.rst
@@ -19,14 +19,14 @@ script and puts it into an examples gallery.
 Who uses Sphinx-Gallery
 =======================
 
-* `Sphinx-Gallery <https://sphinx-gallery.github.io/en/latest/auto_examples/index.html>`_
+* `Sphinx-Gallery <https://sphinx-gallery.github.io/auto_examples/index.html>`_
 * `Scikit-learn <http://scikit-learn.org/dev/auto_examples/index.html>`_
 * `Nilearn <https://nilearn.github.io/auto_examples/index.html>`_
 * `MNE-python <https://www.martinos.org/mne/stable/auto_examples/index.html>`_
 * `PyStruct <https://pystruct.github.io/auto_examples/index.html>`_
 * `GIMLi <http://www.pygimli.org/_examples_auto/index.html>`_
 * `Nestle <https://kbarbary.github.io/nestle/examples/index.html>`_
-* `pyRiemann <https://pythonhosted.org/pyriemann/auto_examples/index.html>`_
+* `pyRiemann <https://pyriemann.readthedocs.io/en/latest/index.html>`_
 * `scikit-image <http://scikit-image.org/docs/dev/auto_examples/>`_
 * `Astropy <http://docs.astropy.org/en/stable/generated/examples/index.html>`_
 * `SunPy <http://docs.sunpy.org/en/stable/generated/gallery/index.html>`_
@@ -36,7 +36,7 @@ Who uses Sphinx-Gallery
 * `Cartopy <http://scitools.org.uk/cartopy/docs/latest/gallery/>`_
 * `PyVista <https://docs.pyvista.org/examples/>`_
 * `SimPEG <http://docs.simpeg.xyz/content/examples/>`_
-* `PlasmaPy <http://docs.plasmapy.org/en/master/auto_examples/>`_
+* `PlasmaPy <http://docs.plasmapy.org/en/latest/auto_examples/>`_
 * `Fury <http://fury.gl/latest/auto_examples/index.html>`_
 
 Installation
diff --git a/doc/index.rst b/doc/index.rst
index 5e98e0a..48d7091 100644
--- a/doc/index.rst
+++ b/doc/index.rst
@@ -96,6 +96,7 @@ Sphinx-Gallery Show: :ref:`examples-index`
    getting_started
    syntax
    configuration
+   projects_list
 
 .. toctree::
    :maxdepth: 2
diff --git a/doc/projects_list.rst b/doc/projects_list.rst
new file mode 100644
index 0000000..8f451f3
--- /dev/null
+++ b/doc/projects_list.rst
@@ -0,0 +1,27 @@
+=======================
+Who uses Sphinx-Gallery
+=======================
+
+Here is a list of projects using `sphinx-gallery`.
+
+* `Sphinx-Gallery <https://sphinx-gallery.github.io/auto_examples/index.html>`_
+* `Scikit-learn <http://scikit-learn.org/dev/auto_examples/index.html>`_
+* `Nilearn <https://nilearn.github.io/auto_examples/index.html>`_
+* `MNE-python <https://www.martinos.org/mne/stable/auto_examples/index.html>`_
+* `PyStruct <https://pystruct.github.io/auto_examples/index.html>`_
+* `GIMLi <http://www.pygimli.org/_examples_auto/index.html>`_
+* `Nestle <https://kbarbary.github.io/nestle/examples/index.html>`_
+* `pyRiemann <https://pyriemann.readthedocs.io/en/latest/index.html>`_
+* `scikit-image <http://scikit-image.org/docs/dev/auto_examples/>`_
+* `Astropy <http://docs.astropy.org/en/stable/generated/examples/index.html>`_
+* `SunPy <http://docs.sunpy.org/en/stable/generated/gallery/index.html>`_
+* `PySurfer <https://pysurfer.github.io/>`_
+* `Matplotlib <https://matplotlib.org/index.html>`_ `Examples <https://matplotlib.org/gallery/index.html>`_ and `Tutorials  <https://matplotlib.org/tutorials/index.html>`__
+* `PyTorch tutorials <http://pytorch.org/tutorials>`_
+* `Cartopy <http://scitools.org.uk/cartopy/docs/latest/gallery/>`_
+* `PyVista <https://docs.pyvista.org/examples/>`_
+* `SimPEG <http://docs.simpeg.xyz/content/examples/>`_
+* `PlasmaPy <http://docs.plasmapy.org/en/latest/auto_examples/>`_
+* `Fury <http://fury.gl/latest/auto_examples/index.html>`_
+* `NetworkX <https://networkx.github.io/documentation/stable/auto_examples/index.html>`_
+
diff --git a/sphinx_gallery/py_source_parser.py b/sphinx_gallery/py_source_parser.py
index 83b43a1..5241723 100644
--- a/sphinx_gallery/py_source_parser.py
+++ b/sphinx_gallery/py_source_parser.py
@@ -175,7 +175,7 @@ def split_code_and_text_blocks(source_file, return_node=False):
     file_conf = extract_file_config(rest_of_content)
 
     pattern = re.compile(
-        r'(?P<header_line>^#{20,}.*|^# ?%%.*)\s(?P<text_content>(?:^#.*\s)*)',
+        r'(?P<header_line>^#{20,}.*|^# ?%%.*)\s(?P<text_content>(?:^#.*\s?)*)',
         flags=re.M)
     sub_pat = re.compile('^#', flags=re.M)
 


Test patch:
diff --git a/sphinx_gallery/tests/test_gen_rst.py b/sphinx_gallery/tests/test_gen_rst.py
index b5cda51..3fba350 100644
--- a/sphinx_gallery/tests/test_gen_rst.py
+++ b/sphinx_gallery/tests/test_gen_rst.py
@@ -103,6 +103,25 @@ def test_direct_comment_after_docstring():
     assert result == expected_result
 
 
+def test_final_rst_last_word(tmpdir):
+    """Tests last word in final rst block included as text"""
+    filename = str(tmpdir.join('temp.py'))
+    with open(filename, 'w') as f:
+        f.write('\n'.join(['"Docstring"',
+                           '# comment only code block',
+                           '#%%',
+                           '# Include this whole sentence.']))
+
+    file_conf, result = sg.split_code_and_text_blocks(f.name)
+
+    assert file_conf == {}
+    expected_result = [
+        ('text', 'Docstring', 1),
+        ('code', '# comment only code block\n', 2),
+        ('text', 'Include this whole sentence.', 4)]
+    assert result == expected_result
+
+
 def test_rst_block_after_docstring(gallery_conf, tmpdir):
     """Assert there is a blank line between the docstring and rst blocks."""
     filename = str(tmpdir.join('temp.py'))