Skip to content

Add embed_code_links parameter #1410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jschueller
Copy link
Contributor

see #1409

Copy link
Contributor

@lucyleeow lucyleeow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable.

I would add a _bool_eval check in _fill_gallery_conf_defaults for the new config.

I think we could add a test in test_gen_gallery.py, using @pytest.mark.add_conf to set the config. Note that the tests here use sphinx_app_wrapper which builds the docs in sphinx_gallery/tests/testconfs/src

@jschueller
Copy link
Contributor Author

Seems reasonable.

I would add a _bool_eval check in _fill_gallery_conf_defaults for the new config.

I think we could add a test in test_gen_gallery.py, using @pytest.mark.add_conf to set the config. Note that the tests here use sphinx_app_wrapper which builds the docs in sphinx_gallery/tests/testconfs/src

done

Copy link
Contributor

@lucyleeow lucyleeow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can add a test that reads in the generated html file and ensure no links are being added.

(For an example, I think test_minigallery_sort_order_callable reads a generated file)

@lucyleeow
Copy link
Contributor

lucyleeow commented Nov 27, 2024

Another note to myself - I should improve the documentation. I did not realise text block links are added by sphinx and are thus dictated by sphinx intersphinx config. We only add the code block links and by default will add for all modules in intersphinx, but check modules in reference_url first. We need to add local module to reference_url, to add links to local module in code.

@jschueller
Copy link
Contributor Author

I think I need help to write a test

@larsoner
Copy link
Contributor

larsoner commented Dec 2, 2024

From #1409 (comment) also instead of a pure boolean I wonder if we should let this be a regex string with default .* for things to link. If you set it to an empty string it links nothing. For @drammock's use case it might be r"mne\..*" for example to link only to MNE objects or whatever

@lucyleeow
Copy link
Contributor

I think I need help to write a test

I had a quick play and something like this should work:

@pytest.mark.add_conf(
    content=r"""
import sys

intersphinx_mapping = {
    "python": (f"https://docs.python.org/{sys.version_info.major}", None),
}
sphinx_gallery_conf = {
    'examples_dirs': 'src',
    'gallery_dirs': 'ex',
    'embed_code_links': True,
}"""
)
def test_embed_code_links(sphinx_app_wrapper):
    """Check `embed_code_links` works."""
    sphinx_app = sphinx_app_wrapper.build_sphinx_app()
    built_example = Path(sphinx_app.outdir, "ex", "plot_1.html")

    with open(built_example, "r", encoding="utf-8") as fid:
        html = fid.read()
    print(html) # Check link has/has not been added in html

I thought that since I added python to intersphinx, print function in example plot_1.py would be linked but it wasn't? Looking at the SG docs themselves, print calls in our examples are also not linked, but variables that are built-ins e.g., string or list are. Maybe I've mis-understood something though?

@jschueller
Copy link
Contributor Author

thanks @lucyleeow I added all in one commit

@lucyleeow
Copy link
Contributor

@jschueller sorry not sure what you added? AFAICT the test still is just a print ?

Also I think @larsoner 's suggestion of having the config be more flexible with a regex would be nice:
#1410 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants