Skip to content

Commit cb768ff

Browse files
authored
Merge branch 'master' into step6
2 parents 5058005 + fffd462 commit cb768ff

File tree

2 files changed

+51
-14
lines changed

2 files changed

+51
-14
lines changed

nbviewer/providers/gist/handlers.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4+
import os
45
import json
56

67
from tornado import web
@@ -17,6 +18,7 @@
1718
clean_filename,
1819
quote,
1920
response_text,
21+
url_path_join,
2022
)
2123

2224
from ..github.handlers import GithubClientMixin
@@ -95,9 +97,14 @@ async def get(self, user, **namespace):
9597
notebooks=notebooks,
9698
description=gist['description'] or '',
9799
))
98-
provider_url = u"https://gist.github.com/{user}".format(user=user)
100+
if self.github_url == 'https://github.com/':
101+
gist_base_url = 'https://gist.github.com/'
102+
else:
103+
gist_base_url = url_path_join(self.github_url, 'gist/')
104+
provider_url = url_path_join(gist_base_url, u"{user}".format(user=user))
99105
html = self.render_usergists_template(entries=entries, user=user, provider_url=provider_url,
100106
prev_url=prev_url, next_url=next_url, **namespace
107+
101108
)
102109
await self.cache_and_finish(html)
103110

@@ -152,11 +159,15 @@ async def tree_get(self, user, gist_id, gist, files):
152159
e['class'] = 'fa-book'
153160
ipynbs.append(e)
154161
else:
155-
provider_url = u"https://gist.github.com/{user}/{gist_id}#file-{clean_name}".format(
162+
if self.github_url == 'https://github.com/':
163+
gist_base_url = 'https://gist.github.com/'
164+
else:
165+
gist_base_url = url_path_join(self.github_url, 'gist/')
166+
provider_url = url_path_join(gist_base_url, u"{user}/{gist_id}#file-{clean_name}".format(
156167
user=user,
157168
gist_id=gist_id,
158169
clean_name=clean_filename(file['filename']),
159-
)
170+
))
160171
e['url'] = provider_url
161172
e['class'] = 'fa-share'
162173
others.append(e)
@@ -298,9 +309,19 @@ def default_handlers(handlers=[], **handler_names):
298309

299310

300311
def uri_rewrites(rewrites=[]):
301-
return [
312+
gist_rewrites = [
302313
(r'^([a-f0-9]+)/?$',
303314
u'/{0}'),
304-
('^https?://gist.github.com/([^\/]+/)?([a-f0-9]+)/?$',
315+
(r'^https?://gist.github.com/([^\/]+/)?([a-f0-9]+)/?$',
305316
u'/{1}'),
306-
] + rewrites
317+
]
318+
# github enterprise
319+
if os.environ.get('GITHUB_API_URL', '') != '':
320+
gist_base_url = url_path_join(os.environ.get('GITHUB_API_URL').split('/api/v3')[0], 'gist/')
321+
gist_rewrites.extend([
322+
# Fetching the Gist ID which is embedded in the URL, but with a different base URL
323+
(r'^' + gist_base_url + r'([^\/]+/)?([a-f0-9]+)/?$',
324+
u'/{1}'),
325+
])
326+
327+
return gist_rewrites + rewrites

nbviewer/providers/github/handlers.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
base64_decode,
3030
quote,
3131
response_text,
32+
url_path_join,
3233
)
3334

3435
from .client import AsyncGitHubClient
@@ -286,9 +287,14 @@ class GitHubBlobHandler(GithubClientMixin, RenderingHandler):
286287
- directory, redirect to tree
287288
"""
288289
async def get_notebook_data(self, user, repo, ref, path):
289-
raw_url = u"https://raw.githubusercontent.com/{user}/{repo}/{ref}/{path}".format(
290-
user=user, repo=repo, ref=ref, path=quote(path)
291-
)
290+
if os.environ.get('GITHUB_API_URL', '') == '':
291+
raw_url = u"https://raw.githubusercontent.com/{user}/{repo}/{ref}/{path}".format(
292+
user=user, repo=repo, ref=ref, path=quote(path)
293+
)
294+
else: #Github Enterprise has a different URL pattern for accessing raw files
295+
raw_url = url_path_join(
296+
self.github_url, user, repo, 'raw', ref, quote(path)
297+
)
292298
blob_url = u"{github_url}{user}/{repo}/blob/{ref}/{path}".format(
293299
user=user, repo=repo, ref=ref, path=quote(path), github_url=self.github_url
294300
)
@@ -429,17 +435,27 @@ def uri_rewrites(rewrites=[]):
429435
]
430436
# github enterprise
431437
if os.environ.get('GITHUB_API_URL', '') != '':
432-
github_api_url = os.environ.get('GITHUB_API_URL')
438+
github_base_url = os.environ.get('GITHUB_API_URL').split('api/v3')[0]
433439

434440
github_rewrites.extend([
435441
# raw view
436-
(r'^' + github_api_url.split('/api/v3/')[0]
437-
+ '/([^\/]+)/([^\/]+)/raw/([^\/]+)/(.*)',
442+
(r'^' + github_base_url
443+
+ r'([^\/]+)/([^\/]+)/raw/([^\/]+)/(.*)',
438444
u'/github/{0}/{1}/blob/{2}/{3}'),
439445

440446
# trees & blobs
441-
(r'^' + github_api_url.split('/api/v3/')[0]
442-
+ '/([\w\-]+)/([^\/]+)/(blob|tree)/(.*)$',
447+
(r'^' + github_base_url
448+
+ r'([\w\-]+)/([^\/]+)/(blob|tree)/(.*)$',
443449
u'/github/{0}/{1}/{2}/{3}'),
450+
451+
# user/repo
452+
(r'^' + github_base_url
453+
+ r'([\w\-]+)/([^\/]+)/?$',
454+
u'/github/{0}/{1}/tree/master'),
455+
456+
# user
457+
(r'^' + github_base_url
458+
+ r'([\w\-]+)/?$',
459+
u'/github/{0}/'),
444460
])
445461
return rewrites + github_rewrites

0 commit comments

Comments
 (0)