1
0
mirror of https://github.com/til-schneider/slim-wiki.git synced 2025-10-22 03:56:05 +02:00

Added breadcrumbs

This commit is contained in:
til-schneider
2015-12-21 10:19:59 +01:00
parent ec191181cb
commit 4942af7072
8 changed files with 116 additions and 18 deletions

View File

@@ -9,17 +9,27 @@ class Main {
// - $basePath: E.g. '/slim-wiki/'
// - $requestPathArray: E.g. array('myfolder', 'mypage')
public function dispatch($baseUrl, $basePath, $requestPathArray) {
$articleBaseDir = realpath(__DIR__ . '/../../articles');
$articleFilename = $articleBaseDir . '/' . implode('/', $requestPathArray);
$articleBaseDir = realpath(__DIR__ . '/../../articles') . '/';
$articleFilename = $articleBaseDir . implode('/', $requestPathArray);
if (is_dir($articleFilename)) {
$articleFilename = rtrim($articleFilename, '/') . '/index.md';
}
if (($articleFilename == realpath($articleFilename)) && file_exists($articleFilename) && is_readable($articleFilename)) {
$wikiName = 'Slim Wiki'; // TODO: Make this configurable
$data = array();
$data['baseUrl'] = $baseUrl;
$data['basePath'] = $basePath;
$data['wikiName'] = $wikiName;
$data['breadcrumbs'] = $this->createBreadcrumbs($articleBaseDir, $requestPathArray, $wikiName);
$articleContent = file_get_contents($articleFilename);
$articleMarkup = Parsedown::instance()->text($articleContent);
include(__DIR__ . '/../layout/page.php');
$data['articleHtml'] = Parsedown::instance()->text($articleContent);
$this->renderPage($data);
} else {
// TODO: Show error page
echo '<p style="color:#990000">File does not exist or is not readable: '.$articleFilename.'</p>';
@@ -27,4 +37,29 @@ class Main {
}
private function createBreadcrumbs($articleBaseDir, $requestPathArray, $wikiName) {
$pathCount = count($requestPathArray);
$breadcrumbArray = array(array('name' => $wikiName, 'path' => '', 'active' => ($pathCount == 0)));
$currentPath = '';
for ($i = 0; $i < $pathCount; $i++) {
$pathPart = $requestPathArray[$i];
$currentPath .= ($i == 0 ? '' : '/') . $pathPart;
$isLast = ($i == $pathCount - 1);
if ($isLast || file_exists($articleBaseDir . $currentPath . '/index.md')) {
// This is the requested file or an directory having an index -> Add it
$breadcrumbArray[] = array('name' => $pathPart, 'path' => urlencode($currentPath), 'active' => $isLast);
}
}
return $breadcrumbArray;
}
private function renderPage($data) {
include(__DIR__ . '/../layout/page.php');
}
}