com phd: Improve PHP 7 compatibility by handling highlight_st ring exceptions.: phpdotnet/phd/Highlighter.php
| From: | Adam Harvey | Date: | Wed, 20 May 2015 17:06:42 +0000 |
| Subject: | com phd: Improve PHP 7 compatibility by handling highlight_st ring exceptions.: phpdotnet/phd/Highlighter.php | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-13319@lists.php.net to get a copy of this message | ||
Commit: 2c6c649ee48f4fb7f91c6b67bd5ad326d80a099e
Author: Adam Harvey <aharvey@php.net> Wed, 20 May 2015 12:06:42 -0500
Parents: 99c9440e5b6459f12e8f46a609845da526c9fc61
Branches: master
Link: http://git.php.net/?p=phd.git;a=commitdiff;h=2c6c649ee48f4fb7f91c6b67bd5ad326d80a099e
Log:
Improve PHP 7 compatibility by handling highlight_string exceptions.
Code that's valid in PHP 5 but invalid in PHP 7 will cause ParseException
objects to be thrown by highlight_string(). We currently have one example of
this in php-doc-en, but will presumably have more as more PHP 7 documentation
is written.
This change will ensure that we handle this as gracefully as we can: instead of
erroring out, we'll at least include the code without syntax highlighting.
Changed paths:
M phpdotnet/phd/Highlighter.php
Diff:
diff --git a/phpdotnet/phd/Highlighter.php b/phpdotnet/phd/Highlighter.php
index ac909d4..b791b68 100644
--- a/phpdotnet/phd/Highlighter.php
+++ b/phpdotnet/phd/Highlighter.php
@@ -62,7 +62,15 @@ class Highlighter
}
if ($role == 'php') {
- return highlight_string($text, 1);
+ try {
+ return highlight_string($text, 1);
+ } catch (\ParseException $e) {
+ v("Parse error while highlighting PHP code: %s\nText: %s", (string) $e,
$text, E_USER_WARNING);
+
+ return '<pre class="'. ($role ? $role . 'code' :
'programlisting') .'">'
+ . htmlspecialchars($text, ENT_QUOTES, 'UTF-8')
+ . "</pre>\n";
+ }
} else {
return '<pre class="'. ($role ? $role . 'code' :
'programlisting') .'">'
. htmlspecialchars($text, ENT_QUOTES, 'UTF-8')