Skip to content

Commit

Permalink
ENH: Support configurable markdown extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Mar 11, 2024
1 parent 641e7a8 commit c2dbcb9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
12 changes: 11 additions & 1 deletion pdoc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,16 @@ def docfilter(obj, _filters=args.filter.strip().split(',')):
for module in args.modules]
pdoc.link_inheritance()

# Loading is done. Output stage ...
config = pdoc._get_config(**template_config)

# Load global markdown extensions
if config.get('md_extensions'):
from .html_helpers import _md
_kwargs = dict(extensions=[], configs={})
_kwargs.update(config.get('md_extensions', {}))
_md.registerExtensions(**_kwargs)

if args.pdf:
_print_pdf(modules, **template_config)
import textwrap
Expand Down Expand Up @@ -583,7 +593,7 @@ def docfilter(obj, _filters=args.filter.strip().split(',')):
sys.stdout.write(os.linesep * (1 + 2 * int(module != modules[-1])))

if args.html:
lunr_config = pdoc._get_config(**template_config).get('lunr_search')
lunr_config = config.get('lunr_search')
if lunr_config is not None:
_generate_lunr_search(
modules, lunr_config.get("index_docstrings", True), template_config)
Expand Down
2 changes: 1 addition & 1 deletion pdoc/html_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ def glimpse(text: str, max_length=153, *, paragraph=True,
output_format='html5', # type: ignore[arg-type]
extensions=[
"markdown.extensions.abbr",
"markdown.extensions.admonition",
"markdown.extensions.attr_list",
"markdown.extensions.def_list",
"markdown.extensions.fenced_code",
"markdown.extensions.footnotes",
"markdown.extensions.tables",
"markdown.extensions.admonition",
"markdown.extensions.smarty",
"markdown.extensions.toc",
],
Expand Down
6 changes: 6 additions & 0 deletions pdoc/templates/config.mako
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@
# Note: in Python docstrings, either all backslashes need to be escaped (\\)
# or you need to use raw r-strings.
latex_math = False
# Additional markdown extensions to enable. See:
# https://python-markdown.github.io/extensions/
# https://python-markdown.github.io/reference/#extensions
# https://github.com/Python-Markdown/markdown/wiki/Third-Party-Extensions
md_extensions = {'extensions': [], 'configs': {}}
%>
11 changes: 11 additions & 0 deletions pdoc/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from glob import glob
from io import StringIO
from itertools import chain
from pathlib import Path
from random import randint
from tempfile import TemporaryDirectory
from time import sleep
Expand Down Expand Up @@ -468,6 +469,16 @@ def test_resolve_typing_forwardrefs(self):
out = out.getvalue()
self.assertIn('Set[Bar]', out)

def test_md_extensions(self):
with temp_dir() as path, \
chdir(path):
Path('foo.py').write_text('"""secret: meta data\n\nOnly this comment expected."""')
with redirect_streams() as (stdout, _), \
run_html('foo.py', html_no_source=None,
config="md_extensions={'extensions': ['markdown.extensions.meta']}",):
self._check_files(include_patterns=['comment expected'],
exclude_patterns=['<p>secret: meta data</p>'])


class ApiTest(unittest.TestCase):
"""
Expand Down

0 comments on commit c2dbcb9

Please sign in to comment.