3 final class DiffusionSourceLinkRemarkupRule
4 extends PhutilRemarkupRule
{
6 const KEY_SOURCELINKS
= 'diffusion.links';
8 public function getPriority() {
12 public function apply($text) {
13 return preg_replace_callback(
14 '@{(?:src|source)\b((?:[^}\\\\]+|\\\\.)*)}@m',
15 array($this, 'markupSourceLink'),
19 public function markupSourceLink(array $matches) {
20 $engine = $this->getEngine();
21 $text_mode = $engine->isTextMode();
22 $mail_mode = $engine->isHTMLMailMode();
24 if (!$this->isFlatText($matches[0]) ||
$text_mode ||
$mail_mode) {
25 // We could do better than this in text mode and mail mode, but focus
30 $metadata_key = self
::KEY_SOURCELINKS
;
31 $metadata = $engine->getTextMetadata($metadata_key, array());
33 $token = $engine->storeText($matches[0]);
38 'input' => $matches[1],
41 $engine->setTextMetadata($metadata_key, $metadata);
46 public function didMarkupText() {
47 $engine = $this->getEngine();
48 $metadata_key = self
::KEY_SOURCELINKS
;
49 $metadata = $engine->getTextMetadata($metadata_key, array());
55 $viewer = $engine->getConfig('viewer');
68 foreach ($metadata as $ref) {
69 $token = $ref['token'];
71 $input = $ref['input'];
76 '(?:"(?P<quotedpath>(?:[^\\\\"]+|\\.)+)"|(?P<rawpath>[^\s,]+))'.
81 if (!preg_match($pattern, $input, $matches)) {
83 'Missing path, expected "{src path ...}" in: %s',
85 $hint = $this->newSyntaxHint($hint_text);
87 $engine->overwriteStoredText($token, $hint);
91 $path = idx($matches, 'rawpath');
93 $path = idx($matches, 'quotedpath');
94 $path = stripcslashes($path);
97 $parts = explode(':', $path, 2);
98 if (count($parts) == 2) {
99 $repository = nonempty($parts[0], null);
106 $options = $matches['options'];
108 $parser = new PhutilSimpleOptions();
109 $options = $parser->parse($options) +
$defaults;
111 foreach ($options as $key => $value) {
112 if (!array_key_exists($key, $defaults)) {
114 'Unknown option "%s" in: %s',
117 $hint = $this->newSyntaxHint($hint_text);
119 $engine->overwriteStoredText($token, $hint);
124 if ($options['repository'] !== null) {
125 $repository = $options['repository'];
128 if ($repository === null) {
130 'Missing repository, expected "{src repository:path ...}" '.
131 'or "{src path repository=...}" in: %s',
133 $hint = $this->newSyntaxHint($hint_text);
135 $engine->overwriteStoredText($token, $hint);
142 'identifier' => $repository,
144 'options' => $options,
152 $query = id(new PhabricatorRepositoryQuery())
154 ->withIdentifiers(ipull($tags, 'identifier'));
158 $repository_map = $query->getIdentifierMap();
160 foreach ($tags as $tag) {
161 $token = $tag['token'];
163 $identifier = $tag['identifier'];
164 $repository = idx($repository_map, $identifier);
166 // For now, just bail out here. Ideally, we should distingiush between
167 // restricted and invalid repositories.
171 $drequest = DiffusionRequest
::newFromDictionary(
174 'repository' => $repository,
177 $options = $tag['options'];
179 $line = $options['line'];
180 $commit = $options['commit'];
181 $ref_name = $options['ref'];
183 $link_uri = $drequest->generateURI(
185 'action' => 'browse',
186 'path' => $tag['path'],
189 'branch' => $ref_name,
192 $view = id(new DiffusionSourceLinkView())
193 ->setRepository($repository)
194 ->setPath($tag['path'])
197 if ($line !== null) {
198 $view->setLine($line);
201 if ($commit !== null) {
202 $view->setCommit($commit);
205 if ($ref_name !== null) {
206 $view->setRefName($ref_name);
209 $engine->overwriteStoredText($token, $view);
213 private function newSyntaxHint($text) {
214 return id(new PHUITagView())
215 ->setType(PHUITagView
::TYPE_SHADE
)
217 ->setIcon('fa-exclamation-triangle')