1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2024-11-24 11:52:34 +01:00

Started page transclusion system

This commit is contained in:
Dan Brown 2017-01-16 21:24:48 +00:00
parent d8c5f72258
commit e4e3b25c22
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
3 changed files with 46 additions and 3 deletions

View File

@ -156,15 +156,19 @@ class PageController extends Controller
return redirect($page->getUrl());
}
$this->checkOwnablePermission('page-view', $page);
$pageContent = $this->entityRepo->renderPage($page);
$sidebarTree = $this->entityRepo->getBookChildren($page->book);
$pageNav = $this->entityRepo->getPageNav($page);
Views::add($page);
$this->setPageTitle($page->getShortName());
return view('pages/show', ['page' => $page, 'book' => $page->book,
'current' => $page, 'sidebarTree' => $sidebarTree, 'pageNav' => $pageNav]);
return view('pages/show', [
'page' => $page,'book' => $page->book,
'current' => $page, 'sidebarTree' => $sidebarTree,
'pageNav' => $pageNav, 'pageContent' => $pageContent]);
}
/**

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

View File

@ -7,6 +7,6 @@
@if (isset($diff) && $diff)
{!! $diff !!}
@else
{!! $page->html !!}
{!! isset($pageContent) ? $pageContent : $page->html !!}
@endif
</div>