Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Modify the markups for parameters (x to *x*) #303

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions numpydoc/docscrape_sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))


Expand Down
2 changes: 2 additions & 0 deletions numpydoc/numpydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down Expand Up @@ -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)
Expand Down