Skip to content

Commit

Permalink
Remove betty.ico and generate favicon.ico from the project logo (#2143)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra authored Oct 18, 2024
1 parent 09ab7a7 commit 01c80be
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
Binary file removed betty/assets/public/static/betty.ico
Binary file not shown.
49 changes: 33 additions & 16 deletions betty/project/generate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
)

import aiofiles
from PIL import Image
from aiofiles.os import makedirs
from math import floor

Expand Down Expand Up @@ -89,7 +90,7 @@ async def generate(project: Project) -> None:

# The static public assets may be overridden depending on the number of locales rendered, so ensure they are
# generated before anything else.
await _generate_static_public(job_context)
await _generate_static_public_assets(job_context)

jobs = []
log_job: Task[None] | None = None
Expand Down Expand Up @@ -164,6 +165,8 @@ async def _run_jobs(
) -> AsyncIterator[Coroutine[Any, Any, None]]:
project = job_context.project
semaphore = Semaphore(512)
yield _run_job(semaphore, _generate_favicon, job_context)
yield _run_job(semaphore, _generate_json_error_responses, project)
yield _run_job(semaphore, _generate_dispatch, job_context)
yield _run_job(semaphore, _generate_robots_txt, job_context)
yield _run_job(semaphore, _generate_sitemap, job_context)
Expand All @@ -173,7 +176,9 @@ async def _run_jobs(
locales = list(project.configuration.locales.keys())

for locale in locales:
yield _run_job(semaphore, _generate_public, job_context, locale)
yield _run_job(
semaphore, _generate_localized_public_assets, job_context, locale
)

async for entity_type in model.ENTITY_TYPE_REPOSITORY:
if not issubclass(entity_type, UserFacingEntity):
Expand Down Expand Up @@ -217,7 +222,7 @@ async def _generate_dispatch(job_context: ProjectContext) -> None:
await project.event_dispatcher.dispatch(GenerateSiteEvent(job_context))


async def _generate_public_asset(
async def _generate_localized_public_asset(
asset_path: Path, project: Project, job_context: ProjectContext, locale: str
) -> None:
assets = await project.assets
Expand All @@ -235,7 +240,7 @@ async def _generate_public_asset(
)


async def _generate_public(
async def _generate_localized_public_assets(
job_context: ProjectContext,
locale: str,
) -> None:
Expand All @@ -251,7 +256,7 @@ async def _generate_public(
)
await gather(
*[
_generate_public_asset(asset_path, project, job_context, locale)
_generate_localized_public_asset(asset_path, project, job_context, locale)
async for asset_path in assets.walk(Path("public") / "localized")
]
)
Expand All @@ -271,7 +276,7 @@ async def _generate_static_public_asset(
await renderer.render_file(file_destination_path, job_context=job_context)


async def _generate_static_public(
async def _generate_static_public_assets(
job_context: ProjectContext,
) -> None:
project = job_context.project
Expand All @@ -283,19 +288,31 @@ async def _generate_static_public(
*[
_generate_static_public_asset(asset_path, project, job_context)
async for asset_path in assets.walk(Path("public") / "static")
],
# Ensure favicon.ico exists, otherwise servers of Betty sites would log
# many a 404 Not Found for it, because some clients eagerly try to see
# if it exists.
to_thread(
shutil.copy2,
await assets.get(Path("public") / "static" / "betty.ico"),
project.configuration.www_directory_path / "favicon.ico",
),
_generate_json_error_responses(project),
]
)


async def _generate_favicon(
job_context: ProjectContext,
) -> None:
"""
Ensure favicon.ico exists.
Without a favicon.ico, servers of Betty sites would log many a 404 Not Found for it, because some clients eagerly
try to see if it exists.
"""
project = job_context.project
await to_thread(
__generate_favicon, project.logo, project.configuration.www_directory_path
)


def __generate_favicon(logo_file_path: Path, www_directory_path: Path) -> None:
with open(logo_file_path, "rb") as logo_f:
image = Image.open(logo_f)
image.save(www_directory_path / "favicon.ico")


async def _generate_json_error_responses(project: Project) -> None:
for code, message in [
(401, _("I'm sorry, dear, but it seems you're not logged in.")),
Expand Down
10 changes: 3 additions & 7 deletions documentation/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import betty
from betty import fs
from betty.assets import AssetRepository
from betty.fs import ROOT_DIRECTORY_PATH
from betty.fs import ASSETS_DIRECTORY_PATH
from betty.locale.localizer import LocalizerRepository

betty_replacements: dict[str, str] = {}
Expand Down Expand Up @@ -38,12 +38,8 @@
"python": ("https://docs.python.org/3/", None),
"referencing": ("https://referencing.readthedocs.io/en/stable/", None),
}
html_favicon = str(
ROOT_DIRECTORY_PATH / "betty" / "assets" / "public" / "static" / "betty.ico"
)
html_logo = str(
ROOT_DIRECTORY_PATH / "betty" / "assets" / "public" / "static" / "betty-32x32.png"
)
html_favicon = str(ASSETS_DIRECTORY_PATH / "public" / "static" / "betty-512x512.png")
html_logo = str(ASSETS_DIRECTORY_PATH / "public" / "static" / "betty-512x512.png")
html_context = {
"display_github": True,
"github_user": "bartfeenstra",
Expand Down

0 comments on commit 01c80be

Please sign in to comment.