2015-05-21 17:39:11 +02:00
|
|
|
<?php namespace Todaymade\Daux\Format\Confluence;
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
use DOMDocument;
|
2015-05-21 17:39:11 +02:00
|
|
|
use Todaymade\Daux\DauxHelper;
|
|
|
|
|
2015-07-28 17:25:03 +02:00
|
|
|
class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage
|
2015-05-21 17:39:11 +02:00
|
|
|
{
|
2015-05-22 14:48:09 +02:00
|
|
|
public $attachments = [];
|
|
|
|
|
2015-05-21 17:39:11 +02:00
|
|
|
protected function generatePage()
|
|
|
|
{
|
|
|
|
$page = parent::generatePage();
|
|
|
|
|
|
|
|
//Embed images
|
2015-05-22 14:48:09 +02:00
|
|
|
// We do it after generation so we can catch the images that were in html already
|
2015-05-21 17:39:11 +02:00
|
|
|
$page = preg_replace_callback(
|
|
|
|
"/<img\\s+[^>]*src=['\"]([^\"]*)['\"][^>]*>/",
|
2015-07-17 23:38:06 +02:00
|
|
|
function($matches) {
|
2015-05-22 14:48:09 +02:00
|
|
|
|
|
|
|
if ($result = $this->findImage($matches[1], $matches[0])) {
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $matches[0];
|
2015-05-21 17:39:11 +02:00
|
|
|
},
|
|
|
|
$page
|
|
|
|
);
|
|
|
|
|
|
|
|
return $page;
|
|
|
|
}
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
private function findImage($src, $tag)
|
2015-05-21 17:39:11 +02:00
|
|
|
{
|
|
|
|
//for protocol relative or http requests : keep the original one
|
|
|
|
if (substr($src, 0, strlen("http")) === "http" || substr($src, 0, strlen("//")) === "//") {
|
|
|
|
return $src;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get the path to the file, relative to the root of the documentation
|
|
|
|
$url = DauxHelper::getCleanPath(dirname($this->file->getUrl()) . '/' . $src);
|
|
|
|
|
|
|
|
//Get any file corresponding to the right one
|
|
|
|
$file = DauxHelper::getFile($this->params['tree'], $url);
|
|
|
|
|
|
|
|
|
|
|
|
if ($file === false) {
|
2015-05-22 14:48:09 +02:00
|
|
|
return false;
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
$filename = basename($file->getPath());
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
//Add the attachment for later upload
|
|
|
|
$this->attachments[] = ['filename' => $filename, 'file' => $file];
|
|
|
|
|
|
|
|
return $this->createImageTag($filename, $this->getAttributes($tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getAttributes($tag)
|
|
|
|
{
|
|
|
|
$dom = new DOMDocument();
|
|
|
|
$dom->loadHTML($tag);
|
|
|
|
|
|
|
|
$img = $dom->getElementsByTagName('img')[0];
|
|
|
|
|
|
|
|
$attributes = ['align', 'class', 'title', 'style', 'alt', 'height', 'width'];
|
|
|
|
$used = [];
|
|
|
|
foreach ($attributes as $attr) {
|
|
|
|
if ($img->attributes->getNamedItem($attr)) {
|
|
|
|
$used[$attr] = $img->attributes->getNamedItem($attr)->value;
|
|
|
|
}
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-05-22 14:48:09 +02:00
|
|
|
return $used;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createImageTag($filename, $attributes)
|
|
|
|
{
|
|
|
|
$img = "<ac:image";
|
|
|
|
|
|
|
|
foreach ($attributes as $name => $value) {
|
2015-07-17 23:38:06 +02:00
|
|
|
$img .= ' ac:' . $name . '="' . htmlentities($value, ENT_QUOTES, 'UTF-8', false) . '"';
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$img .= "><ri:attachment ri:filename=\"$filename\" /></ac:image>";
|
|
|
|
|
|
|
|
return $img;
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
}
|