Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork66
Add language and version switchers#193
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
AA-Turner wants to merge1 commit intopython:mainChoose a base branch fromAA-Turner:switchers-squashed
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletionspyproject.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
72 changes: 71 additions & 1 deletionpython_docs_theme/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
17 changes: 15 additions & 2 deletionspython_docs_theme/layout.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletionspython_docs_theme/static/switchers.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
'use strict'; | ||
const _is_file_uri = (uri) => uri.startsWith('file://'); | ||
const _IS_LOCAL = _is_file_uri(window.location.href); | ||
const _CONTENT_ROOT = document.documentElement.dataset.content_root; | ||
const _CURRENT_PREFIX = _IS_LOCAL | ||
? null | ||
: new URL(_CONTENT_ROOT, window.location).pathname; | ||
const _CURRENT_RELEASE = DOCUMENTATION_OPTIONS.VERSION || ''; | ||
const _CURRENT_VERSION = _CURRENT_RELEASE.split('.').slice(0, 2).join('.'); | ||
const _CURRENT_LANGUAGE = DOCUMENTATION_OPTIONS.LANGUAGE?.toLowerCase() || 'en'; | ||
/** | ||
* Change the current page to the first existing URL in the list. | ||
* @param {Array<string>} urls | ||
* @private | ||
*/ | ||
const _navigate_to_first_existing = async (urls) => { | ||
// Navigate to the first existing URL of urls. | ||
for (const url of urls) { | ||
try { | ||
const response = await fetch(url, { method: 'GET' }) | ||
if (response.ok) { | ||
window.location.href = url; | ||
return url; // Avoid race conditions with multiple redirects | ||
} | ||
} catch(err) { | ||
console.error(`Error in: ${url}`); | ||
console.error(err) | ||
} | ||
} | ||
// if all else fails, redirect to the d.p.o root | ||
window.location.href = '/'; | ||
}; | ||
/** | ||
* Navigate to the selected version. | ||
* @param {Event} event | ||
* @returns {Promise<void>} | ||
*/ | ||
const on_version_switch = async (event) => { | ||
if (_IS_LOCAL) return; | ||
const selected_version = event.target.value; | ||
// Special 'default' case for English. | ||
const new_prefix = | ||
_CURRENT_LANGUAGE === 'en' | ||
? `/${selected_version}/` | ||
: `/${_CURRENT_LANGUAGE}/${selected_version}/`; | ||
const new_prefix_en = `/${selected_version}/`; | ||
if (_CURRENT_PREFIX !== new_prefix) { | ||
// Try the following pages in order: | ||
// 1. The current page in the current language with the new version | ||
// 2. The current page in English with the new version | ||
// 3. The documentation home in the current language with the new version | ||
// 4. The documentation home in English with the new version | ||
await _navigate_to_first_existing([ | ||
window.location.href.replace(_CURRENT_PREFIX, new_prefix), | ||
window.location.href.replace(_CURRENT_PREFIX, new_prefix_en), | ||
new_prefix, | ||
new_prefix_en, | ||
]); | ||
} | ||
}; | ||
/** | ||
* Navigate to the selected language. | ||
* @param {Event} event | ||
* @returns {Promise<void>} | ||
*/ | ||
const on_language_switch = async (event) => { | ||
if (_IS_LOCAL) return; | ||
const selected_language = event.target.value; | ||
// Special 'default' case for English. | ||
const new_prefix = | ||
selected_language === 'en' | ||
? `/${_CURRENT_VERSION}/` | ||
: `/${selected_language}/${_CURRENT_VERSION}/`; | ||
if (_CURRENT_PREFIX !== new_prefix) { | ||
// Try the following pages in order: | ||
// 1. The current page in the new language with the current version | ||
// 2. The documentation home in the new language with the current version | ||
await _navigate_to_first_existing([ | ||
window.location.href.replace(_CURRENT_PREFIX, new_prefix), | ||
new_prefix, | ||
]); | ||
} | ||
}; | ||
/** | ||
* Set up the version and language switchers. | ||
* @returns {Promise<void>} | ||
*/ | ||
const initialise_switchers = async () => { | ||
try { | ||
// Update the version select elements | ||
document | ||
.querySelectorAll('.version_switcher_placeholder select') | ||
.forEach((select) => { | ||
if (_IS_LOCAL) { | ||
select.disabled = true; | ||
select.title = 'Version switching is disabled in local builds'; | ||
} | ||
select.addEventListener('change', on_version_switch); | ||
select.parentElement.classList.remove('version_switcher_placeholder'); | ||
}); | ||
// Update the language select elements | ||
document | ||
.querySelectorAll('.language_switcher_placeholder select') | ||
.forEach((select) => { | ||
if (_IS_LOCAL) { | ||
select.disabled = true; | ||
select.title = 'Language switching is disabled in local builds'; | ||
} | ||
select.addEventListener('change', on_language_switch); | ||
select.parentElement.classList.remove('language_switcher_placeholder'); | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
if (document.readyState !== 'loading') { | ||
initialise_switchers(); | ||
} else { | ||
document.addEventListener('DOMContentLoaded', initialise_switchers); | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.