2015-05-21 17:39:11 +02:00
|
|
|
<?php namespace Todaymade\Daux\Format\Confluence;
|
|
|
|
|
2015-11-06 22:44:34 +01:00
|
|
|
use Todaymade\Daux\Format\Base\EmbedImages;
|
2017-03-02 18:17:29 +01:00
|
|
|
use Todaymade\Daux\Tree\ComputedRaw;
|
|
|
|
use Todaymade\Daux\Tree\Entry;
|
2015-11-06 22:44:34 +01:00
|
|
|
use Todaymade\Daux\Tree\Raw;
|
2015-05-21 17:39:11 +02:00
|
|
|
|
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()
|
|
|
|
{
|
2015-11-06 22:44:34 +01:00
|
|
|
$content = parent::generatePage();
|
2015-05-21 17:39:11 +02:00
|
|
|
|
|
|
|
//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
|
2019-12-05 21:25:58 +01:00
|
|
|
$content = (new EmbedImages($this->config->getTree()))
|
2015-11-06 22:44:34 +01:00
|
|
|
->embed(
|
|
|
|
$content,
|
|
|
|
$this->file,
|
2020-04-22 21:55:53 +02:00
|
|
|
function ($src, array $attributes, Entry $file) {
|
2015-11-06 22:44:34 +01:00
|
|
|
//Add the attachment for later upload
|
2017-03-02 18:17:29 +01:00
|
|
|
if ($file instanceof Raw) {
|
|
|
|
$filename = basename($file->getPath());
|
|
|
|
$this->attachments[$filename] = ['filename' => $filename, 'file' => $file];
|
2020-04-22 21:55:53 +02:00
|
|
|
} elseif ($file instanceof ComputedRaw) {
|
2017-03-02 18:17:29 +01:00
|
|
|
$filename = $file->getUri();
|
|
|
|
$this->attachments[$filename] = ['filename' => $filename, 'content' => $file->getContent()];
|
2017-11-07 22:54:31 +01:00
|
|
|
} else {
|
|
|
|
throw new \RuntimeException("Cannot embed image as we don't understand its type.");
|
2017-03-02 18:17:29 +01:00
|
|
|
}
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2015-11-06 22:44:34 +01:00
|
|
|
return $this->createImageTag($filename, $attributes);
|
|
|
|
}
|
|
|
|
);
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2016-04-14 12:03:25 +02:00
|
|
|
$intro = '';
|
2019-12-05 21:25:58 +01:00
|
|
|
if ($this->config->getConfluenceConfiguration()->hasHeader()) {
|
|
|
|
$intro = '<ac:structured-macro ac:name="info"><ac:rich-text-body>' . $this->config->getConfluenceConfiguration()->getHeader() . '</ac:rich-text-body></ac:structured-macro>';
|
2016-04-14 12:03:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $intro . $content;
|
2015-05-22 14:48:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-07 23:40:12 +02:00
|
|
|
/**
|
2020-04-22 22:24:52 +02:00
|
|
|
* Create an image tag for the specified filename.
|
2017-06-07 23:40:12 +02:00
|
|
|
*
|
|
|
|
* @param string $filename
|
|
|
|
* @param array $attributes
|
2020-04-22 22:24:52 +02:00
|
|
|
*
|
2017-06-07 23:40:12 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2015-05-22 14:48:09 +02:00
|
|
|
private function createImageTag($filename, $attributes)
|
|
|
|
{
|
2016-07-27 21:32:51 +02:00
|
|
|
$img = '<ac:image';
|
2015-05-22 14:48:09 +02:00
|
|
|
|
|
|
|
foreach ($attributes as $name => $value) {
|
2017-03-02 18:17:29 +01:00
|
|
|
if ($name == 'style') {
|
|
|
|
$re = '/float:\s*?(left|right);?/';
|
|
|
|
if (preg_match($re, $value, $matches)) {
|
|
|
|
$img .= ' ac:align="' . $matches[1] . '"';
|
2020-04-22 22:24:52 +02:00
|
|
|
$value = preg_replace($re, '', $value, 1);
|
2017-03-02 18:17:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|