diff --git a/numpydoc/docscrape_sphinx.py b/numpydoc/docscrape_sphinx.py index ebc828fd..21666bea 100644 --- a/numpydoc/docscrape_sphinx.py +++ b/numpydoc/docscrape_sphinx.py @@ -30,6 +30,7 @@ def load_config(self, config): self.xref_param_type = config.get('xref_param_type', False) self.xref_aliases = config.get('xref_aliases', dict()) self.xref_ignore = config.get('xref_ignore', set()) + self.role_param = config.get('role_param', None) self.template = config.get('template', None) if self.template is None: template_dirs = [os.path.join(os.path.dirname(__file__), 'templates')] @@ -93,12 +94,7 @@ def _str_returns(self, name='Returns'): return out def _escape_args_and_kwargs(self, name): - if name[:2] == '**': - return r'\*\*' + name[2:] - elif name[:1] == '*': - return r'\*' + name[1:] - else: - return name + return name.replace('*', '\*') def _process_param(self, param, desc, fake_autosummary): """Determine how to display a parameter @@ -398,6 +394,20 @@ def __str__(self, indent=0, func_role="obj"): ns = dict((k, '\n'.join(v)) for k, v in ns.items()) rendered = self.template.render(**ns) + + # Add roles for parameters + if self.role_param is not None: + param_sections = ['Parameters', 'Other Parameters'] + ( + ['Attributes'] if self.attributes_as_param_list else []) + + for param_section in param_sections: + for param in self[param_section]: + rendered = re.sub( + r'([^`:])`' + self._escape_args_and_kwargs(param.name) + r'`', + r'\1' + self.role_param + r'`' + param.name + r'`', + rendered, + ) + return '\n'.join(self._str_indent(rendered.split('\n'), indent)) diff --git a/numpydoc/numpydoc.py b/numpydoc/numpydoc.py index 93cd975d..6c0f4ef9 100644 --- a/numpydoc/numpydoc.py +++ b/numpydoc/numpydoc.py @@ -155,6 +155,7 @@ def mangle_docstrings(app, what, name, obj, options, lines): 'xref_param_type': app.config.numpydoc_xref_param_type, 'xref_aliases': app.config.numpydoc_xref_aliases_complete, 'xref_ignore': app.config.numpydoc_xref_ignore, + 'role_param': app.config.numpydoc_role_param, } cfg.update(options or {}) @@ -254,6 +255,7 @@ def setup(app, get_doc_object_=get_doc_object): app.add_config_value('numpydoc_xref_param_type', False, True) app.add_config_value('numpydoc_xref_aliases', dict(), True) app.add_config_value('numpydoc_xref_ignore', set(), True) + app.add_config_value('numpydoc_role_param', None, True) # Extra mangling domains app.add_domain(NumpyPythonDomain)