From 26eb2ef7c4fe3de17b16bffb47a2eb290445cf16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Li=C5=A1ka?= Date: Thu, 6 Apr 2023 23:46:50 +0200 Subject: [PATCH] Clean up the CNAME file in ``sphinx.ext.githubpages`` (#11295) Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com> --- sphinx/ext/githubpages.py | 51 +++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/sphinx/ext/githubpages.py b/sphinx/ext/githubpages.py index 1e0cdc968..27e567fa4 100644 --- a/sphinx/ext/githubpages.py +++ b/sphinx/ext/githubpages.py @@ -3,7 +3,7 @@ from __future__ import annotations import os -import urllib +import urllib.parse from typing import Any import sphinx @@ -11,19 +11,44 @@ from sphinx.application import Sphinx from sphinx.environment import BuildEnvironment -def create_nojekyll_and_cname(app: Sphinx, env: BuildEnvironment) -> None: - if app.builder.format == 'html': - open(os.path.join(app.builder.outdir, '.nojekyll'), 'wb').close() +def _get_domain_from_url(url: str) -> str: + """Get the domain from a URL.""" + return url and urllib.parse.urlparse(url).hostname or '' - html_baseurl = app.config.html_baseurl - if html_baseurl: - domain = urllib.parse.urlparse(html_baseurl).hostname - if domain and not domain.endswith(".github.io"): - with open(os.path.join(app.builder.outdir, 'CNAME'), 'w', - encoding="utf-8") as f: - # NOTE: don't write a trailing newline. The `CNAME` file that's - # auto-generated by the Github UI doesn't have one. - f.write(domain) + +def create_nojekyll_and_cname(app: Sphinx, env: BuildEnvironment) -> None: + """Manage the ``.nojekyll`` and ``CNAME`` files for GitHub Pages. + + For HTML-format builders (e.g. 'html', 'dirhtml') we unconditionally create + the ``.nojekyll`` file to signal that GitHub Pages should not run Jekyll + processing. + + If the :confval:`html_baseurl` option is set, we also create a CNAME file + with the domain from ``html_baseurl``, so long as it is not a ``github.io`` + domain. + + If this extension is loaded and the domain in ``html_baseurl`` no longer + requires a CNAME file, we remove any existing ``CNAME`` files from the + output directory. + """ + if app.builder.format != 'html': + return + + open(os.path.join(app.builder.outdir, '.nojekyll'), 'wb').close() + cname_path = os.path.join(app.builder.outdir, 'CNAME') + + domain = _get_domain_from_url(app.config.html_baseurl) + # Filter out GitHub Pages domains, as they do not require CNAME files. + if domain and not domain.endswith(".github.io"): + with open(cname_path, 'w', encoding="utf-8") as f: + # NOTE: don't write a trailing newline. The `CNAME` file that's + # auto-generated by the GitHub UI doesn't have one. + f.write(domain) + else: + try: + os.unlink(cname_path) + except FileNotFoundError: + pass def setup(app: Sphinx) -> dict[str, Any]: