|
8 | 8 | import os
|
9 | 9 | import json
|
10 | 10 | import mimetypes
|
| 11 | +import re |
11 | 12 |
|
12 | 13 | from tornado import (
|
13 | 14 | web,
|
@@ -76,14 +77,10 @@ def get(self, user, repo, path):
|
76 | 77 |
|
77 | 78 |
|
78 | 79 | 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) |
87 | 84 | app_log.info("Redirecting %s to %s", self.request.uri, new_url)
|
88 | 85 | self.redirect(self.from_base(new_url))
|
89 | 86 |
|
@@ -161,6 +158,11 @@ def get(self, user, repo, ref, path):
|
161 | 158 | )
|
162 | 159 | return
|
163 | 160 |
|
| 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 | + |
164 | 166 | base_url = u"/github/{user}/{repo}/tree/{ref}".format(
|
165 | 167 | user=user, repo=repo, ref=ref,
|
166 | 168 | )
|
@@ -341,18 +343,18 @@ def default_handlers(handlers=[]):
|
341 | 343 | # fixing it here.
|
342 | 344 | # There are probably links in the wild that depend on these, so keep
|
343 | 345 | # 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), |
347 | 349 | ] + handlers + [
|
348 | 350 | (r'/github/([^\/]+)', AddSlashHandler),
|
349 |
| - (r'/github/([^\/]+)/', GitHubUserHandler), |
| 351 | + (r'/github/(?P<user>[^\/]+)/', GitHubUserHandler), |
350 | 352 | (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), |
354 | 356 | (r'/github/([^\/]+)/([^\/]+)/tree/([^\/]+)', AddSlashHandler),
|
355 |
| - (r'/github/([^\/]+)/([^\/]+)/tree/([^\/]+)/(.*)', GitHubTreeHandler), |
| 357 | + (r'/github/(?P<user>[^\/]+)/(?P<repo>[^\/]+)/tree/(?P<ref>[^\/]+)/(?P<path>.*)', GitHubTreeHandler), |
356 | 358 | ]
|
357 | 359 |
|
358 | 360 |
|
|
0 commit comments