Skip to content
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

Add version switcher to discretize docs #371

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions docs/_static/versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"version": "main",
"url": "https://discretize.simpeg.xyz/en/main/"
},
{
"name": "0.10.0 (latest)",
"version": "v0.10.0",
Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, the version field should not have the leading v because these versions should be parsable by the compare-versions node module (https://pydata-sphinx-theme.readthedocs.io/en/stable/user_guide/announcements.html#version-warning-banners).

This way, the warning version banners can behave properly. If we have the v, any previous version is recognized as a development version, so the banner shows the wrong text.

We can still use the leading v in the url though.

Copy link
Member

Choose a reason for hiding this comment

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

compare_versions ignores any leading v
https://www.npmjs.com/package/compare-versions

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually experienced a misbehaviour in the version banner in simpeg before I changed the versions to not have a leading v. But yes, you're right that they seem to support it...

Ignore this then and we'll see if it works well.

Copy link
Member

Choose a reason for hiding this comment

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

It looks like the pydata sphinx theme uses the leading v in their's for matching, https://github.com/pydata/pydata-sphinx-theme/blob/main/docs/_static/switcher.json

Copy link
Member Author

Choose a reason for hiding this comment

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

Then maybe there was something else that was failing back then... Well, I'm glad we don't have to fix it.

"url": "https://discretize.simpeg.xyz/en/v0.10.0/",
"preferred": true
}
]

32 changes: 19 additions & 13 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sys
from pathlib import Path
from datetime import datetime
from packaging.version import parse
import discretize
from sphinx_gallery.sorting import FileNameSortKey
import shutil
Expand Down Expand Up @@ -94,8 +95,16 @@
#
# The full version, including alpha/beta/rc tags.
release = version("discretize")
discretize_version = parse(release)

# The short X.Y version.
version = discretize_version.public
if discretize_version.is_devrelease:
branch = "main"
else:
branch = f"v{version}"
Copy link
Member Author

@santisoler santisoler Sep 5, 2024

Choose a reason for hiding this comment

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

We should also remove the leading v from here for the version to be parsable.

Suggested change
branch = f"v{version}"
branch = version

# The short X.Y version.
version = ".".join(release.split(".")[:2])
# version = ".".join(release.split(".")[:2])

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down Expand Up @@ -144,7 +153,6 @@

if link_github:
import inspect
from packaging.version import parse

extensions.append("sphinx.ext.linkcode")

Expand Down Expand Up @@ -197,16 +205,7 @@ def linkcode_resolve(domain, info):
fn = os.path.relpath(fn, start=os.path.dirname(discretize.__file__))
except ValueError:
return None

discretize_version = parse(discretize.__version__)
tag = (
"main"
if discretize_version.is_devrelease
else f"v{discretize_version.public}"
)
return (
f"https://github.com/simpeg/discretize/blob/{tag}/discretize/{fn}{linespec}"
)
return f"https://github.com/simpeg/discretize/blob/{branch}/discretize/{fn}{linespec}"

else:
extensions.append("sphinx.ext.viewcode")
Expand Down Expand Up @@ -246,6 +245,7 @@ def linkcode_resolve(domain, info):
dict(name="Contact", url="https://mattermost.softwareunderground.org/simpeg"),
]


# Use Pydata Sphinx theme
html_theme = "pydata_sphinx_theme"

Expand Down Expand Up @@ -279,7 +279,13 @@ def linkcode_resolve(domain, info):
"use_edit_page_button": False,
"collapse_navigation": True,
"navbar_align": "left", # make elements closer to logo on the left
"navbar_end": ["theme-switcher", "navbar-icon-links"],
"navbar_end": ["version-switcher", "theme-switcher", "navbar-icon-links"],
# Configure version switcher (remember to add it to the "navbar_end")
"switcher": {
"version_match": branch,
"json_url": "https://discretize.simpeg.xyz/en/latest/_static/versions.json",
},
"show_version_warning_banner": True,
}

html_logo = "images/discretize-logo.png"
Expand Down