Skip to content

Commit 078568e

Browse files
committed
Fix highlight override without 'superfences'
and drop unreachable 'codehilite' handling
1 parent 5253cee commit 078568e

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

mkdocstrings/handlers/crystal/renderer.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def render(self, data: DocItem, config: dict) -> str:
3030
subconfig = collections.ChainMap(config, self.default_config)
3131
template = self.env.get_template(data._TEMPLATE)
3232

33-
with self._monkeypatch_highlight_functions(default_lang="crystal"):
33+
with self._monkeypatch_highlight_function(default_lang="crystal"):
3434
return template.render(
3535
config=subconfig,
3636
obj=data,
@@ -44,8 +44,10 @@ def update_env(self, md: Markdown, config: dict) -> None:
4444

4545
self._pymdownx_hl = None
4646
for ext in md.registeredExtensions:
47-
if hasattr(ext, "get_pymdownx_highlighter"):
48-
self._pymdownx_hl = ext
47+
try:
48+
self._pymdownx_hl = ext.get_pymdownx_highlighter()
49+
except AttributeError:
50+
pass
4951

5052
# Disallow raw HTML.
5153
del md.preprocessors["html_block"]
@@ -92,24 +94,16 @@ def do_convert_markdown(self, text: str, context: DocItem, heading_level: int, h
9294
self._md, text, heading_level=heading_level, html_id=html_id
9395
)
9496

95-
def _monkeypatch_highlight_functions(self, default_lang: str):
96-
"""Changes 'codehilite' and 'pymdownx.highlight' extensions to use this lang by default."""
97+
def _monkeypatch_highlight_function(self, default_lang: str):
98+
"""Changes 'pymdownx.highlight' extension to use this lang by default."""
9799
# Yes, there really isn't a better way. I'd be glad to be proven wrong.
98100
if self._pymdownx_hl:
99-
old = self._pymdownx_hl.get_pymdownx_highlighter()
100-
members = {
101-
"highlight": lambda self, src="", language="", *args, **kwargs: old.highlight(
102-
self, src, language or default_lang, *args, **kwargs
103-
)
104-
}
105-
subclass = type("Highlighter", (old,), members)
106-
return _monkeypatch(self._pymdownx_hl, "get_pymdownx_highlighter", lambda: subclass)
107-
else:
108-
old = fenced_code.CodeHilite
109-
new = lambda src, *args, lang=None, **kwargs: old(
110-
src, *args, lang=lang or default_lang, **kwargs
101+
old = self._pymdownx_hl.highlight
102+
new = lambda self, src="", language="", *args, **kwargs: old(
103+
self, src, language or default_lang, *args, **kwargs
111104
)
112-
return _monkeypatch(fenced_code, "CodeHilite", new)
105+
return _monkeypatch(self._pymdownx_hl, "highlight", new)
106+
return contextlib.nullcontext()
113107

114108

115109
@contextlib.contextmanager

0 commit comments

Comments
 (0)