mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-26 08:42:41 +01:00
Add a syntax highlight dropdown, if pygments is enabled.
Summary: - Add a default list of supported languages to default.conf.php and make the initial/default value customizable. - Store a '' in the database to infer the language from the filename/title. Test Plan: Tested in my sandbox with pygments enabled and disabled and various combinations of filename/extension/dropdown selection. Reviewers: epriestley CC: Differential Revision: 587
This commit is contained in:
parent
fc28ab06c2
commit
9454060c29
7 changed files with 75 additions and 10 deletions
|
@ -305,14 +305,6 @@ return array(
|
||||||
// behalf, silencing the warning.
|
// behalf, silencing the warning.
|
||||||
'phabricator.timezone' => null,
|
'phabricator.timezone' => null,
|
||||||
|
|
||||||
|
|
||||||
// Phabricator can highlight PHP by default, but if you want syntax
|
|
||||||
// highlighting for other languages you should install the python package
|
|
||||||
// 'Pygments', make sure the 'pygmentize' script is available in the
|
|
||||||
// $PATH of the webserver, and then enable this.
|
|
||||||
'pygments.enabled' => false,
|
|
||||||
|
|
||||||
|
|
||||||
// -- Files ----------------------------------------------------------------- //
|
// -- Files ----------------------------------------------------------------- //
|
||||||
|
|
||||||
// Lists which uploaded file types may be viewed in the browser. If a file
|
// Lists which uploaded file types may be viewed in the browser. If a file
|
||||||
|
@ -416,4 +408,41 @@ return array(
|
||||||
// settings are the defaults.)
|
// settings are the defaults.)
|
||||||
'celerity.force-disk-reads' => false,
|
'celerity.force-disk-reads' => false,
|
||||||
|
|
||||||
|
// -- Pygments ------------------------------------------------------------ //
|
||||||
|
// Phabricator can highlight PHP by default, but if you want syntax
|
||||||
|
// highlighting for other languages you should install the python package
|
||||||
|
// 'Pygments', make sure the 'pygmentize' script is available in the
|
||||||
|
// $PATH of the webserver, and then enable this.
|
||||||
|
'pygments.enabled' => false,
|
||||||
|
|
||||||
|
// In places that we display a dropdown to syntax-highlight code,
|
||||||
|
// this is where that list is defined.
|
||||||
|
// Syntax is 'lexer-name' => 'Display Name',
|
||||||
|
'pygments.dropdown-choices' => array(
|
||||||
|
'apacheconf' => 'Apache Configuration',
|
||||||
|
'bash' => 'Bash Scripting',
|
||||||
|
'brainfuck' => 'Brainf*ck',
|
||||||
|
'c' => 'C',
|
||||||
|
'cpp' => 'C++',
|
||||||
|
'css' => 'CSS',
|
||||||
|
'diff' => 'Diff',
|
||||||
|
'django' => 'Django Templating',
|
||||||
|
'erb' => 'Embedded Ruby/ERB',
|
||||||
|
'erlang' => 'Erlang',
|
||||||
|
'html' => 'HTML',
|
||||||
|
'infer' => 'Infer from title (extension)',
|
||||||
|
'java' => 'Java',
|
||||||
|
'js' => 'Javascript',
|
||||||
|
'mysql' => 'MySQL',
|
||||||
|
'perl' => 'Perl',
|
||||||
|
'php' => 'PHP',
|
||||||
|
'text' => 'Plain Text',
|
||||||
|
'python' => 'Python',
|
||||||
|
// TODO: 'remarkup' => 'Remarkup',
|
||||||
|
'ruby' => 'Ruby',
|
||||||
|
'xml' => 'XML',
|
||||||
|
),
|
||||||
|
|
||||||
|
'pygments.dropdown-default' => 'infer',
|
||||||
|
|
||||||
);
|
);
|
||||||
|
|
2
resources/sql/patches/052.pastelanguage.sql
Normal file
2
resources/sql/patches/052.pastelanguage.sql
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
ALTER TABLE phabricator_pastebin.pastebin_paste
|
||||||
|
ADD COLUMN language VARCHAR(64) NOT NULL;
|
|
@ -11,7 +11,6 @@ phutil_require_module('phabricator', 'applications/differential/storage/comment'
|
||||||
phutil_require_module('phabricator', 'applications/phid/handle/data');
|
phutil_require_module('phabricator', 'applications/phid/handle/data');
|
||||||
phutil_require_module('phabricator', 'infrastructure/env');
|
phutil_require_module('phabricator', 'infrastructure/env');
|
||||||
|
|
||||||
phutil_require_module('phutil', 'symbols');
|
|
||||||
phutil_require_module('phutil', 'utils');
|
phutil_require_module('phutil', 'utils');
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,16 @@ class PhabricatorPasteCreateController extends PhabricatorPasteController {
|
||||||
if ($request->isFormPost()) {
|
if ($request->isFormPost()) {
|
||||||
$errors = array();
|
$errors = array();
|
||||||
$title = $request->getStr('title');
|
$title = $request->getStr('title');
|
||||||
|
|
||||||
|
$language = $request->getStr('language');
|
||||||
|
if ($language == 'infer') {
|
||||||
|
// If it's infer, store an empty string. Otherwise, store the
|
||||||
|
// language name. We do this so we can refer to 'infer' elsewhere
|
||||||
|
// in the code (such as default value) while retaining backwards
|
||||||
|
// compatibility with old posts with no language stored.
|
||||||
|
$language = '';
|
||||||
|
}
|
||||||
|
|
||||||
$text = $request->getStr('text');
|
$text = $request->getStr('text');
|
||||||
|
|
||||||
if (!strlen($text)) {
|
if (!strlen($text)) {
|
||||||
|
@ -41,6 +51,7 @@ class PhabricatorPasteCreateController extends PhabricatorPasteController {
|
||||||
}
|
}
|
||||||
|
|
||||||
$paste->setTitle($title);
|
$paste->setTitle($title);
|
||||||
|
$paste->setLanguage($language);
|
||||||
|
|
||||||
if (!$errors) {
|
if (!$errors) {
|
||||||
$paste_file = PhabricatorFile::newFromFileData(
|
$paste_file = PhabricatorFile::newFromFileData(
|
||||||
|
@ -76,6 +87,26 @@ class PhabricatorPasteCreateController extends PhabricatorPasteController {
|
||||||
}
|
}
|
||||||
|
|
||||||
$form = new AphrontFormView();
|
$form = new AphrontFormView();
|
||||||
|
|
||||||
|
// If we're coming back from an error and the language was already defined,
|
||||||
|
// use that. Otherwise, ask the config for the default.
|
||||||
|
if ($paste->getLanguage()) {
|
||||||
|
$language_default = $paste->getLanguage();
|
||||||
|
} else {
|
||||||
|
$language_default = PhabricatorEnv::getEnvConfig(
|
||||||
|
'pygments.dropdown-default');
|
||||||
|
}
|
||||||
|
|
||||||
|
$available_languages = PhabricatorEnv::getEnvConfig(
|
||||||
|
'pygments.dropdown-choices');
|
||||||
|
asort($available_languages);
|
||||||
|
|
||||||
|
$language_select = id(new AphrontFormSelectControl())
|
||||||
|
->setLabel('Language')
|
||||||
|
->setName('language')
|
||||||
|
->setValue($language_default)
|
||||||
|
->setOptions($available_languages);
|
||||||
|
|
||||||
$form
|
$form
|
||||||
->setUser($user)
|
->setUser($user)
|
||||||
->setAction($request->getRequestURI()->getPath())
|
->setAction($request->getRequestURI()->getPath())
|
||||||
|
@ -84,6 +115,7 @@ class PhabricatorPasteCreateController extends PhabricatorPasteController {
|
||||||
->setLabel('Title')
|
->setLabel('Title')
|
||||||
->setValue($paste->getTitle())
|
->setValue($paste->getTitle())
|
||||||
->setName('title'))
|
->setName('title'))
|
||||||
|
->appendChild($language_select)
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new AphrontFormTextAreaControl())
|
id(new AphrontFormTextAreaControl())
|
||||||
->setLabel('Text')
|
->setLabel('Text')
|
||||||
|
|
|
@ -10,7 +10,9 @@ phutil_require_module('phabricator', 'aphront/response/redirect');
|
||||||
phutil_require_module('phabricator', 'applications/files/storage/file');
|
phutil_require_module('phabricator', 'applications/files/storage/file');
|
||||||
phutil_require_module('phabricator', 'applications/paste/controller/base');
|
phutil_require_module('phabricator', 'applications/paste/controller/base');
|
||||||
phutil_require_module('phabricator', 'applications/paste/storage/paste');
|
phutil_require_module('phabricator', 'applications/paste/storage/paste');
|
||||||
|
phutil_require_module('phabricator', 'infrastructure/env');
|
||||||
phutil_require_module('phabricator', 'view/form/base');
|
phutil_require_module('phabricator', 'view/form/base');
|
||||||
|
phutil_require_module('phabricator', 'view/form/control/select');
|
||||||
phutil_require_module('phabricator', 'view/form/control/submit');
|
phutil_require_module('phabricator', 'view/form/control/submit');
|
||||||
phutil_require_module('phabricator', 'view/form/control/text');
|
phutil_require_module('phabricator', 'view/form/control/text');
|
||||||
phutil_require_module('phabricator', 'view/form/control/textarea');
|
phutil_require_module('phabricator', 'view/form/control/textarea');
|
||||||
|
|
|
@ -96,7 +96,7 @@ class PhabricatorPasteViewController extends PhabricatorPasteController {
|
||||||
|
|
||||||
$text_list = explode(
|
$text_list = explode(
|
||||||
"\n", $highlightEngine->highlightSource(
|
"\n", $highlightEngine->highlightSource(
|
||||||
$paste->getTitle(),
|
nonempty($paste->getLanguage(), $paste->getTitle()),
|
||||||
$file->loadFileData()));
|
$file->loadFileData()));
|
||||||
|
|
||||||
$rows = $this->buildDisplayRows($text_list);
|
$rows = $this->buildDisplayRows($text_list);
|
||||||
|
|
|
@ -22,6 +22,7 @@ class PhabricatorPaste extends PhabricatorPasteDAO {
|
||||||
protected $title;
|
protected $title;
|
||||||
protected $authorPHID;
|
protected $authorPHID;
|
||||||
protected $filePHID;
|
protected $filePHID;
|
||||||
|
protected $language;
|
||||||
|
|
||||||
public function getConfiguration() {
|
public function getConfiguration() {
|
||||||
return array(
|
return array(
|
||||||
|
|
Loading…
Reference in a new issue