daux.io/libs/Format/Confluence/ContentPage.php

75 regels
2.6 KiB
PHP

<?php namespace Todaymade\Daux\Format\Confluence;
use Todaymade\Daux\Format\Base\EmbedImages;
use Todaymade\Daux\Tree\ComputedRaw;
use Todaymade\Daux\Tree\Entry;
use Todaymade\Daux\Tree\Raw;
class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage
{
public $attachments = [];
protected function generatePage()
{
$content = parent::generatePage();
//Embed images
// We do it after generation so we can catch the images that were in html already
$content = (new EmbedImages($this->config->getTree()))
->embed(
$content,
$this->file,
2020-04-22 21:55:53 +02:00
function ($src, array $attributes, Entry $file) {
//Add the attachment for later upload
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) {
$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.");
}
return $this->createImageTag($filename, $attributes);
}
);
$intro = '';
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>';
}
return $intro . $content;
}
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
*/
private function createImageTag($filename, $attributes)
{
2016-07-27 21:32:51 +02:00
$img = '<ac:image';
foreach ($attributes as $name => $value) {
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);
}
}
2015-07-17 23:38:06 +02:00
$img .= ' ac:' . $name . '="' . htmlentities($value, ENT_QUOTES, 'UTF-8', false) . '"';
}
$img .= "><ri:attachment ri:filename=\"$filename\" /></ac:image>";
return $img;
}
}