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
2016-09-26 09:56:49 -04:00
|
|
|
<?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,
|
|
|
|
|
'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['uri']) {
|
|
|
|
|
$src_uri = id(new PhutilURI('/file/imageproxy/'))
|
|
|
|
|
->setQueryParam('uri', (string)$args['uri']);
|
|
|
|
|
$img = $this->newTag(
|
|
|
|
|
'img',
|
|
|
|
|
array(
|
|
|
|
|
'src' => $src_uri,
|
|
|
|
|
'alt' => $args['alt'],
|
|
|
|
|
'width' => $args['width'],
|
|
|
|
|
'height' => $args['height'],
|
2018-02-17 17:37:38 -08: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
2016-09-26 09:56:49 -04:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|