2011-07-15 23:17:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2012-01-16 19:07:21 +01:00
|
|
|
* Copyright 2012 Facebook, Inc.
|
2011-07-15 23:17:55 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group markup
|
|
|
|
*/
|
2012-03-14 00:21:04 +01:00
|
|
|
final class PhabricatorRemarkupRuleEmbedFile
|
2011-07-15 23:17:55 +02:00
|
|
|
extends PhutilRemarkupRule {
|
|
|
|
|
|
|
|
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-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-01-16 19:07:21 +01:00
|
|
|
$options = PhutilSimpleOptions::parse($matches[2]) + $options;
|
|
|
|
}
|
|
|
|
|
2012-03-23 23:32:07 +01:00
|
|
|
$file_name = coalesce($options['name'], $file->getName());
|
|
|
|
|
|
|
|
if (!$file->isViewableImage() || $options['layout'] == 'link') {
|
|
|
|
// If a file isn't in image, just render a link to it.
|
|
|
|
$link = phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
|
|
|
'href' => $file->getBestURI(),
|
|
|
|
'target' => '_blank',
|
|
|
|
'class' => 'phabricator-remarkup-embed-layout-link',
|
|
|
|
),
|
|
|
|
phutil_escape_html($file_name));
|
|
|
|
return $this->getEngine()->storeText($link);
|
|
|
|
}
|
|
|
|
|
2012-10-08 22:26:10 +02:00
|
|
|
$attrs = array();
|
2012-04-30 22:28:20 +02:00
|
|
|
|
2012-01-16 19:07:21 +01:00
|
|
|
switch ($options['size']) {
|
|
|
|
case 'full':
|
2012-04-30 22:28:20 +02:00
|
|
|
$attrs['src'] = $file->getBestURI();
|
2012-01-16 19:07:21 +01:00
|
|
|
$link = null;
|
|
|
|
break;
|
|
|
|
case 'thumb':
|
|
|
|
default:
|
2012-10-08 22:26:10 +02:00
|
|
|
$attrs['src'] = $file->getPreview220URI();
|
2012-01-16 19:07:21 +01:00
|
|
|
$link = $file->getBestURI();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-04-30 22:28:20 +02:00
|
|
|
$embed = phutil_render_tag('img', $attrs);
|
2012-01-16 19:07:21 +01:00
|
|
|
|
|
|
|
if ($link) {
|
|
|
|
$embed = phutil_render_tag(
|
|
|
|
'a',
|
|
|
|
array(
|
2012-10-08 22:26:10 +02:00
|
|
|
'href' => $link,
|
|
|
|
'class' => 'phabricator-remarkup-embed-image',
|
|
|
|
'target' => '_blank',
|
2012-01-16 19:07:21 +01:00
|
|
|
),
|
|
|
|
$embed);
|
|
|
|
}
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getEngine()->storeText($embed);
|
2011-07-15 23:17:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|