Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Replace PNG/ICO images with SVG #2869

Merged
merged 6 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"""
jaraco marked this conversation as resolved.
Show resolved Hide resolved
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe keep it pointing at SVG as a fallback?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we agree in a good file structure, I can test this.
I suppose it will work fine, although it might obfuscate the setup for multiple sizes in some browser.

html_logo = "images/logo.svg"

html_theme_options = {
"sidebar_hide_name": True,
Expand Down Expand Up @@ -171,3 +173,26 @@
towncrier_draft_include_empty = False

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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call the file properly instead of having a comment only in one place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @webknjaz, I am not sure if I understood correctly your comment.
Do you mean having a "relative_path" instead of "file" in the dictionary? That would make sense...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'm talking about the filename. It's not cool that the file is called favicon.svg and needs a code comment above explaining what it is. This information should be present in a file name, treat it as a variable in code. If the name is self-explanatory, then it'd be useful when checking out the HTML output, or just opening the URL in a browser, or while browsing the repository. A name like favicon does not give any hint about what the difference is from the other SVG file, nor it points at the relation between this and logo-symbol-only.svg.
You've added two code comments for each entry. The first one is quite useful because it explains the motivation but this second one has both useful and pointless bits. If a code comment has to include explanations about what the things are, you're probably calling those things wrong names.

"sizes": "16x16 24x24 32x32 48x48"
},
# rel="apple-touch-icon" does not support SVG yet
]
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
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
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
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.