Skip to content

Commit 224c228

Browse files
committedNov 26, 2024
ENH: Support configurable markdown extensions (CLI-only)
1 parent 115a168 commit 224c228

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed
 

‎pdoc/cli.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,18 @@ def docfilter(obj, _filters=args.filter.strip().split(',')):
557557
for module in args.modules]
558558
pdoc.link_inheritance()
559559

560+
# Loading is done. Output stage ...
561+
config = pdoc._get_config(**template_config)
562+
563+
# Load configured global markdown extensions
564+
# XXX: This is hereby enabled only for CLI usage as for
565+
# API use I couldn't figure out where reliably to put it.
566+
if config.get('md_extensions'):
567+
from .html_helpers import _md
568+
_kwargs = {'extensions': [], 'configs': {}}
569+
_kwargs.update(config.get('md_extensions', {}))
570+
_md.registerExtensions(**_kwargs)
571+
560572
if args.pdf:
561573
_print_pdf(modules, **template_config)
562574
import textwrap
@@ -603,7 +615,7 @@ def docfilter(obj, _filters=args.filter.strip().split(',')):
603615
sys.stdout.write(os.linesep * (1 + 2 * int(module != modules[-1])))
604616

605617
if args.html:
606-
lunr_config = pdoc._get_config(**template_config).get('lunr_search')
618+
lunr_config = config.get('lunr_search')
607619
if lunr_config is not None:
608620
_generate_lunr_search(
609621
modules, lunr_config.get("index_docstrings", True), template_config)

‎pdoc/html_helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ def glimpse(text: str, max_length=153, *, paragraph=True,
7373
output_format='html5', # type: ignore[arg-type]
7474
extensions=[
7575
"markdown.extensions.abbr",
76+
"markdown.extensions.admonition",
7677
"markdown.extensions.attr_list",
7778
"markdown.extensions.def_list",
7879
"markdown.extensions.fenced_code",
7980
"markdown.extensions.footnotes",
8081
"markdown.extensions.tables",
81-
"markdown.extensions.admonition",
8282
"markdown.extensions.smarty",
8383
"markdown.extensions.toc",
8484
],

‎pdoc/templates/config.mako

+6
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,10 @@
6161
# Note: in Python docstrings, either all backslashes need to be escaped (\\)
6262
# or you need to use raw r-strings.
6363
latex_math = False
64+
65+
# Additional markdown extensions to enable. See:
66+
# https://python-markdown.github.io/extensions/
67+
# https://python-markdown.github.io/reference/#extensions
68+
# https://github.com/Python-Markdown/markdown/wiki/Third-Party-Extensions
69+
md_extensions = {'extensions': [], 'configs': {}}
6470
%>

‎pdoc/test/__init__.py

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from glob import glob
1919
from io import StringIO
2020
from itertools import chain
21+
from pathlib import Path
2122
from random import randint
2223
from tempfile import TemporaryDirectory
2324
from time import sleep
@@ -477,6 +478,15 @@ def test_resolve_typing_forwardrefs(self):
477478
out = out.getvalue()
478479
self.assertIn('Set[Bar]', out)
479480

481+
def test_md_extensions(self):
482+
with temp_dir() as path, chdir(path):
483+
Path('foo.py').write_text('"""secret: meta data\n\nOnly this comment expected."""')
484+
with redirect_streams() as (stdout, _), \
485+
run_html('foo.py',
486+
config="md_extensions={'extensions': ['markdown.extensions.meta']}",):
487+
self._check_files(include_patterns=['<p>Only this comment expected.</p>'],
488+
exclude_patterns=['<p>secret: meta data</p>'])
489+
480490

481491
class ApiTest(unittest.TestCase):
482492
"""

0 commit comments

Comments
 (0)