Skip to content

Commit

Permalink
Merge pull request #13 from chriskuehl/table-anchors
Browse files Browse the repository at this point in the history
Add anchor tags to metadata table rows
  • Loading branch information
chriskuehl authored Jun 4, 2024
2 parents 6ccae98 + aeaafa1 commit 0e3efe7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pypi_browser/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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')


Expand Down
21 changes: 21 additions & 0 deletions pypi_browser/static/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 6 additions & 2 deletions pypi_browser/templates/package_file.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ <h4>Package Metadata</h4>
</a>
</caption>
{% for key, values in metadata.items()|sort %}
{% set anchor_id = key|anchorize %}
{% for value in values %}
<tr>
<tr class="anchor-row">
{% if loop.index == 1 %}
<th rowspan="{{values|length}}">{{key}}</th>
<th rowspan="{{values|length}}">
<a id="{{anchor_id}}" href="#{{anchor_id}}" aria-hidden="true">🔗</a>
{{key}}
</th>
{% endif %}
<td class="font-monospace">{{value}}</td>
</tr>
Expand Down

0 comments on commit 0e3efe7

Please sign in to comment.