5
5
import inspect
6
6
import textwrap
7
7
import pydoc
8
- import sphinx
9
8
import collections
9
+ import os
10
+
11
+ from jinja2 import FileSystemLoader
12
+ from jinja2 .sandbox import SandboxedEnvironment
13
+ import sphinx
14
+ from sphinx .jinja2glue import BuiltinTemplateLoader
10
15
11
16
from .docscrape import NumpyDocString , FunctionDoc , ClassDoc
12
17
@@ -24,6 +29,12 @@ def __init__(self, docstring, config={}):
24
29
def load_config (self , config ):
25
30
self .use_plots = config .get ('use_plots' , False )
26
31
self .class_members_toctree = config .get ('class_members_toctree' , True )
32
+ self .template = config .get ('template' , None )
33
+ if self .template is None :
34
+ template_dirs = [os .path .join (os .path .dirname (__file__ ), 'templates' )]
35
+ template_loader = FileSystemLoader (template_dirs )
36
+ template_env = SandboxedEnvironment (loader = template_loader )
37
+ self .template = template_env .get_template ('numpydoc_docstring.rst' )
27
38
28
39
# string conversion routines
29
40
def _str_header (self , name , symbol = '`' ):
@@ -223,25 +234,29 @@ def _str_examples(self):
223
234
return self ._str_section ('Examples' )
224
235
225
236
def __str__ (self , indent = 0 , func_role = "obj" ):
226
- out = []
227
- out += self ._str_signature ()
228
- out += self ._str_index () + ['' ]
229
- out += self ._str_summary ()
230
- out += self ._str_extended_summary ()
231
- out += self ._str_param_list ('Parameters' )
232
- out += self ._str_returns ('Returns' )
233
- out += self ._str_returns ('Yields' )
234
- for param_list in ('Other Parameters' , 'Raises' , 'Warns' ):
235
- out += self ._str_param_list (param_list )
236
- out += self ._str_warnings ()
237
- out += self ._str_see_also (func_role )
238
- out += self ._str_section ('Notes' )
239
- out += self ._str_references ()
240
- out += self ._str_examples ()
241
- for param_list in ('Attributes' , 'Methods' ):
242
- out += self ._str_member_list (param_list )
243
- out = self ._str_indent (out , indent )
244
- return '\n ' .join (out )
237
+ ns = {
238
+ 'signature' : self ._str_signature (),
239
+ 'index' : self ._str_index (),
240
+ 'summary' : self ._str_summary (),
241
+ 'extended_summary' : self ._str_extended_summary (),
242
+ 'parameters' : self ._str_param_list ('Parameters' ),
243
+ 'returns' : self ._str_returns ('Returns' ),
244
+ 'yields' : self ._str_returns ('Yields' ),
245
+ 'other_parameters' : self ._str_param_list ('Other Parameters' ),
246
+ 'raises' : self ._str_param_list ('Raises' ),
247
+ 'warns' : self ._str_param_list ('Warns' ),
248
+ 'warnings' : self ._str_warnings (),
249
+ 'see_also' : self ._str_see_also (func_role ),
250
+ 'notes' : self ._str_section ('Notes' ),
251
+ 'references' : self ._str_references (),
252
+ 'examples' : self ._str_examples (),
253
+ 'attributes' : self ._str_member_list ('Attributes' ),
254
+ 'methods' : self ._str_member_list ('Methods' ),
255
+ }
256
+ ns = dict ((k , '\n ' .join (v )) for k , v in ns .items ())
257
+
258
+ rendered = self .template .render (** ns )
259
+ return '\n ' .join (self ._str_indent (rendered .split ('\n ' ), indent ))
245
260
246
261
247
262
class SphinxFunctionDoc (SphinxDocString , FunctionDoc ):
@@ -263,7 +278,7 @@ def __init__(self, obj, doc=None, config={}):
263
278
SphinxDocString .__init__ (self , doc , config = config )
264
279
265
280
266
- def get_doc_object (obj , what = None , doc = None , config = {}):
281
+ def get_doc_object (obj , what = None , doc = None , config = {}, builder = None ):
267
282
if what is None :
268
283
if inspect .isclass (obj ):
269
284
what = 'class'
@@ -273,6 +288,16 @@ def get_doc_object(obj, what=None, doc=None, config={}):
273
288
what = 'function'
274
289
else :
275
290
what = 'object'
291
+
292
+ template_dirs = [os .path .join (os .path .dirname (__file__ ), 'templates' )]
293
+ if builder is not None :
294
+ template_loader = BuiltinTemplateLoader ()
295
+ template_loader .init (builder , dirs = template_dirs )
296
+ else :
297
+ template_loader = FileSystemLoader (template_dirs )
298
+ template_env = SandboxedEnvironment (loader = template_loader )
299
+ config ['template' ] = template_env .get_template ('numpydoc_docstring.rst' )
300
+
276
301
if what == 'class' :
277
302
return SphinxClassDoc (obj , func_doc = SphinxFunctionDoc , doc = doc ,
278
303
config = config )
0 commit comments