1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-19 16:58:48 +02: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:
Ricky Elrod 2011-07-03 18:29:58 -04:00
parent fc28ab06c2
commit 9454060c29
7 changed files with 75 additions and 10 deletions

View file

@ -305,14 +305,6 @@ return array(
// behalf, silencing the warning.
'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 ----------------------------------------------------------------- //
// Lists which uploaded file types may be viewed in the browser. If a file
@ -416,4 +408,41 @@ return array(
// settings are the defaults.)
'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',
);

View file

@ -0,0 +1,2 @@
ALTER TABLE phabricator_pastebin.pastebin_paste
ADD COLUMN language VARCHAR(64) NOT NULL;

View file

@ -11,7 +11,6 @@ phutil_require_module('phabricator', 'applications/differential/storage/comment'
phutil_require_module('phabricator', 'applications/phid/handle/data');
phutil_require_module('phabricator', 'infrastructure/env');
phutil_require_module('phutil', 'symbols');
phutil_require_module('phutil', 'utils');

View file

@ -31,6 +31,16 @@ class PhabricatorPasteCreateController extends PhabricatorPasteController {
if ($request->isFormPost()) {
$errors = array();
$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');
if (!strlen($text)) {
@ -41,6 +51,7 @@ class PhabricatorPasteCreateController extends PhabricatorPasteController {
}
$paste->setTitle($title);
$paste->setLanguage($language);
if (!$errors) {
$paste_file = PhabricatorFile::newFromFileData(
@ -76,6 +87,26 @@ class PhabricatorPasteCreateController extends PhabricatorPasteController {
}
$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
->setUser($user)
->setAction($request->getRequestURI()->getPath())
@ -84,6 +115,7 @@ class PhabricatorPasteCreateController extends PhabricatorPasteController {
->setLabel('Title')
->setValue($paste->getTitle())
->setName('title'))
->appendChild($language_select)
->appendChild(
id(new AphrontFormTextAreaControl())
->setLabel('Text')

View file

@ -10,7 +10,9 @@ phutil_require_module('phabricator', 'aphront/response/redirect');
phutil_require_module('phabricator', 'applications/files/storage/file');
phutil_require_module('phabricator', 'applications/paste/controller/base');
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/control/select');
phutil_require_module('phabricator', 'view/form/control/submit');
phutil_require_module('phabricator', 'view/form/control/text');
phutil_require_module('phabricator', 'view/form/control/textarea');

View file

@ -96,7 +96,7 @@ class PhabricatorPasteViewController extends PhabricatorPasteController {
$text_list = explode(
"\n", $highlightEngine->highlightSource(
$paste->getTitle(),
nonempty($paste->getLanguage(), $paste->getTitle()),
$file->loadFileData()));
$rows = $this->buildDisplayRows($text_list);

View file

@ -22,6 +22,7 @@ class PhabricatorPaste extends PhabricatorPasteDAO {
protected $title;
protected $authorPHID;
protected $filePHID;
protected $language;
public function getConfiguration() {
return array(