2013-10-17 04:12:14 +02:00
|
|
|
<?php
|
|
|
|
|
2014-08-04 16:55:43 +02:00
|
|
|
final class PhabricatorRemarkupCowsayBlockInterpreter
|
2013-10-17 04:12:14 +02:00
|
|
|
extends PhutilRemarkupBlockInterpreter {
|
|
|
|
|
|
|
|
public function getInterpreterName() {
|
|
|
|
return 'cowsay';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function markupContent($content, array $argv) {
|
2015-09-13 21:27:30 +02:00
|
|
|
$action = idx($argv, 'think') ? 'think' : 'say';
|
2013-10-17 04:12:14 +02:00
|
|
|
$eyes = idx($argv, 'eyes', 'oo');
|
|
|
|
$tongue = idx($argv, 'tongue', ' ');
|
2015-09-13 21:27:30 +02:00
|
|
|
|
|
|
|
$map = self::getCowMap();
|
|
|
|
|
|
|
|
$cow = idx($argv, 'cow');
|
|
|
|
$cow = phutil_utf8_strtolower($cow);
|
|
|
|
if (empty($map[$cow])) {
|
|
|
|
$cow = 'default';
|
2013-10-17 04:12:14 +02:00
|
|
|
}
|
|
|
|
|
2015-09-13 21:27:30 +02:00
|
|
|
$result = id(new PhutilCowsay())
|
|
|
|
->setTemplate($map[$cow])
|
|
|
|
->setAction($action)
|
|
|
|
->setEyes($eyes)
|
|
|
|
->setTongue($tongue)
|
|
|
|
->setText($content)
|
|
|
|
->renderCow();
|
2013-10-17 04:12:14 +02:00
|
|
|
|
2015-10-09 05:03:15 +02:00
|
|
|
$engine = $this->getEngine();
|
|
|
|
|
|
|
|
if ($engine->isTextMode()) {
|
2015-09-13 21:27:30 +02:00
|
|
|
return $result;
|
2013-10-17 04:12:14 +02:00
|
|
|
}
|
|
|
|
|
2015-10-09 05:03:15 +02:00
|
|
|
if ($engine->isHTMLMailMode()) {
|
|
|
|
return phutil_tag('pre', array(), $result);
|
|
|
|
}
|
|
|
|
|
2013-10-17 04:12:14 +02:00
|
|
|
return phutil_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => 'PhabricatorMonospaced remarkup-cowsay',
|
|
|
|
),
|
2015-09-13 21:27:30 +02:00
|
|
|
$result);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static function getCowMap() {
|
|
|
|
$root = dirname(phutil_get_library_root('phabricator'));
|
|
|
|
|
|
|
|
$directories = array(
|
|
|
|
$root.'/externals/cowsay/cows/',
|
|
|
|
$root.'/resources/cows/builtin/',
|
|
|
|
$root.'/resources/cows/custom/',
|
|
|
|
);
|
|
|
|
|
|
|
|
$map = array();
|
|
|
|
foreach ($directories as $directory) {
|
|
|
|
foreach (Filesystem::listDirectory($directory, false) as $cow_file) {
|
|
|
|
$matches = null;
|
|
|
|
if (!preg_match('/^(.*)\.cow\z/', $cow_file, $matches)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$cow_name = $matches[1];
|
|
|
|
$cow_name = phutil_utf8_strtolower($cow_name);
|
|
|
|
$map[$cow_name] = Filesystem::readFile($directory.$cow_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $map;
|
2013-10-17 04:12:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|