Skip to content

Commit

Permalink
create_nojekyll_and_cname: clean-up CNAME file
Browse files Browse the repository at this point in the history
I noticed that while running `pytest --random` and the extension
should wipe CNAME file if a configuration changes and it should
not be created.

Related: sphinx-doc#11285
  • Loading branch information
marxin committed Apr 6, 2023
1 parent 9299003 commit c80bee2
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions sphinx/ext/githubpages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from __future__ import annotations

import os
import urllib
from pathlib import Path
from typing import Any

import sphinx
Expand All @@ -13,17 +13,18 @@

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()
(Path(app.builder.outdir) / '.nojekyll').touch()

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)
cname_path = Path(app.builder.outdir) / 'CNAME'
domain = urllib.parse.urlparse(html_baseurl).hostname if html_baseurl else None
if domain and not domain.endswith(".github.io"):
with cname_path.open('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:
cname_path.unlink(missing_ok=True)


def setup(app: Sphinx) -> dict[str, Any]:
Expand Down

0 comments on commit c80bee2

Please sign in to comment.