2011-07-15 23:17:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group markup
|
|
|
|
*/
|
2012-03-14 00:21:04 +01:00
|
|
|
final class PhabricatorRemarkupRuleEmbedFile
|
2011-07-15 23:17:55 +02:00
|
|
|
extends PhutilRemarkupRule {
|
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
const KEY_RULE_EMBED_FILE = 'rule.embed.file';
|
2013-01-25 02:23:05 +01:00
|
|
|
const KEY_EMBED_FILE_PHIDS = 'phabricator.embedded-file-phids';
|
2012-10-23 04:06:56 +02:00
|
|
|
|
2011-07-15 23:17:55 +02:00
|
|
|
public function apply($text) {
|
|
|
|
return preg_replace_callback(
|
2012-01-16 19:07:21 +01:00
|
|
|
"@{F(\d+)([^}]+?)?}@",
|
2011-07-15 23:17:55 +02:00
|
|
|
array($this, 'markupEmbedFile'),
|
|
|
|
$text);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function markupEmbedFile($matches) {
|
|
|
|
|
|
|
|
$file = null;
|
|
|
|
if ($matches[1]) {
|
|
|
|
// TODO: This is pretty inefficient if there are a bunch of files.
|
|
|
|
$file = id(new PhabricatorFile())->load($matches[1]);
|
|
|
|
}
|
|
|
|
|
2012-01-16 19:07:21 +01:00
|
|
|
if (!$file) {
|
2011-07-15 23:17:55 +02:00
|
|
|
return $matches[0];
|
|
|
|
}
|
2012-10-23 04:06:56 +02:00
|
|
|
$phid = $file->getPHID();
|
|
|
|
|
|
|
|
$engine = $this->getEngine();
|
|
|
|
$token = $engine->storeText('');
|
|
|
|
$metadata_key = self::KEY_RULE_EMBED_FILE;
|
|
|
|
$metadata = $engine->getTextMetadata($metadata_key, array());
|
|
|
|
$bundle = array('token' => $token);
|
2012-01-16 19:07:21 +01:00
|
|
|
|
|
|
|
$options = array(
|
|
|
|
'size' => 'thumb',
|
|
|
|
'layout' => 'left',
|
|
|
|
'float' => false,
|
2012-03-23 23:32:07 +01:00
|
|
|
'name' => null,
|
2012-01-16 19:07:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!empty($matches[2])) {
|
2012-02-18 01:22:02 +01:00
|
|
|
$matches[2] = trim($matches[2], ', ');
|
2012-11-29 15:05:35 +01:00
|
|
|
$parser = new PhutilSimpleOptions();
|
|
|
|
$options = $parser->parse($matches[2]) + $options;
|
2012-01-16 19:07:21 +01:00
|
|
|
}
|
2012-03-23 23:32:07 +01:00
|
|
|
$file_name = coalesce($options['name'], $file->getName());
|
2012-10-23 04:06:56 +02:00
|
|
|
$options['name'] = $file_name;
|
2012-03-23 23:32:07 +01:00
|
|
|
|
2012-10-08 22:26:10 +02:00
|
|
|
$attrs = array();
|
2013-01-02 23:03:29 +01:00
|
|
|
switch ((string)$options['size']) {
|
2012-01-16 19:07:21 +01:00
|
|
|
case 'full':
|
2012-04-30 22:28:20 +02:00
|
|
|
$attrs['src'] = $file->getBestURI();
|
2012-10-23 04:06:56 +02:00
|
|
|
$options['image_class'] = null;
|
2013-01-27 03:59:35 +01:00
|
|
|
$file_data = $file->getMetadata();
|
2013-01-27 04:03:00 +01:00
|
|
|
$height = idx($file_data, PhabricatorFile::METADATA_IMAGE_HEIGHT);
|
2013-01-27 03:59:35 +01:00
|
|
|
if ($height) {
|
|
|
|
$attrs['height'] = $height;
|
|
|
|
}
|
2013-01-27 04:03:00 +01:00
|
|
|
$width = idx($file_data, PhabricatorFile::METADATA_IMAGE_WIDTH);
|
2013-01-27 03:59:35 +01:00
|
|
|
if ($width) {
|
|
|
|
$attrs['width'] = $width;
|
|
|
|
}
|
2012-01-16 19:07:21 +01:00
|
|
|
break;
|
|
|
|
case 'thumb':
|
|
|
|
default:
|
2012-10-08 22:26:10 +02:00
|
|
|
$attrs['src'] = $file->getPreview220URI();
|
2013-01-30 01:52:39 +01:00
|
|
|
$dimensions =
|
|
|
|
PhabricatorImageTransformer::getPreviewDimensions($file, 220);
|
|
|
|
$attrs['width'] = $dimensions['sdx'];
|
|
|
|
$attrs['height'] = $dimensions['sdy'];
|
2012-10-23 04:06:56 +02:00
|
|
|
$options['image_class'] = 'phabricator-remarkup-embed-image';
|
2012-01-16 19:07:21 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-10-23 04:06:56 +02:00
|
|
|
$bundle['attrs'] = $attrs;
|
|
|
|
$bundle['options'] = $options;
|
|
|
|
|
|
|
|
$bundle['meta'] = array(
|
|
|
|
'phid' => $file->getPHID(),
|
|
|
|
'viewable' => $file->isViewableImage(),
|
|
|
|
'uri' => $file->getBestURI(),
|
|
|
|
'dUri' => $file->getDownloadURI(),
|
|
|
|
'name' => $options['name'],
|
|
|
|
);
|
|
|
|
$metadata[$phid][] = $bundle;
|
|
|
|
$engine->setTextMetadata($metadata_key, $metadata);
|
2012-01-16 19:07:21 +01:00
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
return $token;
|
|
|
|
}
|
2012-01-16 19:07:21 +01:00
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
public function didMarkupText() {
|
|
|
|
$engine = $this->getEngine();
|
2012-01-16 19:07:21 +01:00
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
$metadata_key = self::KEY_RULE_EMBED_FILE;
|
|
|
|
$metadata = $engine->getTextMetadata($metadata_key, array());
|
2012-01-16 19:07:21 +01:00
|
|
|
|
2012-10-23 04:06:56 +02:00
|
|
|
if (!$metadata) {
|
|
|
|
return;
|
2012-01-16 19:07:21 +01:00
|
|
|
}
|
|
|
|
|
2013-01-25 02:23:05 +01:00
|
|
|
$file_phids = array();
|
2012-10-23 04:06:56 +02:00
|
|
|
foreach ($metadata as $phid => $bundles) {
|
|
|
|
foreach ($bundles as $data) {
|
|
|
|
|
|
|
|
$options = $data['options'];
|
|
|
|
$meta = $data['meta'];
|
|
|
|
|
|
|
|
if (!$meta['viewable'] || $options['layout'] == 'link') {
|
|
|
|
$link = id(new PhabricatorFileLinkView())
|
|
|
|
->setFilePHID($meta['phid'])
|
|
|
|
->setFileName($meta['name'])
|
|
|
|
->setFileDownloadURI($meta['dUri'])
|
|
|
|
->setFileViewURI($meta['uri'])
|
|
|
|
->setFileViewable($meta['viewable']);
|
|
|
|
$embed = $link->render();
|
|
|
|
$engine->overwriteStoredText($data['token'], $embed);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
require_celerity_resource('lightbox-attachment-css');
|
|
|
|
$img = phutil_render_tag('img', $data['attrs']);
|
|
|
|
|
|
|
|
$embed = javelin_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
2012-11-22 17:25:13 +01:00
|
|
|
'href' => $meta['uri'],
|
2012-10-23 04:06:56 +02:00
|
|
|
'class' => $options['image_class'],
|
|
|
|
'sigil' => 'lightboxable',
|
|
|
|
'mustcapture' => true,
|
|
|
|
'meta' => $meta,
|
|
|
|
),
|
|
|
|
$img);
|
|
|
|
|
|
|
|
$layout_class = null;
|
|
|
|
switch ($options['layout']) {
|
|
|
|
case 'right':
|
|
|
|
case 'center':
|
|
|
|
case 'inline':
|
|
|
|
case 'left':
|
|
|
|
$layout_class = 'phabricator-remarkup-embed-layout-'.
|
|
|
|
$options['layout'];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$layout_class = 'phabricator-remarkup-embed-layout-left';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($options['float']) {
|
|
|
|
switch ($options['layout']) {
|
|
|
|
case 'center':
|
|
|
|
case 'inline':
|
|
|
|
break;
|
|
|
|
case 'right':
|
|
|
|
$layout_class .= ' phabricator-remarkup-embed-float-right';
|
|
|
|
break;
|
|
|
|
case 'left':
|
|
|
|
default:
|
|
|
|
$layout_class .= ' phabricator-remarkup-embed-float-left';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($layout_class) {
|
|
|
|
$embed = phutil_render_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => $layout_class,
|
|
|
|
),
|
|
|
|
$embed);
|
|
|
|
}
|
|
|
|
|
|
|
|
$engine->overwriteStoredText($data['token'], $embed);
|
|
|
|
}
|
2013-01-25 02:23:05 +01:00
|
|
|
$file_phids[] = $phid;
|
2012-10-23 04:06:56 +02:00
|
|
|
}
|
2013-01-25 02:23:05 +01:00
|
|
|
$engine->setTextMetadata(self::KEY_EMBED_FILE_PHIDS, $file_phids);
|
2012-10-23 04:06:56 +02:00
|
|
|
$engine->setTextMetadata($metadata_key, array());
|
2011-07-15 23:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|