Skip to content

Commit e2f7ee9

Browse files
authored
Merge pull request #866 from ivan-gomes/ghe-gist-patch
Fix Gist support with GHE
2 parents 4933ef1 + 4691b92 commit e2f7ee9

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
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, gen
@@ -17,6 +18,7 @@
1718
clean_filename,
1819
quote,
1920
response_text,
21+
url_path_join,
2022
)
2123

2224
from ..github.handlers import GithubClientMixin
@@ -96,9 +98,14 @@ def get(self, user, **namespace):
9698
notebooks=notebooks,
9799
description=gist['description'] or '',
98100
))
99-
provider_url = u"https://gist.github.com/{user}".format(user=user)
101+
if self.github_url == 'https://github.com/':
102+
gist_base_url = 'https://gist.github.com/'
103+
else:
104+
gist_base_url = url_path_join(self.github_url, 'gist/')
105+
provider_url = url_path_join(gist_base_url, u"{user}".format(user=user))
100106
html = self.render_usergists_template(entries=entries, user=user, provider_url=provider_url,
101107
prev_url=prev_url, next_url=next_url, **namespace
108+
102109
)
103110
yield self.cache_and_finish(html)
104111

@@ -155,11 +162,15 @@ def tree_get(self, user, gist_id, gist, files):
155162
e['class'] = 'fa-book'
156163
ipynbs.append(e)
157164
else:
158-
provider_url = u"https://gist.github.com/{user}/{gist_id}#file-{clean_name}".format(
165+
if self.github_url == 'https://github.com/':
166+
gist_base_url = 'https://gist.github.com/'
167+
else:
168+
gist_base_url = url_path_join(self.github_url, 'gist/')
169+
provider_url = url_path_join(gist_base_url, u"{user}/{gist_id}#file-{clean_name}".format(
159170
user=user,
160171
gist_id=gist_id,
161172
clean_name=clean_filename(file['filename']),
162-
)
173+
))
163174
e['url'] = provider_url
164175
e['class'] = 'fa-share'
165176
others.append(e)
@@ -305,9 +316,19 @@ def default_handlers(handlers=[], **handler_names):
305316

306317

307318
def uri_rewrites(rewrites=[]):
308-
return [
319+
gist_rewrites = [
309320
(r'^([a-f0-9]+)/?$',
310321
u'/{0}'),
311-
('^https?://gist.github.com/([^\/]+/)?([a-f0-9]+)/?$',
322+
(r'^https?://gist.github.com/([^\/]+/)?([a-f0-9]+)/?$',
312323
u'/{1}'),
313-
] + rewrites
324+
]
325+
# github enterprise
326+
if os.environ.get('GITHUB_API_URL', '') != '':
327+
gist_base_url = url_path_join(os.environ.get('GITHUB_API_URL').split('/api/v3')[0], 'gist/')
328+
gist_rewrites.extend([
329+
# Fetching the Gist ID which is embedded in the URL, but with a different base URL
330+
(r'^' + gist_base_url + r'([^\/]+/)?([a-f0-9]+)/?$',
331+
u'/{1}'),
332+
])
333+
334+
return gist_rewrites + rewrites

0 commit comments

Comments
 (0)