Skip to content

Commit d06a9b6

Browse files
authored
Merge pull request #841 from krinsman/github_url_fix
Fixes #835 and #324
2 parents c629491 + d2c6d09 commit d06a9b6

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

nbviewer/providers/github/handlers.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import json
1010
import mimetypes
11+
import re
1112

1213
from tornado import (
1314
web,
@@ -76,14 +77,10 @@ def get(self, user, repo, path):
7677

7778

7879
class GitHubRedirectHandler(GithubClientMixin, BaseHandler):
79-
"""redirect github blob|tree|raw urls to /github/ API urls"""
80-
def get(self, user, repo, app, ref, path):
81-
if app == 'raw':
82-
app = 'blob'
83-
new_url = u'{format}/github/{user}/{repo}/{app}/{ref}/{path}'.format(
84-
format=self.format_prefix, user=user, repo=repo, app=app,
85-
ref=ref, path=path,
86-
)
80+
"""redirect github urls to /github/ API urls"""
81+
def get(self, url):
82+
new_url = u'{format}/github/{url}'.format(
83+
format=self.format_prefix, url=url)
8784
app_log.info("Redirecting %s to %s", self.request.uri, new_url)
8885
self.redirect(self.from_base(new_url))
8986

@@ -161,6 +158,11 @@ def get(self, user, repo, ref, path):
161158
)
162159
return
163160

161+
# Account for possibility that GitHub API redirects us to get more accurate breadcrumbs
162+
# See: https://github.com/jupyter/nbviewer/issues/324
163+
example_file_url = contents[0]['html_url']
164+
user, repo = re.match(r"^https://github\.com/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/.*", example_file_url).group('user', 'repo')
165+
164166
base_url = u"/github/{user}/{repo}/tree/{ref}".format(
165167
user=user, repo=repo, ref=ref,
166168
)
@@ -341,18 +343,18 @@ def default_handlers(handlers=[]):
341343
# fixing it here.
342344
# There are probably links in the wild that depend on these, so keep
343345
# these handlers for backwards compatibility.
344-
(r'/url[s]?/github\.com/([^\/]+)/([^\/]+)/(tree|blob|raw)/([^\/]+)/(.*)', GitHubRedirectHandler),
345-
(r'/url[s]?/raw\.?github\.com/([^\/]+)/([^\/]+)/(.*)', RawGitHubURLHandler),
346-
(r'/url[s]?/raw\.?githubusercontent\.com/([^\/]+)/([^\/]+)/(.*)', RawGitHubURLHandler),
346+
(r'/url[s]?/github\.com/(?P<url>.*)', GitHubRedirectHandler),
347+
(r'/url[s]?/raw\.?github\.com/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/(?P<path>.*)', RawGitHubURLHandler),
348+
(r'/url[s]?/raw\.?githubusercontent\.com/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/(?P<path>.*)', RawGitHubURLHandler),
347349
] + handlers + [
348350
(r'/github/([^\/]+)', AddSlashHandler),
349-
(r'/github/([^\/]+)/', GitHubUserHandler),
351+
(r'/github/(?P<user>[^\/]+)/', GitHubUserHandler),
350352
(r'/github/([^\/]+)/([^\/]+)', AddSlashHandler),
351-
(r'/github/([^\/]+)/([^\/]+)/', GitHubRepoHandler),
352-
(r'/github/([^\/]+)/([^\/]+)/blob/([^\/]+)/(.*)/', RemoveSlashHandler),
353-
(r'/github/([^\/]+)/([^\/]+)/blob/([^\/]+)/(.*)', GitHubBlobHandler),
353+
(r'/github/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/', GitHubRepoHandler),
354+
(r'/github/([^\/]+)/([^\/]+)/(?:blob|raw)/([^\/]+)/(.*)/', RemoveSlashHandler),
355+
(r'/github/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/(?:blob|raw)/(?P<ref>[^\/]+)/(?P<path>.*)', GitHubBlobHandler),
354356
(r'/github/([^\/]+)/([^\/]+)/tree/([^\/]+)', AddSlashHandler),
355-
(r'/github/([^\/]+)/([^\/]+)/tree/([^\/]+)/(.*)', GitHubTreeHandler),
357+
(r'/github/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/tree/(?P<ref>[^\/]+)/(?P<path>.*)', GitHubTreeHandler),
356358
]
357359

358360

nbviewer/providers/url/handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def default_handlers(handlers=[]):
9898
"""Tornado handlers"""
9999

100100
return handlers + [
101-
(r'/url([s]?)/([^/]+)/(.*)', URLHandler),
101+
(r'/url(?P<secure>[s]?)/(?P<netloc>[^/]+)/(?P<url>.*)', URLHandler),
102102
]
103103

104104

0 commit comments

Comments
 (0)