Page MenuHomePhabricator

D14273.diff
No OneTemporary

D14273.diff

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',

File Metadata

Mime Type
text/plain
Expires
Tue, Jun 4, 5:58 PM (1 w, 6 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6284679
Default Alt Text
D14273.diff (1 KB)

Event Timeline