diff --git a/src/markup/syntax/highlighter/PhutilPygmentsSyntaxHighlighter.php b/src/markup/syntax/highlighter/PhutilPygmentsSyntaxHighlighter.php --- a/src/markup/syntax/highlighter/PhutilPygmentsSyntaxHighlighter.php +++ b/src/markup/syntax/highlighter/PhutilPygmentsSyntaxHighlighter.php @@ -2,6 +2,9 @@ final class PhutilPygmentsSyntaxHighlighter extends Phobject { + const MAX_LINE_LENGTH = 10240; + const MAX_SOURCE_LENGTH = 1048576; + private $config = array(); public function setConfig($key, $value) { @@ -17,6 +20,12 @@ // use it on files with "\r" newlines. If we have "\r" not followed by // "\n" in the file, skip highlighting. $language = null; + } else if ((strlen($source) > self::MAX_SOURCE_LENGTH) || + ($this->getLongestLineLength($source) > self::MAX_LINE_LENGTH)) { + // `pygmentize` is really slow and expensive when dealing with long + // source but especially with long lines. Better skip highlighting than + // 500 error. + $language = null; } if ($language) { @@ -40,6 +49,17 @@ ->getHighlightFuture($source); } + private function getLongestLineLength($source) { + $longest = 0; + $offset = 0; + while (($pos = strpos($source, "\n", $offset)) !== false) { + $longest = max($longest, $pos - $offset); + $offset = $pos + 1; + } + $longest = max($longest, strlen($source) - $offset); + return $longest; + } + private function getPygmentsLexerNameFromLanguageName($language) { static $map = array( 'adb' => 'ada',