-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2869 from abravalheri/prefer-svg-images
[Docs] Replace PNG/ICO images with SVG
- Loading branch information
Showing
21 changed files
with
675 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PNG/ICO images replaced with SVG in the docs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added support to SVG "favicons" via "in-tree" Sphinx extension. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""'In-tree' sphinx extension to add icons/favicons to documentation""" | ||
import os | ||
from sphinx.util.fileutil import copy_asset_file | ||
|
||
|
||
IMAGES_DIR = "_images" # same used by .. image:: and .. picture:: | ||
|
||
|
||
def _prepare_image(pathto, confdir, outdir, icon_attrs): | ||
"""Copy icon files to the ``IMAGES_DIR`` and return a modified version of | ||
the icon attributes dict replacing ``file`` with the correct ``href``. | ||
""" | ||
icon = icon_attrs.copy() | ||
src = os.path.join(confdir, icon.pop("file")) | ||
if not os.path.exists(src): | ||
raise FileNotFoundError(f"icon {src!r} not found") | ||
|
||
dest = os.path.join(outdir, IMAGES_DIR) | ||
copy_asset_file(src, dest) # already compares if dest exists and is uptodate | ||
|
||
asset_name = os.path.basename(src) | ||
icon["href"] = pathto(f"{IMAGES_DIR}/{asset_name}", resource=True) | ||
return icon | ||
|
||
|
||
def _link_tag(attrs): | ||
return "<link " + " ".join(f'{k}="{v}"' for k, v in attrs.items()) + "/>" | ||
|
||
|
||
def _add_icons(app, _pagename, _templatename, context, doctree): | ||
"""Add multiple "favicons", not limited to PNG/ICO files""" | ||
# https://evilmartians.com/chronicles/how-to-favicon-in-2021-six-files-that-fit-most-needs | ||
# https://caniuse.com/link-icon-svg | ||
try: | ||
pathto = context['pathto'] | ||
except KeyError as ex: | ||
msg = f"{__name__} extension is supposed to be call in HTML contexts" | ||
raise ValueError(msg) from ex | ||
|
||
if doctree and "icons" in app.config: | ||
icons = [ | ||
_prepare_image(pathto, app.confdir, app.outdir, icon) | ||
for icon in app.config["icons"] | ||
] | ||
context["metatags"] += "\n".join(_link_tag(attrs) for attrs in icons) | ||
|
||
|
||
def setup(app): | ||
images_dir = os.path.join(app.outdir, IMAGES_DIR) | ||
os.makedirs(images_dir, exist_ok=True) | ||
|
||
app.add_config_value("icons", None, "html") | ||
app.connect("html-page-context", _add_icons) | ||
|
||
return { | ||
'parallel_read_safe': True, | ||
'parallel_write_safe': True, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.