1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-11 17:32:41 +01:00
phorge-phorge/src/applications/macro/remarkup/PhabricatorRemarkupRuleImageMacro.php
Jakub Vrana efe0c135fe Implement text mode for Phabricator Remarkup rules
Test Plan:
  lang=remarkup
  D1
  {D1}
  {C1}
  {F1}
  [[ Test ]]
  iiam
  {meme, src=iiam, above="I\'m not always", below="But I am"}
  @{function:pht}
  @vrana

Reviewers: epriestley, edward

Reviewed By: epriestley

CC: aran, Korvin

Maniphest Tasks: T2617

Differential Revision: https://secure.phabricator.com/D5392
2013-03-22 17:33:36 -07:00

75 lines
1.8 KiB
PHP

<?php
/**
* @group markup
*/
final class PhabricatorRemarkupRuleImageMacro
extends PhutilRemarkupRule {
private $images;
public function apply($text) {
return preg_replace_callback(
'@^([a-zA-Z0-9:_\-]+)$@m',
array($this, 'markupImageMacro'),
$text);
}
public function markupImageMacro($matches) {
if ($this->images === null) {
$this->images = array();
$viewer = $this->getEngine()->getConfig('viewer');
$rows = id(new PhabricatorMacroQuery())
->setViewer($viewer)
->withStatus(PhabricatorMacroQuery::STATUS_ACTIVE)
->execute();
foreach ($rows as $row) {
$this->images[$row->getName()] = $row->getFilePHID();
}
}
$name = (string)$matches[1];
if (array_key_exists($name, $this->images)) {
$phid = $this->images[$name];
$file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $phid);
if ($this->getEngine()->isTextMode()) {
if ($file) {
$name .= ' <'.$file->getBestURI().'>';
}
return $this->getEngine()->storeText($name);
}
$style = null;
$src_uri = null;
if ($file) {
$src_uri = $file->getBestURI();
$file_data = $file->getMetadata();
$height = idx($file_data, PhabricatorFile::METADATA_IMAGE_HEIGHT);
$width = idx($file_data, PhabricatorFile::METADATA_IMAGE_WIDTH);
if ($height && $width) {
$style = sprintf(
'height: %dpx; width: %dpx;',
$height,
$width);
}
}
$img = phutil_tag(
'img',
array(
'src' => $src_uri,
'alt' => $matches[1],
'title' => $matches[1],
'style' => $style,
));
return $this->getEngine()->storeText($img);
} else {
return $matches[1];
}
}
}