Skip to content

Commit

Permalink
Merge pull request #2869 from abravalheri/prefer-svg-images
Browse files Browse the repository at this point in the history
[Docs] Replace PNG/ICO images with SVG
  • Loading branch information
jaraco authored Nov 14, 2021
2 parents 5141c42 + 4b980ef commit 3f9bbce
Show file tree
Hide file tree
Showing 21 changed files with 675 additions and 35 deletions.
1 change: 1 addition & 0 deletions changelog.d/2867.doc.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PNG/ICO images replaced with SVG in the docs.
1 change: 1 addition & 0 deletions changelog.d/2867.doc.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support to SVG "favicons" via "in-tree" Sphinx extension.
58 changes: 58 additions & 0 deletions docs/_ext/_custom_icons.py
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,
}
29 changes: 27 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
import sys

extensions = ['sphinx.ext.autodoc', 'jaraco.packaging.sphinx', 'rst.linker']

master_doc = "index"
Expand Down Expand Up @@ -101,8 +104,7 @@

# HTML theme
html_theme = 'furo'
html_logo = "images/logo.png"
html_favicon = "images/favicon.ico"
html_logo = "images/logo.svg"

html_theme_options = {
"sidebar_hide_name": True,
Expand Down Expand Up @@ -172,4 +174,27 @@

extensions += ['jaraco.tidelift']

# Add icons (aka "favicons") to documentation
sys.path.append(os.path.join(os.path.dirname(__file__), '_ext'))
extensions += ['_custom_icons']

# List of dicts with <link> HTML attributes
# as defined in https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link
# except that ``file`` gets replaced with the correct ``href``
icons = [
{ # "Catch-all" goes first, otherwise some browsers will overwrite
"rel": "icon",
"type": "image/svg+xml",
"file": "images/logo-symbol-only.svg",
"sizes": "any"
},
{ # Version with thicker strokes for better visibility at smaller sizes
"rel": "icon",
"type": "image/svg+xml",
"file": "images/favicon.svg",
"sizes": "16x16 24x24 32x32 48x48"
},
# rel="apple-touch-icon" does not support SVG yet
]

intersphinx_mapping['pip'] = 'https://pip.pypa.io/en/latest', None
2 changes: 1 addition & 1 deletion docs/images/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ on the circumstances:

The following image illustrate these alternatives:

.. image:: logo-demo-editable-inkscape.png
.. image:: logo-demo.svg
:align: center

Please refer to the SVG files in the `setuptools repository`_ for the specific
Expand Down
Binary file removed docs/images/banner-640x320.png
Binary file not shown.
Binary file removed docs/images/banner-negative-640x320.png
Binary file not shown.
Binary file removed docs/images/favicon.ico
Binary file not shown.
52 changes: 32 additions & 20 deletions docs/images/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/images/favicon.xcf
Binary file not shown.
Binary file removed docs/images/logo-demo-editable-inkscape.png
Binary file not shown.
543 changes: 543 additions & 0 deletions docs/images/logo-demo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/images/logo-editable-inkscape.png
Binary file not shown.
Binary file removed docs/images/logo-inline-negative.png
Binary file not shown.
Binary file removed docs/images/logo-inline.png
Binary file not shown.
Binary file removed docs/images/logo-negative.png
Binary file not shown.
Binary file removed docs/images/logo-over-white.png
Binary file not shown.
Binary file removed docs/images/logo-symbol-only.png
Binary file not shown.
24 changes: 12 additions & 12 deletions docs/images/logo-symbol-only.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/images/logo-text-only.png
Binary file not shown.
Binary file removed docs/images/logo.png
Binary file not shown.

0 comments on commit 3f9bbce

Please sign in to comment.