diff --git a/conf/default.conf.php b/conf/default.conf.php index 00adb39311..ee80b79c30 100644 --- a/conf/default.conf.php +++ b/conf/default.conf.php @@ -486,4 +486,23 @@ return array( 'pygments.dropdown-default' => 'infer', + // This is an override list of regular expressions which allows you to choose + // what language files are highlighted as. If your projects have certain rules + // about filenames or use unusual or ambiguous language extensions, you can + // create a mapping here. This is an ordered dictionary of regular expressions + // which will be tested against the filename. They should map to either an + // explicit language as a string value, or a numeric index into the captured + // groups as an integer. + 'syntax.filemap' => array( + // Example: Treat all '*.xyz' files as PHP. + // '@\\.xyz$@' => 'php', + + // Example: Treat 'httpd.conf' as 'apacheconf'. + // '@/httpd\\.conf$@' => 'apacheconf', + + // Example: Treat all '*.x.bak' file as '.x'. NOTE: we map to capturing + // group 1 by specifying the mapping as "1". + // '@\\.([^.]+)\\.bak$@' => 1, + ), + ); diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index f38452557b..f2019e64c1 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -511,6 +511,7 @@ phutil_register_library_map(array( 'PhabricatorSetup' => 'infrastructure/setup', 'PhabricatorStandardPageView' => 'view/page/standard', 'PhabricatorStatusController' => 'applications/status/base', + 'PhabricatorSyntaxHighlighter' => 'applications/markup/syntax', 'PhabricatorTaskmasterDaemon' => 'infrastructure/daemon/workers/taskmaster', 'PhabricatorTestCase' => 'infrastructure/testing/testcase', 'PhabricatorTimelineCursor' => 'infrastructure/daemon/timeline/storage/cursor', diff --git a/src/applications/differential/parser/changeset/DifferentialChangesetParser.php b/src/applications/differential/parser/changeset/DifferentialChangesetParser.php index edc6153d08..c43aaf9ec7 100644 --- a/src/applications/differential/parser/changeset/DifferentialChangesetParser.php +++ b/src/applications/differential/parser/changeset/DifferentialChangesetParser.php @@ -873,10 +873,7 @@ EOSYNTHETIC; $this->isTopLevel = (($range_start === null) && ($range_len === null)); - $this->highlightEngine = new PhutilDefaultSyntaxHighlighterEngine(); - $this->highlightEngine->setConfig( - 'pygments.enabled', - PhabricatorEnv::getEnvConfig('pygments.enabled')); + $this->highlightEngine = PhabricatorSyntaxHighlighter::newEngine(); $this->tryCacheStuff(); diff --git a/src/applications/differential/parser/changeset/__init__.php b/src/applications/differential/parser/changeset/__init__.php index 34285ac975..7ca3982ed4 100644 --- a/src/applications/differential/parser/changeset/__init__.php +++ b/src/applications/differential/parser/changeset/__init__.php @@ -14,7 +14,7 @@ phutil_require_module('phabricator', 'applications/differential/storage/changese phutil_require_module('phabricator', 'applications/differential/storage/diff'); phutil_require_module('phabricator', 'applications/differential/view/inlinecomment'); phutil_require_module('phabricator', 'applications/files/uri'); -phutil_require_module('phabricator', 'infrastructure/env'); +phutil_require_module('phabricator', 'applications/markup/syntax'); phutil_require_module('phabricator', 'infrastructure/javelin/markup'); phutil_require_module('phabricator', 'storage/queryfx'); @@ -24,7 +24,6 @@ phutil_require_module('phutil', 'filesystem/tempfile'); phutil_require_module('phutil', 'future'); phutil_require_module('phutil', 'future/exec'); phutil_require_module('phutil', 'markup'); -phutil_require_module('phutil', 'markup/syntax/engine/default'); phutil_require_module('phutil', 'utils'); diff --git a/src/applications/diffusion/controller/file/DiffusionBrowseFileController.php b/src/applications/diffusion/controller/file/DiffusionBrowseFileController.php index 3bf7e7ee3b..eac0ce27ff 100644 --- a/src/applications/diffusion/controller/file/DiffusionBrowseFileController.php +++ b/src/applications/diffusion/controller/file/DiffusionBrowseFileController.php @@ -179,16 +179,11 @@ class DiffusionBrowseFileController extends DiffusionController { list($text_list, $rev_list, $blame_dict) = $file_query->getBlameData(); - $highlight_engine = new PhutilDefaultSyntaxHighlighterEngine(); - $highlight_engine->setConfig( - 'pygments.enabled', - PhabricatorEnv::getEnvConfig('pygments.enabled')); - - $text_list = explode( - "\n", - $highlight_engine->highlightSource( - $highlight_engine->getLanguageFromFilename($path), - implode("\n", $text_list))); + $text_list = implode("\n", $text_list); + $text_list = PhabricatorSyntaxHighlighter::highlightWithFilename( + $path, + $text_list); + $text_list = explode("\n", $text_list); $rows = $this->buildDisplayRows($text_list, $rev_list, $blame_dict, $needs_blame, $drequest, $file_query, $selected); diff --git a/src/applications/diffusion/controller/file/__init__.php b/src/applications/diffusion/controller/file/__init__.php index 8b61c73968..7bba4cebcd 100644 --- a/src/applications/diffusion/controller/file/__init__.php +++ b/src/applications/diffusion/controller/file/__init__.php @@ -8,13 +8,12 @@ phutil_require_module('phabricator', 'applications/diffusion/controller/base'); phutil_require_module('phabricator', 'applications/diffusion/query/filecontent/base'); +phutil_require_module('phabricator', 'applications/markup/syntax'); phutil_require_module('phabricator', 'infrastructure/celerity/api'); -phutil_require_module('phabricator', 'infrastructure/env'); phutil_require_module('phabricator', 'infrastructure/javelin/api'); phutil_require_module('phabricator', 'view/layout/panel'); phutil_require_module('phutil', 'markup'); -phutil_require_module('phutil', 'markup/syntax/engine/default'); phutil_require_module('phutil', 'utils'); diff --git a/src/applications/markup/syntax/PhabricatorSyntaxHighlighter.php b/src/applications/markup/syntax/PhabricatorSyntaxHighlighter.php new file mode 100644 index 0000000000..52597ec333 --- /dev/null +++ b/src/applications/markup/syntax/PhabricatorSyntaxHighlighter.php @@ -0,0 +1,51 @@ + PhabricatorEnv::getEnvConfig('pygments.enabled'), + 'filename.map' => PhabricatorEnv::getEnvConfig('syntax.filemap'), + ); + + foreach ($config as $key => $value) { + $engine->setConfig($key, $value); + } + + return $engine; + } + + public static function highlightWithFilename($filename, $source) { + $engine = self::newEngine(); + $language = $engine->getLanguageFromFilename($filename); + return $engine->getHighlightFuture($language, $source)->resolve(); + } + + public static function highlightWithLanguage($language, $source) { + $engine = self::newEngine(); + return $engine->getHighlightFuture($language, $source)->resolve(); + } + + +} diff --git a/src/applications/markup/syntax/__init__.php b/src/applications/markup/syntax/__init__.php new file mode 100644 index 0000000000..230b375ab6 --- /dev/null +++ b/src/applications/markup/syntax/__init__.php @@ -0,0 +1,14 @@ +setConfig( - 'pygments.enabled', - PhabricatorEnv::getEnvConfig('pygments.enabled')); - - $language = $paste->getLanguage(); + $source = $file->loadFileData(); if (empty($language)) { - $language = $highlight_engine->getLanguageFromFilename( - $paste->getTitle()); + $source = PhabricatorSyntaxHighlighter::highlightWithFilename( + $paste->getTitle(), + $source); + } else { + $source = PhabricatorSyntaxHighlighter::highlightWithLanguage( + $language, + $source); } - $text_list = explode( - "\n", - $highlight_engine->highlightSource( - $language, - $file->loadFileData())); + $text_list = explode("\n", $source); $rows = $this->buildDisplayRows($text_list); diff --git a/src/applications/paste/controller/view/__init__.php b/src/applications/paste/controller/view/__init__.php index fc6f4b8546..527fc99e51 100644 --- a/src/applications/paste/controller/view/__init__.php +++ b/src/applications/paste/controller/view/__init__.php @@ -10,14 +10,13 @@ phutil_require_module('phabricator', 'aphront/response/400'); phutil_require_module('phabricator', 'aphront/response/404'); phutil_require_module('phabricator', 'applications/files/storage/file'); phutil_require_module('phabricator', 'applications/files/uri'); +phutil_require_module('phabricator', 'applications/markup/syntax'); phutil_require_module('phabricator', 'applications/paste/controller/base'); phutil_require_module('phabricator', 'applications/paste/storage/paste'); phutil_require_module('phabricator', 'infrastructure/celerity/api'); -phutil_require_module('phabricator', 'infrastructure/env'); phutil_require_module('phabricator', 'view/layout/panel'); phutil_require_module('phutil', 'markup'); -phutil_require_module('phutil', 'markup/syntax/engine/default'); phutil_require_module('phutil', 'utils');