Klaus Demo ~jonashaag/klaus / 2d195c7
Fix #43: --site-name should accept non-ASCII values Jonas Haag 9 years ago
1 changed file(s) with 14 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
33 import time
44 import datetime
55 import mimetypes
6 import locale
7 try:
8 import chardet
9 except ImportError:
10 chardet = None
611
712 from pygments import highlight
813 from pygments.lexers import get_lexer_for_filename, guess_lexer, ClassNotFound
139144
140145 def force_unicode(s):
141146 """ Does all kind of magic to turn `s` into unicode """
147 # It's already unicode, don't do anything:
142148 if isinstance(s, unicode):
143149 return s
150
151 # Try some default encodings:
144152 try:
145153 return s.decode('utf-8')
146154 except UnicodeDecodeError as exc:
147155 pass
148156 try:
149 return s.decode('iso-8859-1')
157 return s.decode(locale.getpreferredencoding())
150158 except UnicodeDecodeError:
151159 pass
152 try:
153 import chardet
160
161 if chardet is not None:
162 # Try chardet, if available
154163 encoding = chardet.detect(s)['encoding']
155164 if encoding is not None:
156165 return s.decode(encoding)
157 except (ImportError, UnicodeDecodeError):
158 raise exc
166
167 raise exc # Give up.
159168
160169
161170 def extract_author_name(email):