From 9d2e8bc1e112e528999d7ce163b2078c39a0329e Mon Sep 17 00:00:00 2001 From: Petr Stastny Date: Tue, 19 Feb 2019 22:09:58 +0100 Subject: [PATCH] Fixed PHP syntax highlighting outside Previously, PHP syntax highlighting worked only inside block. This commit adds specific check, and if user wants to highlight PHP code, special lexer is used, which highlights PHP code even without the block. --- tildes/tildes/lib/markdown.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tildes/tildes/lib/markdown.py b/tildes/tildes/lib/markdown.py index 6d130f8..e1f860f 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 @@ -206,7 +206,13 @@ def apply_syntax_highlighting(html: str) -> str: language = code_block["class"][0].replace("language-", "") 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 language in ["php", "php3", "php4", "php5"]: + lexer = PhpLexer(startinline=True) + else: + lexer = get_lexer_by_name(language) except ClassNotFound: continue