Browse Source

Fixed PHP syntax highlighting outside <?php ?>

Previously, PHP syntax highlighting worked only inside <?php ... ?> 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 <?php ... ?> block.
merge-requests/57/head
Petr Stastny 7 years ago
parent
commit
9d2e8bc1e1
  1. 10
      tildes/tildes/lib/markdown.py

10
tildes/tildes/lib/markdown.py

@ -13,7 +13,7 @@ from html5lib.filters.base import Filter
from html5lib.treewalkers.base import NonRecursiveTreeWalker from html5lib.treewalkers.base import NonRecursiveTreeWalker
from pygments import highlight from pygments import highlight
from pygments.formatters import HtmlFormatter 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 pygments.util import ClassNotFound
from tildes.metrics import histogram_timer from tildes.metrics import histogram_timer
@ -206,7 +206,13 @@ def apply_syntax_highlighting(html: str) -> str:
language = code_block["class"][0].replace("language-", "") language = code_block["class"][0].replace("language-", "")
try: 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 language in ["php", "php3", "php4", "php5"]:
lexer = PhpLexer(startinline=True)
else:
lexer = get_lexer_by_name(language)
except ClassNotFound: except ClassNotFound:
continue continue

Loading…
Cancel
Save