55import inspect
66import textwrap
77import pydoc
8- import sphinx
98import 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
1015
1116from .docscrape import NumpyDocString , FunctionDoc , ClassDoc
1217
@@ -24,6 +29,12 @@ def __init__(self, docstring, config={}):
2429 def load_config (self , config ):
2530 self .use_plots = config .get ('use_plots' , False )
2631 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' )
2738
2839 # string conversion routines
2940 def _str_header (self , name , symbol = '`' ):
@@ -223,25 +234,29 @@ def _str_examples(self):
223234 return self ._str_section ('Examples' )
224235
225236 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 ))
245260
246261
247262class SphinxFunctionDoc (SphinxDocString , FunctionDoc ):
@@ -263,7 +278,7 @@ def __init__(self, obj, doc=None, config={}):
263278 SphinxDocString .__init__ (self , doc , config = config )
264279
265280
266- def get_doc_object (obj , what = None , doc = None , config = {}):
281+ def get_doc_object (obj , what = None , doc = None , config = {}, builder = None ):
267282 if what is None :
268283 if inspect .isclass (obj ):
269284 what = 'class'
@@ -273,6 +288,16 @@ def get_doc_object(obj, what=None, doc=None, config={}):
273288 what = 'function'
274289 else :
275290 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+
276301 if what == 'class' :
277302 return SphinxClassDoc (obj , func_doc = SphinxFunctionDoc , doc = doc ,
278303 config = config )
0 commit comments