Skip to content

Commit 9bcde43

Browse files
authored
Simplify generation of indexsidebar.html (#283)
Remove the double-rendering of ``indexsidebar.html``. For end-of-life versions, change the sidebar to contain a link to the current stable version. For non-EOL versions, overwrite the new ``_docs_by_version.html`` file with the list of links.
1 parent 9f45c4e commit 9bcde43

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

build_docs.py

+23-17
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,6 @@ def current_dev(self) -> Version:
143143
"""Find the current CPython version in development."""
144144
return max(self, key=Version.as_tuple)
145145

146-
def setup_indexsidebar(self, current: Version, dest_path: Path) -> None:
147-
"""Build indexsidebar.html for Sphinx."""
148-
template_path = HERE / "templates" / "indexsidebar.html"
149-
template = jinja2.Template(template_path.read_text(encoding="UTF-8"))
150-
rendered_template = template.render(
151-
current_version=current,
152-
versions=list(reversed(self)),
153-
)
154-
dest_path.write_text(rendered_template, encoding="UTF-8")
155-
156146

157147
@dataclasses.dataclass(frozen=True, kw_only=True, slots=True)
158148
class Version:
@@ -529,9 +519,9 @@ class DocBuilder:
529519
"""Builder for a CPython version and a language."""
530520

531521
version: Version
532-
versions: Versions
533522
language: Language
534523
cpython_repo: Repository
524+
docs_by_version_content: bytes
535525
switchers_content: bytes
536526
build_root: Path
537527
www_root: Path
@@ -667,10 +657,7 @@ def build(self) -> None:
667657
text = text.replace(" -A switchers=1", "")
668658
(self.checkout / "Doc" / "Makefile").write_text(text, encoding="utf-8")
669659

670-
self.versions.setup_indexsidebar(
671-
self.version,
672-
self.checkout / "Doc" / "tools" / "templates" / "indexsidebar.html",
673-
)
660+
self.setup_indexsidebar()
674661
run_with_logging([
675662
"make",
676663
"-C",
@@ -713,6 +700,18 @@ def build_venv(self) -> None:
713700
run([venv_path / "bin" / "python", "-m", "pip", "freeze", "--all"])
714701
self.venv = venv_path
715702

703+
def setup_indexsidebar(self) -> None:
704+
"""Copy indexsidebar.html for Sphinx."""
705+
tmpl_src = HERE / "templates"
706+
tmpl_dst = self.checkout / "Doc" / "tools" / "templates"
707+
dbv_path = tmpl_dst / "_docs_by_version.html"
708+
709+
shutil.copy(tmpl_src / "indexsidebar.html", tmpl_dst / "indexsidebar.html")
710+
if self.version.status != "EOL":
711+
dbv_path.write_bytes(self.docs_by_version_content)
712+
else:
713+
shutil.copy(tmpl_src / "_docs_by_version.html", dbv_path)
714+
716715
def copy_build_to_webroot(self, http: urllib3.PoolManager) -> None:
717716
"""Copy a given build to the appropriate webroot with appropriate rights."""
718717
logging.info("Publishing start.")
@@ -1099,6 +1098,7 @@ def build_docs(args: argparse.Namespace) -> int:
10991098
force_build = args.force
11001099
del args.force
11011100

1101+
docs_by_version_content = render_docs_by_version(versions).encode()
11021102
switchers_content = render_switchers(versions, languages)
11031103

11041104
build_succeeded = set()
@@ -1118,12 +1118,12 @@ def build_docs(args: argparse.Namespace) -> int:
11181118
scope = sentry_sdk.get_isolation_scope()
11191119
scope.set_tag("version", version.name)
11201120
scope.set_tag("language", language.tag)
1121-
cpython_repo.update()
1121+
cpython_repo.update()
11221122
builder = DocBuilder(
11231123
version,
1124-
versions,
11251124
language,
11261125
cpython_repo,
1126+
docs_by_version_content,
11271127
switchers_content,
11281128
**vars(args),
11291129
)
@@ -1179,6 +1179,12 @@ def parse_languages_from_config() -> Languages:
11791179
return Languages.from_json(config["defaults"], config["languages"])
11801180

11811181

1182+
def render_docs_by_version(versions: Versions) -> str:
1183+
"""Generate content for _docs_by_version.html."""
1184+
links = [f'<li><a href="{v.url}">{v.title}</a></li>' for v in reversed(versions)]
1185+
return "\n".join(links)
1186+
1187+
11821188
def render_switchers(versions: Versions, languages: Languages) -> bytes:
11831189
language_pairs = sorted((l.tag, l.switcher_label) for l in languages if l.in_prod) # NoQA: E741
11841190
version_pairs = [(v.name, v.picker_label) for v in reversed(versions)]

templates/_docs_by_version.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{#
2+
This file is only used in indexsidebar.html, where it is included in the docs
3+
by version list. For non-end-of-life branches, build_docs.py overwrites this
4+
list with the full list of versions.
5+
6+
Keep the following two files synchronised:
7+
* cpython/Doc/tools/templates/_docs_by_version.html
8+
* docsbuild-scripts/templates/_docs_by_version.html
9+
#}
10+
<li><a href="https://docs.python.org/3/">{% trans %}Stable{% endtrans %}</a></li>
11+
<li><a href="https://docs.python.org/dev/">{% trans %}In development{% endtrans %}</a></li>

templates/indexsidebar.html

+5-18
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,17 @@
1-
{#
2-
Beware, this file is rendered twice via Jinja2:
3-
- First by build_docs.py, given 'current_version' and 'versions'.
4-
- A 2nd time by Sphinx.
5-
#}
6-
7-
{% raw %}
81
<h3>{% trans %}Download{% endtrans %}</h3>
92
<p><a href="{{ pathto('download') }}">{% trans %}Download these documents{% endtrans %}</a></p>
10-
{% endraw %}
11-
{% if current_version.status != "EOL" %}
12-
{% raw %}<h3>{% trans %}Docs by version{% endtrans %}</h3>{% endraw %}
3+
<h3>{% trans %}Docs by version{% endtrans %}</h3>
134
<ul>
14-
{% for version in versions %}
15-
<li><a href="{{ version.url }}">{{ version.title }}</a></li>
16-
{% endfor %}
17-
{% raw %}<li><a href="https://www.python.org/doc/versions/">{% trans %}All versions{% endtrans %}</a></li>{% endraw %}
5+
{# _docs_by_version.html is overwritten by build_docs.py for non-EOL versions #}
6+
{% include "_docs_by_version.html" without context %}
7+
<li><a href="https://www.python.org/doc/versions/">{% trans %}All versions{% endtrans %}</a></li>
188
</ul>
19-
{% endif %}
20-
{% raw %}
219
<h3>{% trans %}Other resources{% endtrans %}</h3>
2210
<ul>
2311
{# XXX: many of these should probably be merged in the main docs #}
24-
<li><a href="https://peps.python.org">{% trans %}PEP Index{% endtrans %}</a></li>
12+
<li><a href="https://peps.python.org/">{% trans %}PEP Index{% endtrans %}</a></li>
2513
<li><a href="https://wiki.python.org/moin/BeginnersGuide">{% trans %}Beginner's Guide{% endtrans %}</a></li>
2614
<li><a href="https://wiki.python.org/moin/PythonBooks">{% trans %}Book List{% endtrans %}</a></li>
2715
<li><a href="https://www.python.org/doc/av/">{% trans %}Audio/Visual Talks{% endtrans %}</a></li>
2816
<li><a href="https://devguide.python.org/">{% trans %}Python Developer’s Guide{% endtrans %}</a></li>
2917
</ul>
30-
{% endraw %}

0 commit comments

Comments
 (0)