Repository: sphinx-gallery/sphinx-gallery

Issue:
check_duplicated does not respect the ignore_pattern
It seems that check_duplicate_filenames does not respect the ignore_pattern.

The proper fix would probably be to have collect_gallery_files respect the ignore_pattern.

Comments:

Patch:
diff --git a/sphinx_gallery/gen_gallery.py b/sphinx_gallery/gen_gallery.py
index 9d8aadd..1a93db0 100644
--- a/sphinx_gallery/gen_gallery.py
+++ b/sphinx_gallery/gen_gallery.py
@@ -258,7 +258,7 @@ def generate_gallery_rst(app):
 
     # Check for duplicate filenames to make sure linking works as expected
     examples_dirs = [ex_dir for ex_dir, _ in workdirs]
-    files = collect_gallery_files(examples_dirs)
+    files = collect_gallery_files(examples_dirs, gallery_conf)
     check_duplicate_filenames(files)
 
     for examples_dir, gallery_dir in workdirs:
@@ -549,14 +549,16 @@ def summarize_failing_examples(app, exception):
                          "\n" + "-" * 79)
 
 
-def collect_gallery_files(examples_dirs):
+def collect_gallery_files(examples_dirs, gallery_conf):
     """Collect python files from the gallery example directories."""
     files = []
     for example_dir in examples_dirs:
         for root, dirnames, filenames in os.walk(example_dir):
             for filename in filenames:
                 if filename.endswith('.py'):
-                    files.append(os.path.join(root, filename))
+                    if re.search(gallery_conf['ignore_pattern'],
+                                 filename) is None:
+                        files.append(os.path.join(root, filename))
     return files
 
 
diff --git a/sphinx_gallery/gen_rst.py b/sphinx_gallery/gen_rst.py
index 5d632a5..4100482 100644
--- a/sphinx_gallery/gen_rst.py
+++ b/sphinx_gallery/gen_rst.py
@@ -857,12 +857,13 @@ def save_rst_example(example_rst, example_file, time_elapsed,
 
     binder_text = (" or to run this example in your browser via Binder"
                    if len(binder_conf) else "")
-    example_rst = (".. note::\n"
-                   "    :class: sphx-glr-download-link-note\n\n"
-                   "    Click :ref:`here <sphx_glr_download_{0}>` "
-                   "to download the full example code{1}\n"
-                   ".. rst-class:: sphx-glr-example-title\n\n"
-                   ".. _sphx_glr_{0}:\n\n"
+    example_rst = (".. only:: html\n\n"
+                   "    .. note::\n"
+                   "        :class: sphx-glr-download-link-note\n\n"
+                   "        Click :ref:`here <sphx_glr_download_{0}>` "
+                   "    to download the full example code{1}\n"
+                   "    .. rst-class:: sphx-glr-example-title\n\n"
+                   "    .. _sphx_glr_{0}:\n\n"
                    ).format(ref_fname, binder_text) + example_rst
 
     if time_elapsed >= gallery_conf["min_reported_time"]:


Test patch:
diff --git a/sphinx_gallery/tests/test_gen_gallery.py b/sphinx_gallery/tests/test_gen_gallery.py
index 872bb83..9f5269d 100644
--- a/sphinx_gallery/tests/test_gen_gallery.py
+++ b/sphinx_gallery/tests/test_gen_gallery.py
@@ -281,7 +281,7 @@ def test_example_sorting_title(sphinx_app_wrapper):
     _check_order(sphinx_app, 'title')
 
 
-def test_collect_gallery_files(tmpdir):
+def test_collect_gallery_files(tmpdir, gallery_conf):
     """Test that example files are collected properly."""
     rel_filepaths = ['examples/file1.py',
                      'examples/test.rst',
@@ -298,7 +298,7 @@ def test_collect_gallery_files(tmpdir):
 
     examples_path = tmpdir.join('examples')
     dirs = [examples_path.strpath]
-    collected_files = set(collect_gallery_files(dirs))
+    collected_files = set(collect_gallery_files(dirs, gallery_conf))
     expected_files = set(
         [ap.strpath for ap in abs_paths
          if re.search(r'examples.*\.py$', ap.strpath)])
@@ -307,13 +307,35 @@ def test_collect_gallery_files(tmpdir):
 
     tutorials_path = tmpdir.join('tutorials')
     dirs = [examples_path.strpath, tutorials_path.strpath]
-    collected_files = set(collect_gallery_files(dirs))
+    collected_files = set(collect_gallery_files(dirs, gallery_conf))
     expected_files = set(
         [ap.strpath for ap in abs_paths if re.search(r'.*\.py$', ap.strpath)])
 
     assert collected_files == expected_files
 
 
+def test_collect_gallery_files_ignore_pattern(tmpdir, gallery_conf):
+    """Test that ignore pattern example files are not collected."""
+    rel_filepaths = ['examples/file1.py',
+                     'examples/folder1/fileone.py',
+                     'examples/folder1/file2.py',
+                     'examples/folder2/fileone.py']
+
+    abs_paths = [tmpdir.join(rp) for rp in rel_filepaths]
+    for ap in abs_paths:
+        ap.ensure()
+
+    gallery_conf['ignore_pattern'] = r'one'
+    examples_path = tmpdir.join('examples')
+    dirs = [examples_path.strpath]
+    collected_files = set(collect_gallery_files(dirs, gallery_conf))
+    expected_files = set(
+        [ap.strpath for ap in abs_paths
+         if re.search(r'one', ap.strpath) is None])
+
+    assert collected_files == expected_files
+
+
 @pytest.mark.conf_file(content="""
 sphinx_gallery_conf = {
     'backreferences_dir' : os.path.join('modules', 'gen'),
diff --git a/sphinx_gallery/tests/test_gen_rst.py b/sphinx_gallery/tests/test_gen_rst.py
index 911fc6c..d0d0602 100644
--- a/sphinx_gallery/tests/test_gen_rst.py
+++ b/sphinx_gallery/tests/test_gen_rst.py
@@ -417,6 +417,16 @@ def test_remove_config_comments(gallery_conf):
     assert '# sphinx_gallery_thumbnail_number = 1' not in rst
 
 
+def test_download_link_note_only_html(gallery_conf):
+    """Test html only directive for download_link."""
+    rst = _generate_rst(gallery_conf, 'test.py', CONTENT)
+    download_link_note = (".. only:: html\n\n"
+                          "    .. note::\n"
+                          "        :class: sphx-glr-download-link-note\n\n"
+                          )
+    assert download_link_note in rst
+
+
 @pytest.mark.parametrize('ext', ('.txt', '.rst', '.bad'))
 def test_gen_dir_rst(gallery_conf, fakesphinxapp, ext):
     """Test gen_dir_rst."""