Page MenuHomePhabricator

D21713.diff
No OneTemporary

D21713.diff

diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -5770,6 +5770,7 @@
'PhutilRemarkupEngine' => 'infrastructure/markup/remarkup/PhutilRemarkupEngine.php',
'PhutilRemarkupEngineTestCase' => 'infrastructure/markup/remarkup/__tests__/PhutilRemarkupEngineTestCase.php',
'PhutilRemarkupEscapeRemarkupRule' => 'infrastructure/markup/markuprule/PhutilRemarkupEscapeRemarkupRule.php',
+ 'PhutilRemarkupEvalRule' => 'infrastructure/markup/markuprule/PhutilRemarkupEvalRule.php',
'PhutilRemarkupHeaderBlockRule' => 'infrastructure/markup/blockrule/PhutilRemarkupHeaderBlockRule.php',
'PhutilRemarkupHighlightRule' => 'infrastructure/markup/markuprule/PhutilRemarkupHighlightRule.php',
'PhutilRemarkupHorizontalRuleBlockRule' => 'infrastructure/markup/blockrule/PhutilRemarkupHorizontalRuleBlockRule.php',
@@ -12770,6 +12771,7 @@
'PhutilRemarkupEngine' => 'PhutilMarkupEngine',
'PhutilRemarkupEngineTestCase' => 'PhutilTestCase',
'PhutilRemarkupEscapeRemarkupRule' => 'PhutilRemarkupRule',
+ 'PhutilRemarkupEvalRule' => 'PhutilRemarkupRule',
'PhutilRemarkupHeaderBlockRule' => 'PhutilRemarkupBlockRule',
'PhutilRemarkupHighlightRule' => 'PhutilRemarkupRule',
'PhutilRemarkupHorizontalRuleBlockRule' => 'PhutilRemarkupBlockRule',
diff --git a/src/infrastructure/markup/PhabricatorMarkupEngine.php b/src/infrastructure/markup/PhabricatorMarkupEngine.php
--- a/src/infrastructure/markup/PhabricatorMarkupEngine.php
+++ b/src/infrastructure/markup/PhabricatorMarkupEngine.php
@@ -42,7 +42,7 @@
private $objects = array();
private $viewer;
private $contextObject;
- private $version = 20;
+ private $version = 21;
private $engineCaches = array();
private $auxiliaryConfig = array();
@@ -504,6 +504,7 @@
$rules = array();
$rules[] = new PhutilRemarkupEscapeRemarkupRule();
+ $rules[] = new PhutilRemarkupEvalRule();
$rules[] = new PhutilRemarkupMonospaceRule();
diff --git a/src/infrastructure/markup/markuprule/PhutilRemarkupEvalRule.php b/src/infrastructure/markup/markuprule/PhutilRemarkupEvalRule.php
new file mode 100644
--- /dev/null
+++ b/src/infrastructure/markup/markuprule/PhutilRemarkupEvalRule.php
@@ -0,0 +1,100 @@
+<?php
+
+final class PhutilRemarkupEvalRule extends PhutilRemarkupRule {
+
+ const KEY_EVAL = 'eval';
+
+ public function getPriority() {
+ return 50;
+ }
+
+ public function apply($text) {
+ return preg_replace_callback(
+ '/\${{{(.+?)}}}/',
+ array($this, 'newExpressionToken'),
+ $text);
+ }
+
+ public function newExpressionToken(array $matches) {
+ $expression = $matches[1];
+
+ if (!$this->isFlatText($expression)) {
+ return $matches[0];
+ }
+
+ $engine = $this->getEngine();
+ $token = $engine->storeText($expression);
+
+ $list_key = self::KEY_EVAL;
+ $expression_list = $engine->getTextMetadata($list_key, array());
+
+ $expression_list[] = array(
+ 'token' => $token,
+ 'expression' => $expression,
+ 'original' => $matches[0],
+ );
+
+ $engine->setTextMetadata($list_key, $expression_list);
+
+ return $token;
+ }
+
+ public function didMarkupText() {
+ $engine = $this->getEngine();
+
+ $list_key = self::KEY_EVAL;
+ $expression_list = $engine->getTextMetadata($list_key, array());
+
+ foreach ($expression_list as $expression_item) {
+ $token = $expression_item['token'];
+ $expression = $expression_item['expression'];
+
+ $result = $this->evaluateExpression($expression);
+
+ if ($result === null) {
+ $result = $expression_item['original'];
+ }
+
+ $engine->overwriteStoredText($token, $result);
+ }
+ }
+
+ private function evaluateExpression($expression) {
+ static $string_map;
+
+ if ($string_map === null) {
+ $string_map = array(
+ 'strings' => array(
+ 'platform' => array(
+ 'server' => array(
+ 'name' => pht('Phabricator'),
+ 'path' => pht('phabricator/'),
+ ),
+ 'client' => array(
+ 'name' => pht('Arcanist'),
+ 'path' => pht('arcanist/'),
+ ),
+ ),
+ ),
+ );
+ }
+
+ $parts = explode('.', $expression);
+
+ $cursor = $string_map;
+ foreach ($parts as $part) {
+ if (isset($cursor[$part])) {
+ $cursor = $cursor[$part];
+ } else {
+ break;
+ }
+ }
+
+ if (is_string($cursor)) {
+ return $cursor;
+ }
+
+ return null;
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Mon, Mar 31, 5:35 AM (3 w, 2 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7707249
Default Alt Text
D21713.diff (4 KB)

Event Timeline