2
2
3
3
This extension allows you to automagically generate API documentation from your project.
4
4
"""
5
+ import collections
5
6
import io
6
7
import os
7
8
import shutil
38
39
"special-members" ,
39
40
"imported-members" ,
40
41
]
42
+ _TEMPLATE_SUFFIXES = {
43
+ "markdown" : ".md" ,
44
+ "restructuredtext" : ".rst" ,
45
+ }
46
+ """Map an output format to the file extension of the templates to use."""
41
47
_VIEWCODE_CACHE : Dict [str , Tuple [str , Dict ]] = {}
42
48
"""Caches a module's parse results for use in viewcode."""
43
49
@@ -64,6 +70,35 @@ def _normalise_autoapi_dirs(autoapi_dirs, srcdir):
64
70
return normalised_dirs
65
71
66
72
73
+ def _normalise_source_suffix (source_suffix ):
74
+ result = collections .OrderedDict ()
75
+
76
+ if isinstance (source_suffix , str ):
77
+ result [source_suffix ] = "restructuredtext"
78
+ elif isinstance (source_suffix , list ):
79
+ for suffix in source_suffix :
80
+ result [suffix ] = "restructuredtext"
81
+ else :
82
+ result = source_suffix
83
+
84
+ return result
85
+
86
+
87
+ def _normalise_output_suffix (output_suffix , source_suffix ):
88
+ result = output_suffix
89
+
90
+ if not result :
91
+ if ".rst" in source_suffix :
92
+ result = ".rst"
93
+ elif ".txt" in source_suffix :
94
+ result = ".txt"
95
+ else :
96
+ # Fallback to first suffix listed
97
+ result = next (iter (source_suffix ))
98
+
99
+ return result
100
+
101
+
67
102
def run_autoapi (app ): # pylint: disable=too-many-branches
68
103
"""Load AutoAPI data from the filesystem."""
69
104
if app .config .autoapi_type not in LANGUAGE_MAPPERS :
@@ -142,20 +177,25 @@ def run_autoapi(app): # pylint: disable=too-many-branches
142
177
else :
143
178
ignore_patterns = DEFAULT_IGNORE_PATTERNS .get (app .config .autoapi_type , [])
144
179
145
- if ".rst" in app .config .source_suffix :
146
- out_suffix = ".rst"
147
- elif ".txt" in app .config .source_suffix :
148
- out_suffix = ".txt"
180
+ source_suffix = _normalise_source_suffix (app .config .source_suffix )
181
+ out_suffix = _normalise_output_suffix (app .config .autoapi_output_suffix , source_suffix )
182
+ output_format = source_suffix .get (out_suffix )
183
+ if output_format :
184
+ if app .config .autoapi_type == "python" :
185
+ if output_format not in _TEMPLATE_SUFFIXES :
186
+ raise ExtensionError (f"Unknown output format '{ output_format } '" )
187
+ elif output_format != "restructuredtext" :
188
+ raise ExtensionError (f"Unknown output format '{ output_format } '" )
149
189
else :
150
- # Fallback to first suffix listed
151
- out_suffix = next (iter (app .config .source_suffix ))
190
+ raise ExtensionError (f"autoapi_output_suffix '{ out_suffix } ' must be registered with source_suffix" )
152
191
153
192
if sphinx_mapper_obj .load (
154
193
patterns = file_patterns , dirs = normalised_dirs , ignore = ignore_patterns
155
194
):
156
195
sphinx_mapper_obj .map (options = app .config .autoapi_options )
157
196
158
197
if app .config .autoapi_generate_api_docs :
198
+ app .env .autoapi_template_suffix = _TEMPLATE_SUFFIXES [output_format ]
159
199
sphinx_mapper_obj .output_rst (root = normalized_root , source_suffix = out_suffix )
160
200
161
201
@@ -298,6 +338,7 @@ def setup(app):
298
338
app .add_config_value ("autoapi_python_class_content" , "class" , "html" )
299
339
app .add_config_value ("autoapi_generate_api_docs" , True , "html" )
300
340
app .add_config_value ("autoapi_prepare_jinja_env" , None , "html" )
341
+ app .add_config_value ("autoapi_output_suffix" , None , "html" )
301
342
app .add_autodocumenter (documenters .AutoapiFunctionDocumenter )
302
343
app .add_autodocumenter (documenters .AutoapiPropertyDocumenter )
303
344
app .add_autodocumenter (documenters .AutoapiDecoratorDocumenter )
0 commit comments