@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Remarkup rule to embed images

Summary:
Ref T4190. Added the remarkup rule to embed images:

Syntax is as follows:

`{image <IMAGE_URL>}`

Parameters are also supported, like:
`{image uri=<IMAGE_URI>, width=500px, height=200px, alt=picture of a moose, href=google.com}`

URLs without a protocol are not supported.

Test Plan: Tested with many of the syntax variations. If the provided URL doesn't point to an image, then a broken image icon will be shown.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin, epriestley, yelirekim

Maniphest Tasks: T4190

Differential Revision: https://secure.phabricator.com/D16597

Josh Cox 26b29a60 7d576c3f

+78
+2
src/__phutil_library_map__.php
··· 2688 2688 'PhabricatorIconSetEditField' => 'applications/transactions/editfield/PhabricatorIconSetEditField.php', 2689 2689 'PhabricatorIconSetIcon' => 'applications/files/iconset/PhabricatorIconSetIcon.php', 2690 2690 'PhabricatorImageMacroRemarkupRule' => 'applications/macro/markup/PhabricatorImageMacroRemarkupRule.php', 2691 + 'PhabricatorImageRemarkupRule' => 'applications/files/markup/PhabricatorImageRemarkupRule.php', 2691 2692 'PhabricatorImageTransformer' => 'applications/files/PhabricatorImageTransformer.php', 2692 2693 'PhabricatorImagemagickSetupCheck' => 'applications/config/check/PhabricatorImagemagickSetupCheck.php', 2693 2694 'PhabricatorInFlightErrorView' => 'applications/config/view/PhabricatorInFlightErrorView.php', ··· 7523 7524 'PhabricatorIconSetEditField' => 'PhabricatorEditField', 7524 7525 'PhabricatorIconSetIcon' => 'Phobject', 7525 7526 'PhabricatorImageMacroRemarkupRule' => 'PhutilRemarkupRule', 7527 + 'PhabricatorImageRemarkupRule' => 'PhutilRemarkupRule', 7526 7528 'PhabricatorImageTransformer' => 'Phobject', 7527 7529 'PhabricatorImagemagickSetupCheck' => 'PhabricatorSetupCheck', 7528 7530 'PhabricatorInFlightErrorView' => 'AphrontView',
+1
src/applications/files/application/PhabricatorFilesApplication.php
··· 37 37 public function getRemarkupRules() { 38 38 return array( 39 39 new PhabricatorEmbedFileRemarkupRule(), 40 + new PhabricatorImageRemarkupRule(), 40 41 ); 41 42 } 42 43
+75
src/applications/files/markup/PhabricatorImageRemarkupRule.php
··· 1 + <?php 2 + 3 + final class PhabricatorImageRemarkupRule extends PhutilRemarkupRule { 4 + public function getPriority() { 5 + return 200.0; 6 + } 7 + 8 + public function apply($text) { 9 + return preg_replace_callback( 10 + '@{(image|img) ((?:[^}\\\\]+|\\\\.)*)}@m', 11 + array($this, 'markupImage'), 12 + $text); 13 + } 14 + 15 + public function markupImage(array $matches) { 16 + if (!$this->isFlatText($matches[0])) { 17 + return $matches[0]; 18 + } 19 + $args = array(); 20 + $defaults = array( 21 + 'uri' => null, 22 + 'alt' => null, 23 + 'href' => null, 24 + 'width' => null, 25 + 'height' => null, 26 + ); 27 + $trimmed_match = trim($matches[2]); 28 + if ($this->isURI($trimmed_match)) { 29 + $args['uri'] = new PhutilURI($trimmed_match); 30 + } else { 31 + $parser = new PhutilSimpleOptions(); 32 + $keys = $parser->parse($trimmed_match); 33 + 34 + $uri_key = ''; 35 + foreach (array('src', 'uri', 'url') as $key) { 36 + if (array_key_exists($key, $keys)) { 37 + $uri_key = $key; 38 + } 39 + } 40 + if ($uri_key) { 41 + $args['uri'] = new PhutilURI($keys[$uri_key]); 42 + } 43 + $args += $keys; 44 + } 45 + 46 + $args += $defaults; 47 + 48 + if ($args['href'] && !PhabricatorEnv::isValidURIForLink($args['href'])) { 49 + $args['href'] = null; 50 + } 51 + 52 + if ($args['uri']) { 53 + $src_uri = id(new PhutilURI('/file/imageproxy/')) 54 + ->setQueryParam('uri', (string)$args['uri']); 55 + $img = $this->newTag( 56 + 'img', 57 + array( 58 + 'src' => $src_uri, 59 + 'alt' => $args['alt'], 60 + 'href' => $args['href'], 61 + 'width' => $args['width'], 62 + 'height' => $args['height'], 63 + )); 64 + return $this->getEngine()->storeText($img); 65 + } else { 66 + return $matches[0]; 67 + } 68 + } 69 + 70 + private function isURI($uri_string) { 71 + // Very simple check to make sure it starts with either http or https. 72 + // If it does, we'll try to treat it like a valid URI 73 + return preg_match('~^https?\:\/\/.*\z~i', $uri_string); 74 + } 75 + }