Skip to content

Commit 5583e4a

Browse files
committed
WIP: Switchers handled by docsbuild-scripts.
1 parent e71030c commit 5583e4a

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed

build_docs.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import os
3939
import pathlib
4040
import re
41+
from shlex import quote
4142
import shutil
4243
import subprocess
4344
import sys
@@ -254,6 +255,40 @@ def translation_branch(locale_repo, locale_clone_dir, needed_version):
254255
return locate_nearest_version(branches, needed_version)
255256

256257

258+
def setup_switchers(html_root):
259+
"""Setup cross-links between cpython versions:
260+
- Cross-link various languages in a language switcher
261+
- Cross-link various versions in a version switcher
262+
"""
263+
shutil.copy("switchers.js", os.path.join(html_root, "_static"))
264+
shell_out(
265+
" ".join(
266+
[
267+
"sed",
268+
"-i",
269+
quote(
270+
r's#\(^ *\)</body>$#\1<script type="text/javascript" src="_static/switchers.js"></script>\n\0#'
271+
),
272+
os.path.join(html_root, "*.html"),
273+
]
274+
),
275+
shell=True,
276+
)
277+
shell_out(
278+
" ".join(
279+
[
280+
"sed",
281+
"-i",
282+
quote(
283+
r's#\(^ *\)</body>$#\1<script type="text/javascript" src="../_static/switchers.js"></script>\n\0#'
284+
),
285+
os.path.join(html_root, "*/*.html"),
286+
]
287+
),
288+
shell=True,
289+
)
290+
291+
257292
def build_one(
258293
version,
259294
git_branch,
@@ -303,6 +338,15 @@ def build_one(
303338
python = os.path.join(venv, "bin/python")
304339
sphinxbuild = os.path.join(venv, "bin/sphinx-build")
305340
blurb = os.path.join(venv, "bin/blurb")
341+
# Disable cpython switchers, we handle them now:
342+
shell_out(
343+
[
344+
"sed",
345+
"-i",
346+
"s/ *-A switchers=1//",
347+
os.path.join(checkout, "Doc", "Makefile"),
348+
]
349+
)
306350
shell_out(
307351
[
308352
"make",
@@ -319,6 +363,7 @@ def build_one(
319363
logfile=os.path.join(log_directory, logname),
320364
)
321365
shell_out(["chgrp", "-R", group, log_directory])
366+
setup_switchers(os.path.join(checkout, "Doc", "build", "html"))
322367
logging.info("Build done for version: %s, language: %s", version, language)
323368

324369

indexsidebar.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<h3>{% trans %}Download{% endtrans %}</h3>
2+
<p><a href="{{ pathto('download') }}">{% trans %}Download these documents{% endtrans %}</a></p>
3+
<h3>{% trans %}Docs by version{% endtrans %}</h3>
4+
<ul>
5+
<li><a href="https://docs.python.org/3.10/">{% trans %}Python 3.10 (in development){% endtrans %}</a></li>
6+
<li><a href="https://docs.python.org/3.9/">{% trans %}Python 3.9 (pre-release){% endtrans %}</a></li>
7+
<li><a href="https://docs.python.org/3.8/">{% trans %}Python 3.8 (stable){% endtrans %}</a></li>
8+
<li><a href="https://docs.python.org/3.7/">{% trans %}Python 3.7 (stable){% endtrans %}</a></li>
9+
<li><a href="https://docs.python.org/3.6/">{% trans %}Python 3.6 (security-fixes){% endtrans %}</a></li>
10+
<li><a href="https://docs.python.org/3.5/">{% trans %}Python 3.5 (security-fixes){% endtrans %}</a></li>
11+
<li><a href="https://docs.python.org/2.7/">{% trans %}Python 2.7 (EOL){% endtrans %}</a></li>
12+
<li><a href="https://www.python.org/doc/versions/">{% trans %}All versions{% endtrans %}</a></li>
13+
</ul>
14+
15+
<h3>{% trans %}Other resources{% endtrans %}</h3>
16+
<ul>
17+
{# XXX: many of these should probably be merged in the main docs #}
18+
<li><a href="https://www.python.org/dev/peps/">{% trans %}PEP Index{% endtrans %}</a></li>
19+
<li><a href="https://wiki.python.org/moin/BeginnersGuide">{% trans %}Beginner's Guide{% endtrans %}</a></li>
20+
<li><a href="https://wiki.python.org/moin/PythonBooks">{% trans %}Book List{% endtrans %}</a></li>
21+
<li><a href="https://www.python.org/doc/av/">{% trans %}Audio/Visual Talks{% endtrans %}</a></li>
22+
<li><a href="https://devguide.python.org/">{% trans %}Python Developer’s Guide{% endtrans %}</a></li>
23+
</ul>

switchers.js

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
(function() {
2+
'use strict';
3+
4+
// Parses versions in URL segments like:
5+
// "3", "dev", "release/2.7" or "3.6rc2"
6+
var version_regexs = [
7+
'(?:\\d)',
8+
'(?:\\d\\.\\d[\\w\\d\\.]*)',
9+
'(?:dev)',
10+
'(?:release/\\d.\\d[\\x\\d\\.]*)'];
11+
12+
var all_versions = {
13+
'3.10': 'dev (3.10)',
14+
'3.9': 'pre (3.9)',
15+
'3.8': '3.8',
16+
'3.7': '3.7',
17+
'3.6': '3.6',
18+
'3.5': '3.5',
19+
'2.7': '2.7',
20+
};
21+
22+
var all_languages = {
23+
'en': 'English',
24+
'fr': 'French',
25+
'ja': 'Japanese',
26+
'ko': 'Korean',
27+
'pt-br': 'Brazilian Portuguese',
28+
'zh-cn': 'Simplified Chinese',
29+
};
30+
31+
function build_version_select(current_version, current_release) {
32+
var buf = ['<select>'];
33+
34+
$.each(all_versions, function(version, title) {
35+
buf.push('<option value="' + version + '"');
36+
if (version == current_version)
37+
buf.push(' selected="selected">' + current_release + '</option>');
38+
else
39+
buf.push('>' + title + '</option>');
40+
});
41+
42+
buf.push('</select>');
43+
return buf.join('');
44+
}
45+
46+
function build_language_select(current_language) {
47+
var buf = ['<select>'];
48+
49+
$.each(all_languages, function(language, title) {
50+
if (language == current_language)
51+
buf.push('<option value="' + language + '" selected="selected">' +
52+
all_languages[current_language] + '</option>');
53+
else
54+
buf.push('<option value="' + language + '">' + title + '</option>');
55+
});
56+
if (!(current_language in all_languages)) {
57+
// In case we're browsing a language that is not yet in all_languages.
58+
buf.push('<option value="' + current_language + '" selected="selected">' +
59+
current_language + '</option>');
60+
all_languages[current_language] = current_language;
61+
}
62+
buf.push('</select>');
63+
return buf.join('');
64+
}
65+
66+
function navigate_to_first_existing(urls) {
67+
// Navigate to the first existing URL in urls.
68+
var url = urls.shift();
69+
if (urls.length == 0) {
70+
window.location.href = url;
71+
return;
72+
}
73+
$.ajax({
74+
url: url,
75+
success: function() {
76+
window.location.href = url;
77+
},
78+
error: function() {
79+
navigate_to_first_existing(urls);
80+
}
81+
});
82+
}
83+
84+
function on_version_switch() {
85+
var selected_version = $(this).children('option:selected').attr('value') + '/';
86+
var url = window.location.href;
87+
var current_language = language_segment_from_url(url);
88+
var current_version = version_segment_in_url(url);
89+
var new_url = url.replace('.org/' + current_language + current_version,
90+
'.org/' + current_language + selected_version);
91+
if (new_url != url) {
92+
navigate_to_first_existing([
93+
new_url,
94+
url.replace('.org/' + current_language + current_version,
95+
'.org/' + selected_version),
96+
'https://docs.python.org/' + current_language + selected_version,
97+
'https://docs.python.org/' + selected_version,
98+
'https://docs.python.org/'
99+
]);
100+
}
101+
}
102+
103+
function on_language_switch() {
104+
var selected_language = $(this).children('option:selected').attr('value') + '/';
105+
var url = window.location.href;
106+
var current_language = language_segment_from_url(url);
107+
var current_version = version_segment_in_url(url);
108+
if (selected_language == 'en/') // Special 'default' case for english.
109+
selected_language = '';
110+
var new_url = url.replace('.org/' + current_language + current_version,
111+
'.org/' + selected_language + current_version);
112+
if (new_url != url) {
113+
navigate_to_first_existing([
114+
new_url,
115+
'https://docs.python.org/'
116+
]);
117+
}
118+
}
119+
120+
// Returns the path segment of the language as a string, like 'fr/'
121+
// or '' if not found.
122+
function language_segment_from_url(url) {
123+
var language_regexp = '\.org/([a-z]{2}(?:-[a-z]{2})?/)';
124+
var match = url.match(language_regexp);
125+
if (match !== null)
126+
return match[1];
127+
return '';
128+
}
129+
130+
// Returns the path segment of the version as a string, like '3.6/'
131+
// or '' if not found.
132+
function version_segment_in_url(url) {
133+
var language_segment = '(?:[a-z]{2}(?:-[a-z]{2})?/)';
134+
var version_segment = '(?:(?:' + version_regexs.join('|') + ')/)';
135+
var version_regexp = '\\.org/' + language_segment + '?(' + version_segment + ')';
136+
var match = url.match(version_regexp);
137+
if (match !== null)
138+
return match[1];
139+
return ''
140+
}
141+
142+
function create_placeholders_if_missing() {
143+
if ($('.version_switcher_placeholder').length) return;
144+
var the_place = $("body>div.related>ul>li:not(.right):contains('Documentation'):first")
145+
the_place.html('<span class="language_switcher_placeholder"></span> \
146+
<span class="version_switcher_placeholder"></span> \
147+
<a href="../index.html">Documentation</a> &#187;')
148+
}
149+
150+
$(document).ready(function() {
151+
var release = DOCUMENTATION_OPTIONS.VERSION;
152+
var language_segment = language_segment_from_url(window.location.href);
153+
var current_language = language_segment.replace(/\/+$/g, '') || 'en';
154+
var version = release.substr(0, 3);
155+
var version_select = build_version_select(version, release);
156+
157+
create_placeholders_if_missing()
158+
$('.version_switcher_placeholder').html(version_select);
159+
$('.version_switcher_placeholder select').bind('change', on_version_switch);
160+
161+
var language_select = build_language_select(current_language);
162+
163+
$('.language_switcher_placeholder').html(language_select);
164+
$('.language_switcher_placeholder select').bind('change', on_language_switch);
165+
});
166+
})();

0 commit comments

Comments
 (0)