From b06f08f3a511365da68788486ae413b838425e74 Mon Sep 17 00:00:00 2001 From: Lucy Liu Date: Thu, 30 Apr 2020 17:28:07 +0200 Subject: [PATCH 1/6] add alt thumbnails --- sphinx_gallery/backreferences.py | 12 +++++++----- sphinx_gallery/gen_rst.py | 8 ++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/sphinx_gallery/backreferences.py b/sphinx_gallery/backreferences.py index ee10a782b..d2e6547c0 100644 --- a/sphinx_gallery/backreferences.py +++ b/sphinx_gallery/backreferences.py @@ -240,6 +240,7 @@ def identify_names(script_blocks, global_variables=None, node=''): .. only:: html .. figure:: /{thumbnail} + :alt: {title} :ref:`sphx_glr_{ref_name}` @@ -255,8 +256,8 @@ def identify_names(script_blocks, global_variables=None, node=''): """ -def _thumbnail_div(target_dir, src_dir, fname, snippet, is_backref=False, - check=True): +def _thumbnail_div(target_dir, src_dir, fname, snippet, title, + is_backref=False, check=True): """Generate RST to place a thumbnail in a gallery.""" thumb, _ = _find_image_ext( os.path.join(target_dir, 'images', 'thumb', @@ -275,11 +276,11 @@ def _thumbnail_div(target_dir, src_dir, fname, snippet, is_backref=False, template = BACKREF_THUMBNAIL_TEMPLATE if is_backref else THUMBNAIL_TEMPLATE return template.format(snippet=escape(snippet), - thumbnail=thumb, ref_name=ref_name) + thumbnail=thumb, title=title, ref_name=ref_name) def _write_backreferences(backrefs, seen_backrefs, gallery_conf, - target_dir, fname, snippet): + target_dir, fname, snippet, title): """Write backreference file including a thumbnail list of examples.""" if gallery_conf['backreferences_dir'] is None: return @@ -296,7 +297,8 @@ def _write_backreferences(backrefs, seen_backrefs, gallery_conf, ex_file.write('\n\n' + heading + '\n') ex_file.write('^' * len(heading) + '\n') ex_file.write(_thumbnail_div(target_dir, gallery_conf['src_dir'], - fname, snippet, is_backref=True)) + fname, snippet, title, + is_backref=True)) seen_backrefs.add(backref) diff --git a/sphinx_gallery/gen_rst.py b/sphinx_gallery/gen_rst.py index eab8b4a9e..dd68f458f 100644 --- a/sphinx_gallery/gen_rst.py +++ b/sphinx_gallery/gen_rst.py @@ -336,12 +336,12 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): 'generating gallery for %s... ' % build_target_dir, length=len(sorted_listdir)) for fname in iterator: - intro, cost = generate_file_rst( + intro, title, cost = generate_file_rst( fname, target_dir, src_dir, gallery_conf, seen_backrefs) src_file = os.path.normpath(os.path.join(src_dir, fname)) costs.append((cost, src_file)) this_entry = _thumbnail_div(target_dir, gallery_conf['src_dir'], - fname, intro) + """ + fname, intro, title) + """ .. toctree:: :hidden: @@ -818,9 +818,9 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf, if cobj['module'].startswith(gallery_conf['doc_module'])) # Write backreferences _write_backreferences(backrefs, seen_backrefs, gallery_conf, target_dir, - fname, intro) + fname, intro, title) - return intro, (time_elapsed, memory_used) + return intro, title, (time_elapsed, memory_used) def rst_blocks(script_blocks, output_blocks, file_conf, gallery_conf): From 4ad42ae53e8e98b372bab6102c0f6291e513291d Mon Sep 17 00:00:00 2001 From: Lucy Liu Date: Thu, 30 Apr 2020 17:55:24 +0200 Subject: [PATCH 2/6] amend test --- sphinx_gallery/tests/test_backreferences.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sphinx_gallery/tests/test_backreferences.py b/sphinx_gallery/tests/test_backreferences.py index 76a1e1935..1eb1630fb 100644 --- a/sphinx_gallery/tests/test_backreferences.py +++ b/sphinx_gallery/tests/test_backreferences.py @@ -20,6 +20,7 @@ .. only:: html .. figure:: /fake_dir/images/thumb/sphx_glr_test_file_thumb.png + :alt: test title :ref:`sphx_glr_fake_dir_test_file.py` @@ -46,10 +47,12 @@ def test_thumbnail_div(content, tooltip, is_backref): """Test if the thumbnail div generates the correct string.""" with pytest.raises(RuntimeError, match='internal sphinx-gallery thumb'): html_div = sg._thumbnail_div('fake_dir', '', 'test_file.py', - '<"test">') + '<"test">', '<"title">') content = _sanitize_rst(content) + title = 'test title' html_div = sg._thumbnail_div('fake_dir', '', 'test_file.py', - content, is_backref=is_backref, check=False) + content, title, is_backref=is_backref, + check=False) if is_backref: extra = """ From 3280af1934ab5dbae8f59f46a9f49efdcb81ce43 Mon Sep 17 00:00:00 2001 From: Lucy Liu Date: Thu, 30 Apr 2020 20:18:49 +0200 Subject: [PATCH 3/6] FIX code path --- sphinx_gallery/gen_rst.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sphinx_gallery/gen_rst.py b/sphinx_gallery/gen_rst.py index dd68f458f..254c0a93b 100644 --- a/sphinx_gallery/gen_rst.py +++ b/sphinx_gallery/gen_rst.py @@ -336,6 +336,7 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): 'generating gallery for %s... ' % build_target_dir, length=len(sorted_listdir)) for fname in iterator: + print('for {} generate file run'.format(fname)) intro, title, cost = generate_file_rst( fname, target_dir, src_dir, gallery_conf, seen_backrefs) src_file = os.path.normpath(os.path.join(src_dir, fname)) @@ -756,7 +757,7 @@ def generate_file_rst(fname, target_dir, src_dir, gallery_conf, if md5sum_is_current(target_file): if executable: gallery_conf['stale_examples'].append(target_file) - return intro, (0, 0) + return intro, title, (0, 0) image_dir = os.path.join(target_dir, 'images') if not os.path.exists(image_dir): From 1dbc5a48f942e611ab56c5304b64d23c6bb40007 Mon Sep 17 00:00:00 2001 From: Lucy Liu Date: Thu, 30 Apr 2020 23:05:09 +0200 Subject: [PATCH 4/6] better tests --- sphinx_gallery/gen_rst.py | 1 - sphinx_gallery/tests/test_full.py | 50 ++++++++++++++++++++++++------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/sphinx_gallery/gen_rst.py b/sphinx_gallery/gen_rst.py index 254c0a93b..01fffc5f6 100644 --- a/sphinx_gallery/gen_rst.py +++ b/sphinx_gallery/gen_rst.py @@ -336,7 +336,6 @@ def generate_dir_rst(src_dir, target_dir, gallery_conf, seen_backrefs): 'generating gallery for %s... ' % build_target_dir, length=len(sorted_listdir)) for fname in iterator: - print('for {} generate file run'.format(fname)) intro, title, cost = generate_file_rst( fname, target_dir, src_dir, gallery_conf, seen_backrefs) src_file = os.path.normpath(os.path.join(src_dir, fname)) diff --git a/sphinx_gallery/tests/test_full.py b/sphinx_gallery/tests/test_full.py index e6cb36ef7..4e2ed2117 100644 --- a/sphinx_gallery/tests/test_full.py +++ b/sphinx_gallery/tests/test_full.py @@ -552,21 +552,51 @@ def test_rebuild(tmpdir_factory, sphinx_app): different=('plot_numpy_matplotlib.ipynb')) -def test_alt_text(sphinx_app): - """Test alt text""" +def test_alt_text_image(sphinx_app): + """Test alt text for matplotlib images in html and rst""" + out_dir = sphinx_app.outdir src_dir = sphinx_app.srcdir - fname = op.join(src_dir, 'auto_examples', 'plot_matplotlib_alt.rst') - assert op.isfile(fname) - with codecs.open(fname, 'r', 'utf-8') as fid: + # alt text in rst + example_rst = op.join(src_dir, 'auto_examples', 'plot_matplotlib_alt.rst') + with codecs.open(example_rst, 'r', 'utf-8') as fid: rst = fid.read() # suptitle and axes titles assert ':alt: This is a sup title, subplot 1, subplot 2' in rst # multiple titles assert ':alt: Left Title, Center Title, Right Title' in rst - fname = op.join(src_dir, 'auto_examples', 'plot_numpy_matplotlib.rst') - assert op.isfile(fname) - with codecs.open(fname, 'r', 'utf-8') as fid: + # no fig title - alt text is file name, rst + example_rst = op.join(src_dir, 'auto_examples', + 'plot_numpy_matplotlib.rst') + with codecs.open(example_rst, 'r', 'utf-8') as fid: + rst = fid.read() + assert ':alt: plot numpy matplotlib' in rst + # html + example_html = op.join(out_dir, 'auto_examples', + 'plot_numpy_matplotlib.html') + with codecs.open(example_html, 'r', 'utf-8') as fid: + html = fid.read() + assert 'alt="plot numpy matplotlib"' in html + + +def test_alt_text_thumbnail(sphinx_app): + """Test alt text for thumbnail in html and rst.""" + out_dir = sphinx_app.outdir + src_dir = sphinx_app.srcdir + # check gallery index thumbnail, html + generated_examples_index = op.join(out_dir, 'auto_examples', 'index.html') + with codecs.open(generated_examples_index, 'r', 'utf-8') as fid: + html = fid.read() + assert 'alt="SVG graphics"' in html + # check backreferences thumbnail, html + backref_html = op.join(out_dir, 'gen_modules', + 'sphinx_gallery.backreferences.html') + with codecs.open(backref_html, 'r', 'utf-8') as fid: + html = fid.read() + assert 'alt="Link to other packages"' in html + # check gallery index thumbnail, rst + generated_examples_index = op.join(src_dir, 'auto_examples', + 'index.rst') + with codecs.open(generated_examples_index, 'r', 'utf-8') as fid: rst = fid.read() - # file name when no fig title - assert 'plot numpy matplotlib' in rst + assert ':alt: Trivial module to provide a value for plot_numpy_matplotlib.py' in rst # noqa: E501 From db53d6d5c8f94567994143dbf49e9eb96b43efce Mon Sep 17 00:00:00 2001 From: Lucy Liu Date: Thu, 30 Apr 2020 23:26:04 +0200 Subject: [PATCH 5/6] lint --- sphinx_gallery/tests/test_full.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_gallery/tests/test_full.py b/sphinx_gallery/tests/test_full.py index 4e2ed2117..307caa191 100644 --- a/sphinx_gallery/tests/test_full.py +++ b/sphinx_gallery/tests/test_full.py @@ -599,4 +599,4 @@ def test_alt_text_thumbnail(sphinx_app): 'index.rst') with codecs.open(generated_examples_index, 'r', 'utf-8') as fid: rst = fid.read() - assert ':alt: Trivial module to provide a value for plot_numpy_matplotlib.py' in rst # noqa: E501 + assert ':alt: Trivial module to provide a value for plot_numpy_matplotlib.py' in rst # noqa: E501 From eb6281a7126700eed4ccaca56aff73bc00710a97 Mon Sep 17 00:00:00 2001 From: Lucy Liu Date: Thu, 30 Apr 2020 23:33:00 +0200 Subject: [PATCH 6/6] comment --- sphinx_gallery/tests/test_full.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx_gallery/tests/test_full.py b/sphinx_gallery/tests/test_full.py index 307caa191..528ba6581 100644 --- a/sphinx_gallery/tests/test_full.py +++ b/sphinx_gallery/tests/test_full.py @@ -556,7 +556,7 @@ def test_alt_text_image(sphinx_app): """Test alt text for matplotlib images in html and rst""" out_dir = sphinx_app.outdir src_dir = sphinx_app.srcdir - # alt text in rst + # alt text is fig titles, rst example_rst = op.join(src_dir, 'auto_examples', 'plot_matplotlib_alt.rst') with codecs.open(example_rst, 'r', 'utf-8') as fid: rst = fid.read()