1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-04-07 09:58:33 +02:00
phorge-phorge/src/applications/celerity/management/CelerityManagementSyntaxWorkflow.php
epriestley 01289f3f48 Generate syntax highlighting CSS from a reusable map
Summary:
Ref T9790. This prepares the syntax color rules to be reused in mail.

This goes about halfway toward T5701 by sort-of supporting different styles but not really.

Test Plan:
  - Ran `bin/celerity syntax` to regenerate syntax map.
  - Viewed some highlighted code, didn't see any differences.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T9790

Differential Revision: https://secure.phabricator.com/D15846
2016-05-05 02:50:48 -07:00

67 lines
1.6 KiB
PHP

<?php
final class CelerityManagementSyntaxWorkflow
extends CelerityManagementWorkflow {
protected function didConstruct() {
$this
->setName('syntax')
->setExamples('**syntax** [options]')
->setSynopsis(pht('Rebuild syntax highlighting CSS.'))
->setArguments(
array());
}
public function execute(PhutilArgumentParser $args) {
$styles = PhabricatorSyntaxStyle::getAllStyles();
$root = dirname(phutil_get_library_root('phabricator'));
$root = $root.'/webroot/rsrc/css/syntax/';
foreach ($styles as $key => $style) {
$content = $this->generateCSS($style);
$path = $root.'/syntax-'.$key.'.css';
Filesystem::writeFile($path, $content);
echo tsprintf(
"%s\n",
pht(
'Rebuilt "%s" syntax CSS.',
basename($path)));
}
return 0;
}
private function generateCSS(PhabricatorSyntaxStyle $style) {
$key = $style->getSyntaxStyleKey();
$provides = "syntax-{$key}-css";
$generated = 'generated';
$header =
"/**\n".
" * @provides {$provides}\n".
" * @{$generated}\n".
" */\n\n";
$groups = array();
$map = $style->getStyleMap();
ksort($map);
foreach ($map as $key => $value) {
$groups[$value][] = $key;
}
$rules = array();
foreach ($groups as $body => $classes) {
$parts = array();
foreach ($classes as $class) {
$parts[] = ".remarkup-code .{$class}";
}
$rules[] = implode(",\n", $parts)." {\n {$body}\n}";
}
$rules = implode("\n\n", $rules);
return $header.$rules."\n";
}
}