Browse Source

Fix PHP syntax highlighting outside <?php ?>

Previously, PHP syntax highlighting worked only inside <?php ... ?>
block.

Pygments can disable this behavior, but we have to pass an argument when
creating the PhpLexer to do it.
merge-requests/60/head
Petr Stastny 6 years ago
committed by Deimos
parent
commit
b45ce35446
  1. 7
      tildes/tildes/lib/markdown.py

7
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 <?php ... ?> will get highlighted.
if isinstance(lexer, PhpLexer):
lexer = PhpLexer(startinline=True)
except ClassNotFound:
continue

Loading…
Cancel
Save