mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-22 05:20:56 +01:00
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
This commit is contained in:
parent
7d576c3f94
commit
26b29a60c0
3 changed files with 78 additions and 0 deletions
|
@ -2688,6 +2688,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorIconSetEditField' => 'applications/transactions/editfield/PhabricatorIconSetEditField.php',
|
||||
'PhabricatorIconSetIcon' => 'applications/files/iconset/PhabricatorIconSetIcon.php',
|
||||
'PhabricatorImageMacroRemarkupRule' => 'applications/macro/markup/PhabricatorImageMacroRemarkupRule.php',
|
||||
'PhabricatorImageRemarkupRule' => 'applications/files/markup/PhabricatorImageRemarkupRule.php',
|
||||
'PhabricatorImageTransformer' => 'applications/files/PhabricatorImageTransformer.php',
|
||||
'PhabricatorImagemagickSetupCheck' => 'applications/config/check/PhabricatorImagemagickSetupCheck.php',
|
||||
'PhabricatorInFlightErrorView' => 'applications/config/view/PhabricatorInFlightErrorView.php',
|
||||
|
@ -7523,6 +7524,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorIconSetEditField' => 'PhabricatorEditField',
|
||||
'PhabricatorIconSetIcon' => 'Phobject',
|
||||
'PhabricatorImageMacroRemarkupRule' => 'PhutilRemarkupRule',
|
||||
'PhabricatorImageRemarkupRule' => 'PhutilRemarkupRule',
|
||||
'PhabricatorImageTransformer' => 'Phobject',
|
||||
'PhabricatorImagemagickSetupCheck' => 'PhabricatorSetupCheck',
|
||||
'PhabricatorInFlightErrorView' => 'AphrontView',
|
||||
|
|
|
@ -37,6 +37,7 @@ final class PhabricatorFilesApplication extends PhabricatorApplication {
|
|||
public function getRemarkupRules() {
|
||||
return array(
|
||||
new PhabricatorEmbedFileRemarkupRule(),
|
||||
new PhabricatorImageRemarkupRule(),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
final class PhabricatorImageRemarkupRule extends PhutilRemarkupRule {
|
||||
public function getPriority() {
|
||||
return 200.0;
|
||||
}
|
||||
|
||||
public function apply($text) {
|
||||
return preg_replace_callback(
|
||||
'@{(image|img) ((?:[^}\\\\]+|\\\\.)*)}@m',
|
||||
array($this, 'markupImage'),
|
||||
$text);
|
||||
}
|
||||
|
||||
public function markupImage(array $matches) {
|
||||
if (!$this->isFlatText($matches[0])) {
|
||||
return $matches[0];
|
||||
}
|
||||
$args = array();
|
||||
$defaults = array(
|
||||
'uri' => null,
|
||||
'alt' => null,
|
||||
'href' => null,
|
||||
'width' => null,
|
||||
'height' => null,
|
||||
);
|
||||
$trimmed_match = trim($matches[2]);
|
||||
if ($this->isURI($trimmed_match)) {
|
||||
$args['uri'] = new PhutilURI($trimmed_match);
|
||||
} else {
|
||||
$parser = new PhutilSimpleOptions();
|
||||
$keys = $parser->parse($trimmed_match);
|
||||
|
||||
$uri_key = '';
|
||||
foreach (array('src', 'uri', 'url') as $key) {
|
||||
if (array_key_exists($key, $keys)) {
|
||||
$uri_key = $key;
|
||||
}
|
||||
}
|
||||
if ($uri_key) {
|
||||
$args['uri'] = new PhutilURI($keys[$uri_key]);
|
||||
}
|
||||
$args += $keys;
|
||||
}
|
||||
|
||||
$args += $defaults;
|
||||
|
||||
if ($args['href'] && !PhabricatorEnv::isValidURIForLink($args['href'])) {
|
||||
$args['href'] = null;
|
||||
}
|
||||
|
||||
if ($args['uri']) {
|
||||
$src_uri = id(new PhutilURI('/file/imageproxy/'))
|
||||
->setQueryParam('uri', (string)$args['uri']);
|
||||
$img = $this->newTag(
|
||||
'img',
|
||||
array(
|
||||
'src' => $src_uri,
|
||||
'alt' => $args['alt'],
|
||||
'href' => $args['href'],
|
||||
'width' => $args['width'],
|
||||
'height' => $args['height'],
|
||||
));
|
||||
return $this->getEngine()->storeText($img);
|
||||
} else {
|
||||
return $matches[0];
|
||||
}
|
||||
}
|
||||
|
||||
private function isURI($uri_string) {
|
||||
// Very simple check to make sure it starts with either http or https.
|
||||
// If it does, we'll try to treat it like a valid URI
|
||||
return preg_match('~^https?\:\/\/.*\z~i', $uri_string);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue