From 9454060c297069552eff1fa84a44319e469d927d Mon Sep 17 00:00:00 2001 From: Ricky Elrod Date: Sun, 3 Jul 2011 18:29:58 -0400 Subject: [PATCH] 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 --- conf/default.conf.php | 45 +++++++++++++++---- resources/sql/patches/052.pastelanguage.sql | 2 + .../data/commitmessage/__init__.php | 1 - .../PhabricatorPasteCreateController.php | 32 +++++++++++++ .../paste/controller/create/__init__.php | 2 + .../view/PhabricatorPasteViewController.php | 2 +- .../paste/storage/paste/PhabricatorPaste.php | 1 + 7 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 resources/sql/patches/052.pastelanguage.sql diff --git a/conf/default.conf.php b/conf/default.conf.php index fcd09e4898..da5d7a0c2b 100644 --- a/conf/default.conf.php +++ b/conf/default.conf.php @@ -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', + ); diff --git a/resources/sql/patches/052.pastelanguage.sql b/resources/sql/patches/052.pastelanguage.sql new file mode 100644 index 0000000000..47f09e12c2 --- /dev/null +++ b/resources/sql/patches/052.pastelanguage.sql @@ -0,0 +1,2 @@ +ALTER TABLE phabricator_pastebin.pastebin_paste + ADD COLUMN language VARCHAR(64) NOT NULL; \ No newline at end of file diff --git a/src/applications/differential/data/commitmessage/__init__.php b/src/applications/differential/data/commitmessage/__init__.php index 4234c8c8aa..c6ce1f81a8 100644 --- a/src/applications/differential/data/commitmessage/__init__.php +++ b/src/applications/differential/data/commitmessage/__init__.php @@ -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'); diff --git a/src/applications/paste/controller/create/PhabricatorPasteCreateController.php b/src/applications/paste/controller/create/PhabricatorPasteCreateController.php index 3f2f241d79..e56b400e05 100644 --- a/src/applications/paste/controller/create/PhabricatorPasteCreateController.php +++ b/src/applications/paste/controller/create/PhabricatorPasteCreateController.php @@ -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') diff --git a/src/applications/paste/controller/create/__init__.php b/src/applications/paste/controller/create/__init__.php index f6151ac905..d0215f70f2 100644 --- a/src/applications/paste/controller/create/__init__.php +++ b/src/applications/paste/controller/create/__init__.php @@ -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'); diff --git a/src/applications/paste/controller/view/PhabricatorPasteViewController.php b/src/applications/paste/controller/view/PhabricatorPasteViewController.php index 850fb7842d..776eacea5f 100644 --- a/src/applications/paste/controller/view/PhabricatorPasteViewController.php +++ b/src/applications/paste/controller/view/PhabricatorPasteViewController.php @@ -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); diff --git a/src/applications/paste/storage/paste/PhabricatorPaste.php b/src/applications/paste/storage/paste/PhabricatorPaste.php index 37c1564f83..4edeea81ea 100644 --- a/src/applications/paste/storage/paste/PhabricatorPaste.php +++ b/src/applications/paste/storage/paste/PhabricatorPaste.php @@ -22,6 +22,7 @@ class PhabricatorPaste extends PhabricatorPasteDAO { protected $title; protected $authorPHID; protected $filePHID; + protected $language; public function getConfiguration() { return array(