Skip to content

Commit 0e3efe7

Browse files
authored
Merge pull request #13 from chriskuehl/table-anchors
Add anchor tags to metadata table rows
2 parents 6ccae98 + aeaafa1 commit 0e3efe7

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

pypi_browser/app.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ def _pluralize(n: int) -> str:
9595
return '' if n == 1 else 's'
9696

9797

98+
def _anchorize(title: str) -> str:
99+
"""Convert metadata title into HTML anchor ID."""
100+
return re.sub(r'\-+', '-', re.sub(r'[^a-z0-9]', '-', title.lower()))
101+
102+
98103
def _human_size(size: int) -> str:
99104
if size >= ONE_GB:
100105
return f'{size / ONE_GB:.1f} GiB'
@@ -108,6 +113,7 @@ def _human_size(size: int) -> str:
108113

109114
templates.env.filters['human_size'] = _human_size
110115
templates.env.filters['pluralize'] = _pluralize
116+
templates.env.filters['anchorize'] = _anchorize
111117
templates.env.globals['pypi_browser_version'] = importlib.metadata.version('pypi-browser-webapp')
112118

113119

pypi_browser/static/site.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,24 @@ html {
1919
.page-package-file-archive-path .codeview pre {
2020
font-size: 14px;
2121
}
22+
23+
.anchor-row {
24+
position: relative;
25+
}
26+
27+
.anchor-row a {
28+
display: none;
29+
padding-right: 5px;
30+
text-decoration: none;
31+
color: #bbb;
32+
}
33+
34+
.anchor-row a:hover {
35+
color: #999;
36+
}
37+
38+
.anchor-row:hover a {
39+
position: absolute;
40+
right: 100%;
41+
display: inline;
42+
}

pypi_browser/templates/package_file.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ <h4>Package Metadata</h4>
3636
</a>
3737
</caption>
3838
{% for key, values in metadata.items()|sort %}
39+
{% set anchor_id = key|anchorize %}
3940
{% for value in values %}
40-
<tr>
41+
<tr class="anchor-row">
4142
{% if loop.index == 1 %}
42-
<th rowspan="{{values|length}}">{{key}}</th>
43+
<th rowspan="{{values|length}}">
44+
<a id="{{anchor_id}}" href="#{{anchor_id}}" aria-hidden="true">🔗</a>
45+
{{key}}
46+
</th>
4347
{% endif %}
4448
<td class="font-monospace">{{value}}</td>
4549
</tr>

0 commit comments

Comments
 (0)