Skip to content

Commit bedabc0

Browse files
committed
ENH: Support __docformat__ also via command line
E.g. --config='docformat="numpy"' Fixes #169
1 parent 4c6cfcb commit bedabc0

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

pdoc/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ def _render_template(template_name, **kwargs):
114114
config.update((var, getattr(config_module, var, None))
115115
for var in config_module.__dict__
116116
if var not in MAKO_INTERNALS)
117-
known_keys = set(config) | {'module', 'modules', 'http_server', 'external_links'} # deprecated
117+
known_keys = (set(config)
118+
| {'docformat'} # Feature. https://github.com/pdoc3/pdoc/issues/169
119+
| {'module', 'modules', 'http_server', 'external_links'}) # deprecated
118120
invalid_keys = {k: v for k, v in kwargs.items() if k not in known_keys}
119121
if invalid_keys:
120122
warn('Unknown configuration variables (not in config.mako): {}'.format(invalid_keys))

pdoc/html_helpers.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,13 @@ def handleMatch(self, m, data):
385385
return wrapper, m.start(0), m.end(0)
386386

387387

388-
def to_html(text: str, docformat: str = 'numpy,google', *,
388+
def to_html(text: str, *,
389+
docformat: str = None,
389390
module: pdoc.Module = None, link: Callable[..., str] = None,
390391
latex_math: bool = False):
391392
"""
392-
Returns HTML of `text` interpreted as `docformat`.
393-
By default, Numpydoc and Google-style docstrings are assumed,
393+
Returns HTML of `text` interpreted as `docformat`. `__docformat__` is respected
394+
if present, otherwise Numpydoc and Google-style docstrings are assumed,
394395
as well as pure Markdown.
395396
396397
`module` should be the documented module (so the references can be
@@ -414,13 +415,16 @@ def to_markdown(text: str, *,
414415
module: pdoc.Module = None, link: Callable[..., str] = None):
415416
"""
416417
Returns `text`, assumed to be a docstring in `docformat`, converted to markdown.
418+
`__docformat__` is respected
419+
if present, otherwise Numpydoc and Google-style docstrings are assumed,
420+
as well as pure Markdown.
417421
418422
`module` should be the documented module (so the references can be
419423
resolved) and `link` is the hyperlinking function like the one in the
420424
example template.
421425
"""
422-
if docformat is None:
423-
docformat = getattr(getattr(module, 'obj', None), '__docformat__', 'numpy,google ')
426+
if not docformat:
427+
docformat = str(getattr(getattr(module, 'obj', None), '__docformat__', 'numpy,google '))
424428
docformat, *_ = docformat.lower().split()
425429
if not (set(docformat.split(',')) & {'', 'numpy', 'google'}):
426430
warn('__docformat__ value {!r} in module {!r} not supported. '

pdoc/templates/html.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
1717
def to_html(text):
18-
return _to_html(text, module=module, link=link, latex_math=latex_math)
18+
return _to_html(text, docformat=docformat, module=module, link=link, latex_math=latex_math)
1919
2020
2121
def get_annotation(bound_method, sep=':'):

pdoc/templates/pdf.mako

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<%!
1+
<%
22
import re
33
import pdoc
44
from pdoc.html_helpers import to_markdown
@@ -10,7 +10,7 @@
1010
return '[{0}](#{1} "{1}")'.format(name, dobj.refname)
1111
1212
def _to_md(text, module):
13-
text = to_markdown(text, module=module, link=link)
13+
text = to_markdown(text, docformat=docformat, module=module, link=link)
1414
# Setext H2 headings to atx H2 headings
1515
text = re.sub(r'\n(.+)\n-{3,}\n', r'\n## \1\n\n', text)
1616
# Convert admonitions into simpler paragraphs, dedent contents

pdoc/test/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,12 @@ def test_html_ref_links(self):
228228
],
229229
)
230230

231+
def test_docformat(self):
232+
with self.assertWarns(UserWarning) as cm,\
233+
run_html(EXAMPLE_MODULE, config='docformat="restructuredtext"'):
234+
self._basic_html_assertions()
235+
self.assertIn('numpy', cm.warning.args[0])
236+
231237
def test_html_no_source(self):
232238
with self.assertWarns(DeprecationWarning),\
233239
run_html(EXAMPLE_MODULE, html_no_source=None):

0 commit comments

Comments
 (0)