From aeaafa14a92b963583238146c18f26a8da6975b1 Mon Sep 17 00:00:00 2001 From: Chris Kuehl Date: Mon, 3 Jun 2024 22:51:19 -0500 Subject: [PATCH] Add anchor tags to metadata table rows --- pypi_browser/app.py | 6 ++++++ pypi_browser/static/site.css | 21 +++++++++++++++++++++ pypi_browser/templates/package_file.html | 8 ++++++-- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pypi_browser/app.py b/pypi_browser/app.py index bbcd7cd..1ed1a59 100644 --- a/pypi_browser/app.py +++ b/pypi_browser/app.py @@ -95,6 +95,11 @@ def _pluralize(n: int) -> str: return '' if n == 1 else 's' +def _anchorize(title: str) -> str: + """Convert metadata title into HTML anchor ID.""" + return re.sub(r'\-+', '-', re.sub(r'[^a-z0-9]', '-', title.lower())) + + def _human_size(size: int) -> str: if size >= ONE_GB: return f'{size / ONE_GB:.1f} GiB' @@ -108,6 +113,7 @@ def _human_size(size: int) -> str: templates.env.filters['human_size'] = _human_size templates.env.filters['pluralize'] = _pluralize +templates.env.filters['anchorize'] = _anchorize templates.env.globals['pypi_browser_version'] = importlib.metadata.version('pypi-browser-webapp') diff --git a/pypi_browser/static/site.css b/pypi_browser/static/site.css index 268519b..a795437 100644 --- a/pypi_browser/static/site.css +++ b/pypi_browser/static/site.css @@ -19,3 +19,24 @@ html { .page-package-file-archive-path .codeview pre { font-size: 14px; } + +.anchor-row { + position: relative; +} + +.anchor-row a { + display: none; + padding-right: 5px; + text-decoration: none; + color: #bbb; +} + +.anchor-row a:hover { + color: #999; +} + +.anchor-row:hover a { + position: absolute; + right: 100%; + display: inline; +} diff --git a/pypi_browser/templates/package_file.html b/pypi_browser/templates/package_file.html index 3d78d2e..c7bfb4b 100644 --- a/pypi_browser/templates/package_file.html +++ b/pypi_browser/templates/package_file.html @@ -36,10 +36,14 @@

Package Metadata

{% for key, values in metadata.items()|sort %} + {% set anchor_id = key|anchorize %} {% for value in values %} - + {% if loop.index == 1 %} - {{key}} + + + {{key}} + {% endif %} {{value}}