10000 Support redirects from non-padded PEP numbers by AA-Turner · Pull Request #2421 · python/peps · GitHub
[go: up one dir, main page]

Skip to content

Support redirects from non-padded PEP numbers #2421

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
PRS: Support redirects
Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
  • Loading branch information
AA-Turner and ezio-melotti committed Mar 12, 2022
commit eead2f7c813cd49a68ca9d94840bc76d0f007374
1 change: 1 addition & 0 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
html_permalinks = False # handled in the PEPContents transform
html_baseurl = "https://peps.python.org" # to create the CNAME file
gettext_auto_build = False # speed-ups
html_additional_pages = {"404": "not_found.html"}

templates_path = ['pep_sphinx_extensions/pep_theme/templates'] # Theme template relative paths from `confdir`
44 changes: 44 additions & 0 deletions pep_sphinx_extensions/pep_theme/templates/not_found.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{# 404 Not Found: Redirect typos and link to PEP 0 #}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="color-scheme" content="light dark" />
<title>PEP Not Found | peps.python.org</title>
<link rel="shortcut icon" href="{{ pathto('_static/py.png', resource=True) }}"/>
<link rel="stylesheet" href="{{ pathto('_static/style.css', resource=True) }}" type="text/css" />
<link rel="stylesheet" href="{{ pathto('_static/mq.css', resource=True) }}" type="text/css" />
<link rel="stylesheet" href="{{ pathto('_static/dark.css', resource=True) }}" type="text/css" media="(prefers-color-scheme: dark)" id="css-dark"/>
<link href="https://fonts.googleapis.com/css2?family=Source+Sans+Pro:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
<meta name="description" content="Python Enhancement Proposals (PEPs)"/>
<script>
const pepNumMatch = window.location.pathname.match(/^\d+$/)
Copy link
Member

Choose a reason for hiding this comment

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

With ^ and $ in the regex, it won't match pep-NNN. Did you just change this temporarily to test?
Note that in the suggestion below I used match[0] because of this change, but if my previous suggestion is restored it should be match[1].

Copy link
Member Author

Choose a reason for hiding this comment

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

it won't match pep-NNN

From above, "I don't want to include the pep- prefix for the auto-redirects"

A

Copy link
Member
@ezio-melotti ezio-melotti Mar 12, 2022

Choose a reason for hiding this comment

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

Why not?
If we set the autolinking to https://peps.python.org/pep-<num>/ and <num> is a valid 4-digits number (e.g. pep-0008), then it will go straight to the right page. If we omit the pep- prefix, then it will have to go through an extra redirect if JS is enabled, or end up at the 404 page if JS is disabled.

In case of a valid PEP with a <4 digits number (e.g. pep-8) it will go to /pep-8/ and get redirected to /pep-0008.
In case of an invalid PEP with a <4 digits number (e.g. pep-123) it will go to /pep-123/, get redirected to /pep-0123, and then stop at the 404 page since the PEP doesn't exist and the id is already 4 digits long.

Am I missing something?

Copy link
Member

Choose a reason for hiding this comment

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

The autolinking on this repo is currently set to link to https://python.org/dev/peps/pep-<num>. My suggestion that simply matched pep-(\d+) works even with this type of URL, but fails with /^\d+$/.

if (pepNumMatch !== null) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (pepNumMatch !== null) {
if (pepNumMatch !== null && match[0].length < 4) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Our comments crossed over, sorry!

We could check the number but if invalid the only harm of not checking would be an extra redirect from one invalid URL to another.

A

const pepNum = pepNumMatch[0]
window.location.pathname = "/pep-" + pepNum.padStart(4, "0")
}
</script>
</head>
<body>
<section id="pep-page-section">
<header>
<h1>Python Enhancement Proposals</h1>
<ul class="breadcrumbs">
<li><a href="https://www.python.org/" title="The Python Programming Language">Python</a> &raquo; </li>
<li><a href="{{ pathto("pep-0000") }}">PEP Index</a></li>
</ul>
<button onClick="toggleColourScheme()"></button>
</header>
<article>
<section id="pep-content">
<h1 class="page-title">PEP Not Found</h1>
<p>There is no PEP at this address.</p>
<p>Please return to <a href="{{ pathto("pep-0000") }}">the index</a>.</p>
</section>
</article>
<nav id="pep-sidebar"></nav>
</section>
<script src="{{ pathto('_static/colour_scheme.js', resource=True) }}"></script>
</body>
</html>
0