|
9 | 9 |
|
10 | 10 | # Only for windows compatibility - Forces default encoding to UTF8, which it may not be on windows
|
11 | 11 | if os.name == 'nt':
|
12 |
| - reload(sys) |
13 |
| - sys.setdefaultencoding('UTF8') |
| 12 | + # monkeypatches sphinxcontrib.datatemplates so it uses utf-8 as the encoding |
| 13 | + # instead of cp1252 when opening a file. using sys.setdefaultencoding does |
| 14 | + # not alter the encoding when opening a file for reading. |
| 15 | + |
| 16 | + from sphinxcontrib.datatemplates import directive |
| 17 | + from xml.etree import ElementTree as ET |
| 18 | + import mimetypes |
| 19 | + |
| 20 | + old_load_data = directive.DataTemplateLegacy._load_data |
| 21 | + |
| 22 | + def _load_data(self, env, data_source, encoding): |
| 23 | + rel_filename, filename = env.relfn2path(data_source) |
| 24 | + if data_source.endswith('.yaml'): |
| 25 | + return self._load_yaml(filename, 'utf-8') |
| 26 | + elif data_source.endswith('.json'): |
| 27 | + return self._load_json(filename, 'utf-8') |
| 28 | + elif data_source.endswith('.csv'): |
| 29 | + return self._load_csv(filename, 'utf-8') |
| 30 | + elif "xml" in mimetypes.guess_type(data_source)[0]: |
| 31 | + # there are many XML based formats |
| 32 | + return ET.parse(filename).getroot() |
| 33 | + else: |
| 34 | + raise NotImplementedError('cannot load file type of %s' % |
| 35 | + data_source) |
| 36 | + |
| 37 | + |
| 38 | + directive.DataTemplateLegacy._load_data = _load_data |
| 39 | + setattr( |
| 40 | + sys.modules['sphinxcontrib.datatemplates.directive'].DataTemplateLegacy, |
| 41 | + '_load_data', |
| 42 | + _load_data |
| 43 | + ) |
14 | 44 |
|
15 | 45 |
|
16 | 46 | sys.path.append(os.getcwd()) # noqa
|
|
0 commit comments