Skip to content

Start a sitemap. #99

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

Merged
merged 2 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions build_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import sys
from datetime import datetime

import jinja2

HERE = Path(__file__).resolve().parent

try:
Expand Down Expand Up @@ -80,6 +82,10 @@ def __init__(self, name, branch, status, sphinx_version=DEFAULT_SPHINX_VERSION):
self.status = status
self.sphinx_version = sphinx_version

@property
def changefreq(self):
return {"EOL": "never", "security-fixes": "yearly"}.get(self.status, "daily")

@property
def url(self):
return "https://docs.python.org/{}/".format(self.name)
Expand Down Expand Up @@ -484,6 +490,24 @@ def build_venv(build_root, version):
return venv_path


def build_robots_txt(www_root):
with open(HERE / "templates" / "robots.txt") as robots_txt_template_file:
with open(os.path.join(www_root, "robots.txt"), "w") as robots_txt_file:
template = jinja2.Template(robots_txt_template_file.read())
robots_txt_file.write(
template.render(languages=LANGUAGES, versions=VERSIONS) + "\n"
)


def build_sitemap(www_root):
with open(HERE / "templates" / "sitemap.xml") as sitemap_template_file:
with open(os.path.join(www_root, "sitemap.xml"), "w") as sitemap_file:
template = jinja2.Template(sitemap_template_file.read())
sitemap_file.write(
template.render(languages=LANGUAGES, versions=VERSIONS) + "\n"
)


def copy_build_to_webroot(
build_root,
version,
Expand Down Expand Up @@ -768,6 +792,8 @@ def main():
)
if sentry_sdk:
sentry_sdk.capture_exception(err)
build_sitemap(args.www_root)
build_robots_txt(args.www_root)


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
sentry-sdk
jinja2
22 changes: 22 additions & 0 deletions templates/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Sitemap: https://docs.python.org/sitemap.xml

# Prevent development and old documentation from showing up in search results.
User-agent: *
Disallow: /dev
Disallow: /release

# Disallow EOL versions
Disallow: /2/
Disallow: /2.0/
Disallow: /2.1/
Disallow: /2.2/
Disallow: /2.3/
Disallow: /2.4/
Disallow: /2.5/
Disallow: /2.6/
Disallow: /2.7/
Disallow: /3.0/
Disallow: /3.1/
Disallow: /3.2/
Disallow: /3.3/
Disallow: /3.4/
22 changes: 22 additions & 0 deletions templates/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
{% for version in versions %}
{%- if version.status != "EOL" %}
<url>
<loc>https://docs.python.org/{{ version.name }}/</loc>
{% for language in languages -%}
<xhtml:link rel="alternate" hreflang="{{ language.iso639_tag.replace('_', '-') }}" href="https://docs.python.org/{{ language.tag }}/{{ version.name }}/"/>
{% endfor -%}
<changefreq>{{ version.changefreq }}</changefreq>
</url>
{% endif -%}
{% endfor %}
<url>
<loc>https://docs.python.org/3/</loc>
{% for language in languages -%}
<xhtml:link rel="alternate" hreflang="{{ language.iso639_tag.replace('_', '-') }}" href="https://docs.python.org/{{ language.tag }}/3/"/>
{% endfor -%}
<changefreq>daily</changefreq>
</url>
</urlset>