Create single-page HTML output, prepares the terrain for PDF/ePub output
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
<?php namespace Todaymade\Daux\Format\Confluence;
|
||||
|
||||
use DOMDocument;
|
||||
use Todaymade\Daux\DauxHelper;
|
||||
use Todaymade\Daux\Format\Base\EmbedImages;
|
||||
use Todaymade\Daux\Tree\Raw;
|
||||
|
||||
class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage
|
||||
{
|
||||
@ -9,68 +9,25 @@ class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage
|
||||
|
||||
protected function generatePage()
|
||||
{
|
||||
$page = parent::generatePage();
|
||||
$content = parent::generatePage();
|
||||
|
||||
//Embed images
|
||||
// We do it after generation so we can catch the images that were in html already
|
||||
$page = preg_replace_callback(
|
||||
"/<img\\s+[^>]*src=['\"]([^\"]*)['\"][^>]*>/",
|
||||
function($matches) {
|
||||
$content = (new EmbedImages($this->params['tree']))
|
||||
->embed(
|
||||
$content,
|
||||
$this->file,
|
||||
function ($src, array $attributes, Raw $file) {
|
||||
$filename = basename($file->getPath());
|
||||
|
||||
if ($result = $this->findImage($matches[1], $matches[0])) {
|
||||
return $result;
|
||||
//Add the attachment for later upload
|
||||
$this->attachments[] = ['filename' => $filename, 'file' => $file];
|
||||
|
||||
return $this->createImageTag($filename, $attributes);
|
||||
}
|
||||
);
|
||||
|
||||
return $matches[0];
|
||||
},
|
||||
$page
|
||||
);
|
||||
|
||||
return $page;
|
||||
}
|
||||
|
||||
private function findImage($src, $tag)
|
||||
{
|
||||
//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) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$filename = basename($file->getPath());
|
||||
|
||||
//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;
|
||||
}
|
||||
}
|
||||
|
||||
return $used;
|
||||
return $content;
|
||||
}
|
||||
|
||||
private function createImageTag($filename, $attributes)
|
||||
|
@ -17,11 +17,11 @@ class ImageRenderer extends \League\CommonMark\Inline\Renderer\ImageRenderer
|
||||
{
|
||||
// External Images need special handling
|
||||
if (strpos($inline->getUrl(), 'http') === 0) {
|
||||
return new HtmlElement(
|
||||
'ac:image',
|
||||
[],
|
||||
new HtmlElement('ri:url', ['ri:value' => $inline->getUrl()])
|
||||
);
|
||||
return new HtmlElement(
|
||||
'ac:image',
|
||||
[],
|
||||
new HtmlElement('ri:url', ['ri:value' => $inline->getUrl()])
|
||||
);
|
||||
}
|
||||
|
||||
return parent::render($inline, $htmlRenderer);
|
||||
|
Verwijs in nieuw issue
Block a user