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());
}
$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());
@ -434,6 +433,7 @@ class PageController extends Controller
{
$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'

View File

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

View File

@ -13,7 +13,6 @@ use Carbon\Carbon;
use DOMDocument;
use DOMXPath;
use Illuminate\Support\Collection;
use Symfony\Component\DomCrawler\Crawler;
class EntityRepo
{
@ -140,7 +139,7 @@ class EntityRepo
*/
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)
{
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);
$content = $page->html;
$matches = [];
preg_match_all("/{{@\s?([0-9].*?)}}/", $content, $matches);
if (count($matches[0]) === 0) return $content;
$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);
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;
}
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);
$html = '';
foreach ($body->childNodes as $node) {
$html .= $doc->saveHTML($node);
}
return $html;
return $content;
}
/**
@ -874,15 +881,15 @@ class EntityRepo
/**
* 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");

View File

@ -1,10 +1,22 @@
<?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.
@ -14,7 +26,7 @@ class ExportService
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);
}
@ -26,7 +38,8 @@ class ExportService
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) {
@ -59,9 +72,13 @@ class ExportService
$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);
}
}
@ -88,14 +105,14 @@ class ExportService
/**
* 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.

View File

@ -136,9 +136,6 @@
background-color: #EEE;
padding: $-s;
display: block;
> * {
display: inline-block;
}
&:before {
font-family: 'Material-Design-Iconic-Font';
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 {
margin: 0;
}
//body.ie #entity-selector-wrap .popup-body .form-group {
// min-height: 60vh;
//}
.image-manager-body {
min-height: 70vh;

View File

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