Updated page include logic to use blade-style tags

It will also snippets of a page if and id is provided in a tag
This commit is contained in:
Dan Brown
2017-01-21 13:53:00 +00:00
parent e4e3b25c22
commit a4f6bc63f0
7 changed files with 66 additions and 50 deletions

View File

@ -156,12 +156,11 @@ class PageController extends Controller
return redirect($page->getUrl()); return redirect($page->getUrl());
} }
$this->checkOwnablePermission('page-view', $page); $this->checkOwnablePermission('page-view', $page);
$pageContent = $this->entityRepo->renderPage($page); $pageContent = $this->entityRepo->renderPage($page);
$sidebarTree = $this->entityRepo->getBookChildren($page->book); $sidebarTree = $this->entityRepo->getBookChildren($page->book);
$pageNav = $this->entityRepo->getPageNav($page); $pageNav = $this->entityRepo->getPageNav($pageContent);
Views::add($page); Views::add($page);
$this->setPageTitle($page->getShortName()); $this->setPageTitle($page->getShortName());
@ -434,6 +433,7 @@ class PageController extends Controller
{ {
$page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug); $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug);
$pdfContent = $this->exportService->pageToPdf($page); $pdfContent = $this->exportService->pageToPdf($page);
// return $pdfContent;
return response()->make($pdfContent, 200, [ return response()->make($pdfContent, 200, [
'Content-Type' => 'application/octet-stream', 'Content-Type' => 'application/octet-stream',
'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf' 'Content-Disposition' => 'attachment; filename="' . $pageSlug . '.pdf'

View File

@ -1,6 +1,4 @@
<?php <?php namespace BookStack\Http\Middleware;
namespace BookStack\Http\Middleware;
use Carbon\Carbon; use Carbon\Carbon;
use Closure; use Closure;

View File

@ -13,7 +13,6 @@ use Carbon\Carbon;
use DOMDocument; use DOMDocument;
use DOMXPath; use DOMXPath;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Symfony\Component\DomCrawler\Crawler;
class EntityRepo class EntityRepo
{ {
@ -140,7 +139,7 @@ class EntityRepo
*/ */
public function getById($type, $id, $allowDrafts = false) public function getById($type, $id, $allowDrafts = false)
{ {
return $this->entityQuery($type, $allowDrafts)->findOrFail($id); return $this->entityQuery($type, $allowDrafts)->find($id);
} }
/** /**
@ -805,34 +804,42 @@ class EntityRepo
*/ */
public function renderPage(Page $page) public function renderPage(Page $page)
{ {
libxml_use_internal_errors(true); $content = $page->html;
$doc = new DOMDocument(); $matches = [];
$doc->loadHTML(mb_convert_encoding('<body>'.$page->html.'</body>', 'HTML-ENTITIES', 'UTF-8')); preg_match_all("/{{@\s?([0-9].*?)}}/", $content, $matches);
$xpath = new DOMXpath($doc); if (count($matches[0]) === 0) return $content;
$bsElems = $xpath->query('body/div[@bs-embed-page]'); foreach ($matches[1] as $index => $includeId) {
if (is_null($bsElems)) return $page->html; $splitInclude = explode('#', $includeId, 2);
foreach ($bsElems as $bsElem) { $pageId = intval($splitInclude[0]);
$pageId = intval($bsElem->getAttribute('bs-embed-page')); if (is_nan($pageId)) continue;
$embeddedPage = $this->getById('page', $pageId);
if ($embeddedPage !== null) { $page = $this->getById('page', $pageId);
$innerPage = $doc->createDocumentFragment(); if ($page === null) {
$innerPage->appendXML($embeddedPage->html); $content = str_replace($matches[0][$index], '', $content);
// Empty div then append in child content continue;
foreach ($bsElem->childNodes as $child) {
$bsElem->removeChild($child);
}
$bsElem->appendChild($innerPage);
} }
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);
} }
$body = $doc->getElementsByTagName('body')->item(0); return $content;
$html = '';
foreach ($body->childNodes as $node) {
$html .= $doc->saveHTML($node);
}
return $html;
} }
/** /**
@ -874,15 +881,15 @@ class EntityRepo
/** /**
* Parse the headers on the page to get a navigation menu * Parse the headers on the page to get a navigation menu
* @param Page $page * @param String $pageContent
* @return array * @return array
*/ */
public function getPageNav(Page $page) public function getPageNav($pageContent)
{ {
if ($page->html == '') return []; if ($pageContent == '') return [];
libxml_use_internal_errors(true); libxml_use_internal_errors(true);
$doc = new DOMDocument(); $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); $xPath = new DOMXPath($doc);
$headers = $xPath->query("//h1|//h2|//h3|//h4|//h5|//h6"); $headers = $xPath->query("//h1|//h2|//h3|//h4|//h5|//h6");

View File

@ -1,10 +1,22 @@
<?php namespace BookStack\Services; <?php namespace BookStack\Services;
use BookStack\Page; use BookStack\Page;
use BookStack\Repos\EntityRepo;
class ExportService 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. * Convert a page to a self-contained HTML file.
* Includes required CSS & image content. Images are base64 encoded into the HTML. * Includes required CSS & image content. Images are base64 encoded into the HTML.
@ -14,7 +26,7 @@ class ExportService
public function pageToContainedHtml(Page $page) public function pageToContainedHtml(Page $page)
{ {
$cssContent = file_get_contents(public_path('/css/export-styles.css')); $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); return $this->containHtml($pageHtml);
} }
@ -26,7 +38,8 @@ class ExportService
public function pageToPdf(Page $page) public function pageToPdf(Page $page)
{ {
$cssContent = file_get_contents(public_path('/css/export-styles.css')); $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; $useWKHTML = config('snappy.pdf.binary') !== false;
$containedHtml = $this->containHtml($pageHtml); $containedHtml = $this->containHtml($pageHtml);
if ($useWKHTML) { if ($useWKHTML) {
@ -59,9 +72,13 @@ class ExportService
$pathString = $srcString; $pathString = $srcString;
} }
if ($isLocal && !file_exists($pathString)) continue; if ($isLocal && !file_exists($pathString)) continue;
$imageContent = file_get_contents($pathString); try {
$imageEncoded = 'data:image/' . pathinfo($pathString, PATHINFO_EXTENSION) . ';base64,' . base64_encode($imageContent); $imageContent = file_get_contents($pathString);
$newImageString = str_replace($srcString, $imageEncoded, $oldImgString); $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); $htmlContent = str_replace($oldImgString, $newImageString, $htmlContent);
} }
} }
@ -88,14 +105,14 @@ class ExportService
/** /**
* Converts the page contents into simple plain text. * Converts the page contents into simple plain text.
* This method filters any bad looking content to * This method filters any bad looking content to provide a nice final output.
* provide a nice final output.
* @param Page $page * @param Page $page
* @return mixed * @return mixed
*/ */
public function pageToPlainText(Page $page) public function pageToPlainText(Page $page)
{ {
$text = $page->text; $html = $this->entityRepo->renderPage($page);
$text = strip_tags($html);
// Replace multiple spaces with single spaces // Replace multiple spaces with single spaces
$text = preg_replace('/\ {2,}/', ' ', $text); $text = preg_replace('/\ {2,}/', ' ', $text);
// Reduce multiple horrid whitespace characters. // Reduce multiple horrid whitespace characters.

View File

@ -136,9 +136,6 @@
background-color: #EEE; background-color: #EEE;
padding: $-s; padding: $-s;
display: block; display: block;
> * {
display: inline-block;
}
&:before { &:before {
font-family: 'Material-Design-Iconic-Font'; font-family: 'Material-Design-Iconic-Font';
padding-right: $-s; padding-right: $-s;

View File

@ -70,9 +70,6 @@ body.flexbox-support #entity-selector-wrap .popup-body .form-group {
#entity-selector-wrap .popup-body .form-group { #entity-selector-wrap .popup-body .form-group {
margin: 0; margin: 0;
} }
//body.ie #entity-selector-wrap .popup-body .form-group {
// min-height: 60vh;
//}
.image-manager-body { .image-manager-body {
min-height: 70vh; min-height: 70vh;

View File

@ -1,4 +1,4 @@
//@import "reset"; @import "reset";
@import "variables"; @import "variables";
@import "mixins"; @import "mixins";
@import "html"; @import "html";