Skip to content

Commit 4691b92

Browse files
committed
Derive Gist base URL from GitHub url instead of introducing a new environment variable.
1 parent 0ddbaf1 commit 4691b92

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

nbviewer/providers/gist/handlers.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
clean_filename,
1919
quote,
2020
response_text,
21+
url_path_join,
2122
)
2223

2324
from ..github.handlers import GithubClientMixin
@@ -97,8 +98,11 @@ def get(self, user, **namespace):
9798
notebooks=notebooks,
9899
description=gist['description'] or '',
99100
))
100-
gist_url = os.environ.get('GIST_URL', 'https://gist.github.com/')
101-
provider_url = gist_url + u"{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))
102106
html = self.render_usergists_template(entries=entries, user=user, provider_url=provider_url,
103107
prev_url=prev_url, next_url=next_url, **namespace
104108

@@ -158,12 +162,15 @@ def tree_get(self, user, gist_id, gist, files):
158162
e['class'] = 'fa-book'
159163
ipynbs.append(e)
160164
else:
161-
gist_url = os.environ.get('GIST_URL', 'https://gist.github.com/')
162-
provider_url = gist_url + u"{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(
163170
user=user,
164171
gist_id=gist_id,
165172
clean_name=clean_filename(file['filename']),
166-
)
173+
))
167174
e['url'] = provider_url
168175
e['class'] = 'fa-share'
169176
others.append(e)
@@ -316,11 +323,11 @@ def uri_rewrites(rewrites=[]):
316323
u'/{1}'),
317324
]
318325
# github enterprise
319-
if os.environ.get('GIST_URL', '') != '':
320-
gist_url = os.environ.get('GIST_URL')
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/')
321328
gist_rewrites.extend([
322-
# embedded in URL
323-
(r'^' + gist_url + r'([^\/]+/)?([a-f0-9]+)/?$',
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]+)/?$',
324331
u'/{1}'),
325332
])
326333

0 commit comments

Comments
 (0)