From 4080d1432e5016d8f7c58fa0f0237200e20b4345 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Tue, 12 Jul 2022 09:43:50 +0200 Subject: [PATCH] Fix code highlight --- app/main.py | 1 + app/utils/highlight.py | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 6dc4c2b..90ef66f 100644 --- a/app/main.py +++ b/app/main.py @@ -66,6 +66,7 @@ _RESIZED_CACHE: MutableMapping[tuple[str, int], tuple[bytes, str, Any]] = LFUCac # TODO(ts): # # Next: +# - DB models for webmentions # - allow to undo follow requests # - indieauth tweaks # - API for posting notes diff --git a/app/utils/highlight.py b/app/utils/highlight.py index 1413e7d..4334cb3 100644 --- a/app/utils/highlight.py +++ b/app/utils/highlight.py @@ -16,9 +16,12 @@ def highlight(html: str) -> str: for code in soup.find_all("code"): if not code.parent.name == "pre": continue - lexer = guess_lexer(code.text) + code_content = ( + code.encode_contents().decode().replace("
", "\n").replace("
", "\n") + ) + lexer = guess_lexer(code_content) tag = BeautifulSoup( - phighlight(code.text, lexer, _FORMATTER), "html5lib" + phighlight(code_content, lexer, _FORMATTER), "html5lib" ).body.next pre = code.parent pre.replaceWith(tag)