Skip to content

Commit fca0134

Browse files
committed
Normalize URLs to GitHub project repos
In particular we want to remove any trailing `.git` from the project URL, since this results in broken links when we contruct the "Fork" and "Open Issues" links in the plugin page. (While `https://github.com/_owner_/_project_.git` will work to get to the repo, `https://github.com/_owner_/_project_.git/fork` is a 404.)
1 parent 850e52f commit fca0134

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

packages/project-data/lektor_project_data.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import cgi
3+
import re
34

45
import readme_renderer.markdown
56
import readme_renderer.rst
@@ -17,6 +18,24 @@
1718
}
1819

1920

21+
def normalize_url(url):
22+
"""Normalize project home page URLs."""
23+
# Normalize any URLS to GitHub project repos.
24+
m = re.match(
25+
r"""
26+
https?://(?:www\.)?github\.com
27+
/ (?P<owner>[^/]+)
28+
/ (?P<project>[^/]+?) (?:\.git)
29+
/? \Z
30+
""",
31+
url,
32+
flags=re.VERBOSE
33+
)
34+
if m:
35+
return "https://github.com/{owner}/{project}".format(**m.groupdict())
36+
return url
37+
38+
2039
class ProjectDataPlugin(Plugin):
2140
name = 'Project Data'
2241
description = u'Retrieve project information from PyPI.'
@@ -71,6 +90,8 @@ def package_data(self, name, entry_point=None):
7190
self.data['description'], self.data['description_content_type'])
7291
if not self.data.get('home_page'):
7392
self.data['home_page'] = f'https://pypi.org/project/{name}/'
93+
else:
94+
self.data['home_page'] = normalize_url(self.data['home_page'])
7495

7596
def github_data(self, owner=None, repo=None):
7697
url = 'https://api.github.com/repos/{}/{}'.format(owner, repo)

0 commit comments

Comments
 (0)