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);
+ $pageNav = $this->entityRepo->getPageNav($pageContent);
Views::add($page);
$this->setPageTitle($page->getShortName());
{
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$pdfContent = $this->exportService->pageToPdf($page);
+// return $pdfContent;
return response()->make($pdfContent, 200, [
'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'
use DOMDocument;
use DOMXPath;
use Illuminate\Support\Collection;
-use Symfony\Component\DomCrawler\Crawler;
class EntityRepo
{
*/
public function getById($type, $id, $allowDrafts = false)
{
- return $this->entityQuery($type, $allowDrafts)->findOrFail($id);
+ return $this->entityQuery($type, $allowDrafts)->find($id);
}
/**
*/
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);
+ $content = $page->html;
+ $matches = [];
+ preg_match_all("/{{@\s?([0-9].*?)}}/", $content, $matches);
+ if (count($matches[0]) === 0) return $content;
+
+ foreach ($matches[1] as $index => $includeId) {
+ $splitInclude = explode('#', $includeId, 2);
+ $pageId = intval($splitInclude[0]);
+ if (is_nan($pageId)) continue;
+
+ $page = $this->getById('page', $pageId);
+ if ($page === null) {
+ $content = str_replace($matches[0][$index], '', $content);
+ continue;
}
- }
- $body = $doc->getElementsByTagName('body')->item(0);
- $html = '';
- foreach ($body->childNodes as $node) {
- $html .= $doc->saveHTML($node);
+ if (count($splitInclude) === 1) {
+ $content = str_replace($matches[0][$index], $page->html, $content);
+ continue;
+ }
+
+ $doc = new DOMDocument();
+ $doc->loadHTML(mb_convert_encoding('<body>'.$page->html.'</body>', 'HTML-ENTITIES', 'UTF-8'));
+ $matchingElem = $doc->getElementById($splitInclude[1]);
+ if ($matchingElem === null) {
+ $content = str_replace($matches[0][$index], '', $content);
+ continue;
+ }
+ $innerContent = '';
+ foreach ($matchingElem->childNodes as $childNode) {
+ $innerContent .= $doc->saveHTML($childNode);
+ }
+ $content = str_replace($matches[0][$index], trim($innerContent), $content);
}
- return $html;
+ return $content;
}
/**
/**
* Parse the headers on the page to get a navigation menu
- * @param Page $page
+ * @param String $pageContent
* @return array
*/
- public function getPageNav(Page $page)
+ public function getPageNav($pageContent)
{
- if ($page->html == '') return [];
+ if ($pageContent == '') return [];
libxml_use_internal_errors(true);
$doc = new DOMDocument();
- $doc->loadHTML(mb_convert_encoding($page->html, 'HTML-ENTITIES', 'UTF-8'));
+ $doc->loadHTML(mb_convert_encoding($pageContent, 'HTML-ENTITIES', 'UTF-8'));
$xPath = new DOMXPath($doc);
$headers = $xPath->query("//h1|//h2|//h3|//h4|//h5|//h6");
<?php namespace BookStack\Services;
use BookStack\Page;
+use BookStack\Repos\EntityRepo;
class ExportService
{
+ protected $entityRepo;
+
+ /**
+ * ExportService constructor.
+ * @param $entityRepo
+ */
+ public function __construct(EntityRepo $entityRepo)
+ {
+ $this->entityRepo = $entityRepo;
+ }
+
/**
* Convert a page to a self-contained HTML file.
* Includes required CSS & image content. Images are base64 encoded into the HTML.
public function pageToContainedHtml(Page $page)
{
$cssContent = file_get_contents(public_path('/css/export-styles.css'));
- $pageHtml = view('pages/export', ['page' => $page, 'css' => $cssContent])->render();
+ $pageHtml = view('pages/export', ['page' => $page, 'pageContent' => $this->entityRepo->renderPage($page), 'css' => $cssContent])->render();
return $this->containHtml($pageHtml);
}
public function pageToPdf(Page $page)
{
$cssContent = file_get_contents(public_path('/css/export-styles.css'));
- $pageHtml = view('pages/pdf', ['page' => $page, 'css' => $cssContent])->render();
+ $pageHtml = view('pages/pdf', ['page' => $page, 'pageContent' => $this->entityRepo->renderPage($page), 'css' => $cssContent])->render();
+// return $pageHtml;
$useWKHTML = config('snappy.pdf.binary') !== false;
$containedHtml = $this->containHtml($pageHtml);
if ($useWKHTML) {
$pathString = $srcString;
}
if ($isLocal && !file_exists($pathString)) continue;
- $imageContent = file_get_contents($pathString);
- $imageEncoded = 'data:image/' . pathinfo($pathString, PATHINFO_EXTENSION) . ';base64,' . base64_encode($imageContent);
- $newImageString = str_replace($srcString, $imageEncoded, $oldImgString);
+ try {
+ $imageContent = file_get_contents($pathString);
+ $imageEncoded = 'data:image/' . pathinfo($pathString, PATHINFO_EXTENSION) . ';base64,' . base64_encode($imageContent);
+ $newImageString = str_replace($srcString, $imageEncoded, $oldImgString);
+ } catch (\ErrorException $e) {
+ $newImageString = '';
+ }
$htmlContent = str_replace($oldImgString, $newImageString, $htmlContent);
}
}
/**
* Converts the page contents into simple plain text.
- * This method filters any bad looking content to
- * provide a nice final output.
+ * This method filters any bad looking content to provide a nice final output.
* @param Page $page
* @return mixed
*/
public function pageToPlainText(Page $page)
{
- $text = $page->text;
+ $html = $this->entityRepo->renderPage($page);
+ $text = strip_tags($html);
// Replace multiple spaces with single spaces
$text = preg_replace('/\ {2,}/', ' ', $text);
// Reduce multiple horrid whitespace characters.