Skip to content

Commit

Permalink
Enable shorter gallery URLs (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt authored Dec 6, 2024
1 parent bcca105 commit 3993e8b
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,22 @@ def gallery_spec(name):
'sections': [gallery_spec(project) for project in projects],
}

def to_gallery_redirects():
# Redirects from /projname to /gallery/projname/<index>
def root_and_gallery_index_redirects():
"""
The projects were originally (pyviz era) at the root, e.g. examples.pyviz.org/attractors/attractors.html
At the time, redirects were implemented to allow shorter URLs like examples.pyviz.org/attractors(/).
Projects are now under the /gallery folder, because otherwise they would all
end up being displayed in the navbar, as the PyData Sphinx Theme displays the
top-level toctree in the navbar. The first goal of this function is to
generate these redirects to preserve these old links:
examples.holoviz.org/attractors(/) -> examples.holoviz.org/gallery/attractors/attractors.html
For mono-notebook projects (the majority), links like examples.pyviz.org/gallery/boids(/)
don't redirect by default to examples.pyviz.org/gallery/boids/boids.html.
The second goal of the function is to enable this by creating this redirect link:
examples.holoviz.org/gallery/boids/index -> examples.holoviz.org/gallery/boids/boids
Which whill enable examples.holoviz.org/gallery/boids(/)
"""
redirects = {}
for project in projects:
notebooks = find_notebooks(project, root='..')
Expand All @@ -186,7 +200,10 @@ def to_gallery_redirects():
index = nbstems[0]
else:
raise RuntimeError(f'Too many notebooks found: {nbstems}')
redirects[project] = f'gallery/{project}/{index}'
redirects[f'{project}/index'] = f'gallery/{project}/{index}'
if index != 'index':
# Projects with an index notebook don't need the redirect
redirects[f'gallery/{project}/index'] = f'gallery/{project}/{index}'
return redirects

top_level_redirects = {
Expand Down Expand Up @@ -252,7 +269,8 @@ def to_gallery_redirects():
**top_level_redirects,
**project_direct_links,
# Links from e.g. /attractors to /gallery/attractors/index.html
**to_gallery_redirects(),
# And from e.g. /gallery/boids/index.html to /gallery/boids/boids.html
**root_and_gallery_index_redirects(),
}

html_context.update({
Expand Down

0 comments on commit 3993e8b

Please sign in to comment.