From b3de7ee3d24010c784f33ce9985d7a6790b78c9d Mon Sep 17 00:00:00 2001 From: takanori-pskq Date: Fri, 6 Nov 2020 15:45:31 +0000 Subject: [PATCH 1/2] ENH: Modify the markups for parameters (`x` to *x*) --- numpydoc/docscrape_sphinx.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/numpydoc/docscrape_sphinx.py b/numpydoc/docscrape_sphinx.py index ebc828fd..65281ee2 100644 --- a/numpydoc/docscrape_sphinx.py +++ b/numpydoc/docscrape_sphinx.py @@ -93,12 +93,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 +393,16 @@ 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) + + # Modify the markups for parameters (`x` to *x*) + for param_section in ['Parameters', 'Other Parameters', 'Attributes']: + for param in self[param_section]: + rendered = re.sub( + r'([^`:])`' + self._escape_args_and_kwargs(param.name) + r'`', + r'\1*' + param.name + r'*', + rendered, + ) + return '\n'.join(self._str_indent(rendered.split('\n'), indent)) From c7c4eff56a5d89f9267d4ef82fb2f36a80f89a68 Mon Sep 17 00:00:00 2001 From: takanori-pskq Date: Sun, 8 Nov 2020 02:30:59 +0000 Subject: [PATCH 2/2] ENH: Modify to use a new option ``numpydoc_role_param`` --- numpydoc/docscrape_sphinx.py | 21 +++++++++++++-------- numpydoc/numpydoc.py | 2 ++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/numpydoc/docscrape_sphinx.py b/numpydoc/docscrape_sphinx.py index 65281ee2..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')] @@ -394,14 +395,18 @@ def __str__(self, indent=0, func_role="obj"): rendered = self.template.render(**ns) - # Modify the markups for parameters (`x` to *x*) - for param_section in ['Parameters', 'Other Parameters', 'Attributes']: - for param in self[param_section]: - rendered = re.sub( - r'([^`:])`' + self._escape_args_and_kwargs(param.name) + r'`', - r'\1*' + param.name + r'*', - rendered, - ) + # 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)