From b45ce354465ad819a458b3f8f99f6fbfb22348f1 Mon Sep 17 00:00:00 2001 From: Petr Stastny Date: Tue, 19 Feb 2019 22:09:58 +0100 Subject: [PATCH] Fix PHP syntax highlighting outside Previously, PHP syntax highlighting worked only inside block. Pygments can disable this behavior, but we have to pass an argument when creating the PhpLexer to do it. --- tildes/tildes/lib/markdown.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tildes/tildes/lib/markdown.py b/tildes/tildes/lib/markdown.py index 6d130f8..f600351 100644 --- a/tildes/tildes/lib/markdown.py +++ b/tildes/tildes/lib/markdown.py @@ -13,7 +13,7 @@ from html5lib.filters.base import Filter from html5lib.treewalkers.base import NonRecursiveTreeWalker from pygments import highlight from pygments.formatters import HtmlFormatter -from pygments.lexers import get_lexer_by_name +from pygments.lexers import get_lexer_by_name, PhpLexer from pygments.util import ClassNotFound from tildes.metrics import histogram_timer @@ -207,6 +207,11 @@ def apply_syntax_highlighting(html: str) -> str: try: lexer = get_lexer_by_name(language) + # If target language is PHP, override default lexer construction + # and set startinline to True, so even code that is not enclosed + # inside will get highlighted. + if isinstance(lexer, PhpLexer): + lexer = PhpLexer(startinline=True) except ClassNotFound: continue