diff --git a/python/mdhtml/export.py b/python/mdhtml/export.py index d8619ab..a9cb7b1 100644 --- a/python/mdhtml/export.py +++ b/python/mdhtml/export.py @@ -8,8 +8,8 @@ from ._html import parse_mdhtml -try: from fastpylight import highlight, highlight_spans, languages -except ImportError: highlight = highlight_spans = languages = None +try: from fastpylight import highlight, highlight_spans +except ImportError: highlight = highlight_spans = None __all__ = ["SCHEMES", "REFTYPES", "ref_tokens", "ref_variant", "decode_raw", "group_plan", "HeadingNums", "Resolver", "to_html", "math_js"] @@ -331,16 +331,18 @@ def _hl(self, pre): if self.hl_lang and (new := self.hl_lang(text, lang)) != lang: lang = new if lang: code.attrs["class"] = f"language-{lang}" - if self.hl and highlight_spans and lang is not None and lang in languages(): - if self.hl == "spans": - frag = parse_mdhtml(highlight_spans(text, lang)) - _set_children(code, list(frag.children[0].children[0].children)) - else: - frag = parse_mdhtml(highlight(text, lang)) - hlc = _mk("hl-code", {"toks": frag.children[0].attrs.get("toks")}) - pre.parent.replace_child(hlc, pre) - hlc.append_child(pre) - pre = hlc + if self.hl and highlight_spans and lang is not None: + try: + if self.hl == "spans": + frag = parse_mdhtml(highlight_spans(text, lang)) + _set_children(code, list(frag.children[0].children[0].children)) + else: + frag = parse_mdhtml(highlight(text, lang)) + hlc = _mk("hl-code", {"toks": frag.children[0].attrs.get("toks")}) + pre.parent.replace_child(hlc, pre) + hlc.append_child(pre) + pre = hlc + except ValueError: pass if self.code_wrap and (repl := self.code_wrap(pre.to_html(pretty=False), lang, text)) is not None: pre.parent.replace_child(parse_mdhtml(repl), pre) diff --git a/tests/test_export.py b/tests/test_export.py index 52d67c8..a07af77 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -130,6 +130,11 @@ def wrap(html, lang, text): assert '
' in h
     assert '
graph TD
' in h and 'language-mermaid' not in h +def test_hl_lang_alias(): + h = to_html(to_mdhtml('```py\n1+1\n```\n\n```nosuchlang\nx\n```\n')) + assert '1' in h # alias resolved by the highlighter + assert 'x\n' in h # unknown language left unhighlighted + def test_refs_ids(): src = to_mdhtml('# A {#sec-a}\n\nSee [@sec-a], [Clause @sec-b], [-@sec-a], and [@fig-e; @sec-nope].\n\n![E](e.png){#fig-e}\n', implicit_figures=True)