Fix #43: --site-name should accept non-ASCII values
Jonas Haag
9 years ago
3 | 3 | import time |
4 | 4 | import datetime |
5 | 5 | import mimetypes |
6 | import locale | |
7 | try: | |
8 | import chardet | |
9 | except ImportError: | |
10 | chardet = None | |
6 | 11 | |
7 | 12 | from pygments import highlight |
8 | 13 | from pygments.lexers import get_lexer_for_filename, guess_lexer, ClassNotFound |
139 | 144 | |
140 | 145 | def force_unicode(s): |
141 | 146 | """ Does all kind of magic to turn `s` into unicode """ |
147 | # It's already unicode, don't do anything: | |
142 | 148 | if isinstance(s, unicode): |
143 | 149 | return s |
150 | ||
151 | # Try some default encodings: | |
144 | 152 | try: |
145 | 153 | return s.decode('utf-8') |
146 | 154 | except UnicodeDecodeError as exc: |
147 | 155 | pass |
148 | 156 | try: |
149 | return s.decode('iso-8859-1') | |
157 | return s.decode(locale.getpreferredencoding()) | |
150 | 158 | except UnicodeDecodeError: |
151 | 159 | pass |
152 | try: | |
153 | import chardet | |
160 | ||
161 | if chardet is not None: | |
162 | # Try chardet, if available | |
154 | 163 | encoding = chardet.detect(s)['encoding'] |
155 | 164 | if encoding is not None: |
156 | 165 | return s.decode(encoding) |
157 | except (ImportError, UnicodeDecodeError): | |
158 | raise exc | |
166 | ||
167 | raise exc # Give up. | |
159 | 168 | |
160 | 169 | |
161 | 170 | def extract_author_name(email): |