Skip to content

Commit f9e1344

Browse files
authored
Start a sitemap. (#99)
1 parent c23b401 commit f9e1344

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

build_docs.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import sys
4949
from datetime import datetime
5050

51+
import jinja2
52+
5153
HERE = Path(__file__).resolve().parent
5254

5355
try:
@@ -80,6 +82,10 @@ def __init__(self, name, branch, status, sphinx_version=DEFAULT_SPHINX_VERSION):
8082
self.status = status
8183
self.sphinx_version = sphinx_version
8284

85+
@property
86+
def changefreq(self):
87+
return {"EOL": "never", "security-fixes": "yearly"}.get(self.status, "daily")
88+
8389
@property
8490
def url(self):
8591
return "https://docs.python.org/{}/".format(self.name)
@@ -484,6 +490,24 @@ def build_venv(build_root, version):
484490
return venv_path
485491

486492

493+
def build_robots_txt(www_root):
494+
with open(HERE / "templates" / "robots.txt") as robots_txt_template_file:
495+
with open(os.path.join(www_root, "robots.txt"), "w") as robots_txt_file:
496+
template = jinja2.Template(robots_txt_template_file.read())
497+
robots_txt_file.write(
498+
template.render(languages=LANGUAGES, versions=VERSIONS) + "\n"
499+
)
500+
501+
502+
def build_sitemap(www_root):
503+
with open(HERE / "templates" / "sitemap.xml") as sitemap_template_file:
504+
with open(os.path.join(www_root, "sitemap.xml"), "w") as sitemap_file:
505+
template = jinja2.Template(sitemap_template_file.read())
506+
sitemap_file.write(
507+
template.render(languages=LANGUAGES, versions=VERSIONS) + "\n"
508+
)
509+
510+
487511
def copy_build_to_webroot(
488512
build_root,
489513
version,
@@ -768,6 +792,8 @@ def main():
768792
)
769793
if sentry_sdk:
770794
sentry_sdk.capture_exception(err)
795+
build_sitemap(args.www_root)
796+
build_robots_txt(args.www_root)
771797

772798

773799
if __name__ == "__main__":

requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
sentry-sdk
2+
jinja2

templates/robots.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Sitemap: https://docs.python.org/sitemap.xml
2+
3+
# Prevent development and old documentation from showing up in search results.
4+
User-agent: *
5+
Disallow: /dev
6+
Disallow: /release
7+
8+
# Disallow EOL versions
9+
Disallow: /2/
10+
Disallow: /2.0/
11+
Disallow: /2.1/
12+
Disallow: /2.2/
13+
Disallow: /2.3/
14+
Disallow: /2.4/
15+
Disallow: /2.5/
16+
Disallow: /2.6/
17+
Disallow: /2.7/
18+
Disallow: /3.0/
19+
Disallow: /3.1/
20+
Disallow: /3.2/
21+
Disallow: /3.3/
22+
Disallow: /3.4/

templates/sitemap.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
3+
xmlns:xhtml="http://www.w3.org/1999/xhtml">
4+
{% for version in versions %}
5+
{%- if version.status != "EOL" %}
6+
<url>
7+
<loc>https://docs.python.org/{{ version.name }}/</loc>
8+
{% for language in languages -%}
9+
<xhtml:link rel="alternate" hreflang="{{ language.iso639_tag.replace('_', '-') }}" href="https://docs.python.org/{{ language.tag }}/{{ version.name }}/"/>
10+
{% endfor -%}
11+
<changefreq>{{ version.changefreq }}</changefreq>
12+
</url>
13+
{% endif -%}
14+
{% endfor %}
15+
<url>
16+
<loc>https://docs.python.org/3/</loc>
17+
{% for language in languages -%}
18+
<xhtml:link rel="alternate" hreflang="{{ language.iso639_tag.replace('_', '-') }}" href="https://docs.python.org/{{ language.tag }}/3/"/>
19+
{% endfor -%}
20+
<changefreq>daily</changefreq>
21+
</url>
22+
</urlset>

0 commit comments

Comments
 (0)