+
+ /**
+ * Render the page for viewing, Parsing and performing features such as page transclusion.
+ * @param Page $page
+ * @return mixed|string
+ */
+ public function renderPage(Page $page)
+ {
+ $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;
+ }
+
+ 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 $content;
+ }
+