Started page transclusion system

This commit is contained in:
Dan Brown
2017-01-16 21:24:48 +00:00
parent d8c5f72258
commit e4e3b25c22
3 changed files with 46 additions and 3 deletions

View File

@ -13,6 +13,7 @@ use Carbon\Carbon;
use DOMDocument;
use DOMXPath;
use Illuminate\Support\Collection;
use Symfony\Component\DomCrawler\Crawler;
class EntityRepo
{
@ -796,6 +797,44 @@ class EntityRepo
return $html;
}
/**
* Render the page for viewing, Parsing and performing features such as page transclusion.
* @param Page $page
* @return mixed|string
*/
public function renderPage(Page $page)
{
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML(mb_convert_encoding('<body>'.$page->html.'</body>', 'HTML-ENTITIES', 'UTF-8'));
$xpath = new DOMXpath($doc);
$bsElems = $xpath->query('body/div[@bs-embed-page]');
if (is_null($bsElems)) return $page->html;
foreach ($bsElems as $bsElem) {
$pageId = intval($bsElem->getAttribute('bs-embed-page'));
$embeddedPage = $this->getById('page', $pageId);
if ($embeddedPage !== null) {
$innerPage = $doc->createDocumentFragment();
$innerPage->appendXML($embeddedPage->html);
// Empty div then append in child content
foreach ($bsElem->childNodes as $child) {
$bsElem->removeChild($child);
}
$bsElem->appendChild($innerPage);
}
}
$body = $doc->getElementsByTagName('body')->item(0);
$html = '';
foreach ($body->childNodes as $node) {
$html .= $doc->saveHTML($node);
}
return $html;
}
/**
* Get a new draft page instance.
* @param Book $book