mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-10 00:42:41 +01:00
Clean up Phabricator interface to syntax highlighting
Summary: Reduce the amount of code duplication here and allow for an override configuration on the filename.map stuff. Test Plan: Checked paste, diffusion and differential syntax highlighting and everything appeared reasonable. Reviewed By: codeblock Reviewers: tuomaspelkonen, codeblock, jungejason, aran CC: aran, codeblock, epriestley Differential Revision: 601
This commit is contained in:
parent
70cd8b1b34
commit
85b34c23f9
10 changed files with 103 additions and 33 deletions
|
@ -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,
|
||||
),
|
||||
|
||||
);
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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');
|
||||
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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');
|
||||
|
||||
|
||||
|
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group markup
|
||||
*/
|
||||
final class PhabricatorSyntaxHighlighter {
|
||||
|
||||
public static function newEngine() {
|
||||
$engine = new PhutilDefaultSyntaxHighlighterEngine();
|
||||
|
||||
$config = array(
|
||||
'pygments.enabled' => 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
14
src/applications/markup/syntax/__init__.php
Normal file
14
src/applications/markup/syntax/__init__.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is automatically generated. Lint this module to rebuild it.
|
||||
* @generated
|
||||
*/
|
||||
|
||||
|
||||
|
||||
phutil_require_module('phabricator', 'infrastructure/env');
|
||||
|
||||
phutil_require_module('phutil', 'markup/syntax/engine/default');
|
||||
|
||||
|
||||
phutil_require_source('PhabricatorSyntaxHighlighter.php');
|
|
@ -89,23 +89,19 @@ class PhabricatorPasteViewController extends PhabricatorPasteController {
|
|||
require_celerity_resource('diffusion-source-css');
|
||||
require_celerity_resource('syntax-highlighting-css');
|
||||
|
||||
$highlight_engine = new PhutilDefaultSyntaxHighlighterEngine();
|
||||
$highlight_engine->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);
|
||||
|
||||
|
|
|
@ -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');
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue