Skip to content

Commit

Permalink
Merge pull request #432 from bollwyvl/unicode-body
Browse files Browse the repository at this point in the history
Force UTF-8 on notebooks from truncated gists
  • Loading branch information
Carreau committed Mar 21, 2015
2 parents 5b0bfd8 + 87d3f67 commit bf0c90c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion nbviewer/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ def get(self, user, gist_id, filename=''):
if file['truncated']:
app_log.debug("Gist %s/%s truncated, fetching %s", gist_id, filename, file['raw_url'])
response = yield self.fetch(file['raw_url'])
content = response_text(response)
content = response_text(response, encoding='utf-8')
else:
content = file['content']

Expand Down
16 changes: 13 additions & 3 deletions nbviewer/tests/test_gist.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
#-----------------------------------------------------------------------------
# Copyright (C) 2013 The IPython Development Team
#
Expand Down Expand Up @@ -31,28 +32,37 @@ def test_gist_list(self):
self.assertEqual(r.status_code, 200)
html = r.text
self.assertIn('<th>Name</th>', html)

def test_multifile_gist(self):
url = self.url('7518294', 'Untitled0.ipynb')
r = requests.get(url)
self.assertEqual(r.status_code, 200)
html = r.text
self.assertIn('Download Notebook', html)

def test_anonymous_gist(self):
url = self.url('gist/4465051')
r = requests.get(url)
self.assertEqual(r.status_code, 200)
html = r.text
self.assertIn('Download Notebook', html)

def test_gist_unicode(self):
url = self.url('gist/amueller/3974344')
r = requests.get(url)
self.assertEqual(r.status_code, 200)
html = r.text
self.assertIn('<th>Name</th>', html)

def test_gist_unicode_content(self):
url = self.url('gist/ocefpaf/cf023a8db7097bd9fe92')
r = requests.get(url)
self.assertEqual(r.status_code, 200)
html = r.text
self.assertNotIn('param&#195;&#169;trica', html)
self.assertIn('param&#233;trica', html)



class FormatHTMLGistTestCase(GistTestCase, FormatHTMLMixin):
pass
8 changes: 6 additions & 2 deletions nbviewer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,13 @@ def get_encoding_from_headers(headers):
if 'text' in content_type:
return 'ISO-8859-1'

def response_text(response):
def response_text(response, encoding=None):
"""mimic requests.text property, but for plain HTTPResponse"""
encoding = get_encoding_from_headers(response.headers) or 'utf-8'
encoding = (
encoding or
get_encoding_from_headers(response.headers) or
'utf-8'
)
return response.body.decode(encoding, 'replace')

# parse_header_links from requests.util
Expand Down

0 comments on commit bf0c90c

Please sign in to comment.