1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-22 05:20:56 +01:00

Remarkup rule for rendering PHIDs as handles

Summary:
adds the `{{PHID....}}` rule. Should mostly be useful in UI code that refers to Objects.

It doesn't add any mention links/transactions.

Test Plan: Comment with this, see email (plain + html) and comment box.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D15488
This commit is contained in:
Aviv Eyal 2016-03-17 20:24:03 +00:00 committed by avivey
parent 8f94aa8a06
commit 2b9d4f70ba
3 changed files with 112 additions and 0 deletions

View file

@ -2454,6 +2454,7 @@ phutil_register_library_map(array(
'PhabricatorHandlePool' => 'applications/phid/handle/pool/PhabricatorHandlePool.php',
'PhabricatorHandlePoolTestCase' => 'applications/phid/handle/pool/__tests__/PhabricatorHandlePoolTestCase.php',
'PhabricatorHandleQuery' => 'applications/phid/query/PhabricatorHandleQuery.php',
'PhabricatorHandleRemarkupRule' => 'applications/phid/remarkup/PhabricatorHandleRemarkupRule.php',
'PhabricatorHandlesEditField' => 'applications/transactions/editfield/PhabricatorHandlesEditField.php',
'PhabricatorHarbormasterApplication' => 'applications/harbormaster/application/PhabricatorHarbormasterApplication.php',
'PhabricatorHarbormasterConfigOptions' => 'applications/harbormaster/config/PhabricatorHarbormasterConfigOptions.php',
@ -6897,6 +6898,7 @@ phutil_register_library_map(array(
'PhabricatorHandlePool' => 'Phobject',
'PhabricatorHandlePoolTestCase' => 'PhabricatorTestCase',
'PhabricatorHandleQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'PhabricatorHandleRemarkupRule' => 'PhutilRemarkupRule',
'PhabricatorHandlesEditField' => 'PhabricatorPHIDListEditField',
'PhabricatorHarbormasterApplication' => 'PhabricatorApplication',
'PhabricatorHarbormasterConfigOptions' => 'PhabricatorApplicationConfigOptions',

View file

@ -0,0 +1,109 @@
<?php
final class PhabricatorHandleRemarkupRule extends PhutilRemarkupRule {
const KEY_RULE_HANDLE = 'rule.handle';
const KEY_RULE_HANDLE_ORIGINAL = 'rule.handle.original';
public function apply($text) {
return preg_replace_callback(
'/{(PHID-[a-zA-Z0-9-]*)}/',
array($this, 'markupHandle'),
$text);
}
public function markupHandle(array $matches) {
$engine = $this->getEngine();
$viewer = $engine->getConfig('viewer');
if (!$this->isFlatText($matches[0])) {
return $matches[0];
}
$phid_type = phid_get_type($matches[1]);
if ($phid_type == PhabricatorPHIDConstants::PHID_TYPE_UNKNOWN) {
return $matches[0];
}
$token = $engine->storeText($matches[0]);
if ($engine->isTextMode()) {
return $token;
}
$original_key = self::KEY_RULE_HANDLE_ORIGINAL;
$original = $engine->getTextMetadata($original_key, array());
$original[$token] = $matches[0];
$engine->setTextMetadata($original_key, $original);
$metadata_key = self::KEY_RULE_HANDLE;
$metadata = $engine->getTextMetadata($metadata_key, array());
$phid = $matches[1];
if (empty($metadata[$phid])) {
$metadata[$phid] = array();
}
$metadata[$phid][] = $token;
$engine->setTextMetadata($metadata_key, $metadata);
return $token;
}
public function didMarkupText() {
$engine = $this->getEngine();
$metadata_key = self::KEY_RULE_HANDLE;
$metadata = $engine->getTextMetadata($metadata_key, array());
if (empty($metadata)) {
// No mentions, or we already processed them.
return;
}
$original_key = self::KEY_RULE_HANDLE_ORIGINAL;
$original = $engine->getTextMetadata($original_key, array());
$phids = array_keys($metadata);
$handles = id(new PhabricatorHandleQuery())
->setViewer($this->getEngine()->getConfig('viewer'))
->withPHIDs($phids)
->execute();
foreach ($metadata as $phid => $tokens) {
$handle = idx($handles, $phid);
if ($handle->isComplete()) {
if ($engine->isHTMLMailMode()) {
$href = $handle->getURI();
$href = PhabricatorEnv::getProductionURI($href);
$link = phutil_tag(
'a',
array(
'href' => $href,
'style' => '
border-color: #f1f7ff;
color: #19558d;
background-color: #f1f7ff;
border: 1px solid transparent;
border-radius: 3px;
font-weight: bold;
padding: 0 4px;',
),
$handle->getLinkName());
} else {
$link = $handle->renderTag();
$link->setPHID($phid);
}
foreach ($tokens as $token) {
$engine->overwriteStoredText($token, $link);
}
} else {
foreach ($tokens as $token) {
$engine->overwriteStoredText($token, idx($original, $token));
}
}
}
$engine->setTextMetadata($metadata_key, array());
}
}

View file

@ -494,6 +494,7 @@ final class PhabricatorMarkupEngine extends Phobject {
$rules[] = new PhabricatorIconRemarkupRule();
$rules[] = new PhabricatorEmojiRemarkupRule();
$rules[] = new PhabricatorHandleRemarkupRule();
$applications = PhabricatorApplication::getAllInstalledApplications();
foreach ($applications as $application) {