1
0
mirror of https://github.com/til-schneider/slim-wiki.git synced 2025-10-21 19:46:08 +02:00

Automatically updating preview while editing

This commit is contained in:
til-schneider
2015-12-22 10:56:59 +01:00
parent 1c5bd3847f
commit 0b36d30e61
7 changed files with 178 additions and 22 deletions

View File

@@ -1,9 +1,23 @@
<?php
require_once __DIR__ . '/../lib/parsedown/Parsedown.php';
class Main {
private static $singleton;
private $articleBaseDir;
private function __construct() {
$this->articleBaseDir = realpath(__DIR__ . '/../../articles') . '/';
}
public static function get() {
if (is_null(self::$singleton)) {
self::$singleton = new self();
}
return self::$singleton;
}
// Parameters:
// - $baseUrl: E.g. 'http://localhost/slim-wiki/'
// - $basePath: E.g. '/slim-wiki/'
@@ -11,13 +25,60 @@ class Main {
public function dispatch($baseUrl, $basePath, $requestPathArray) {
$config = $this->loadConfig();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$this->handlePost($requestPathArray);
} else {
$this->handleGet($baseUrl, $basePath, $requestPathArray, $config);
}
}
private function handlePost($requestPathArray) {
if (count($requestPathArray) == 2 && $requestPathArray[0] == 'rpc') {
$requestData = json_decode(file_get_contents('php://input'), true);
$objectName = $requestPathArray[1];
$object = null;
if ($objectName == 'render') {
require_once __DIR__ . '/RenderService.php';
$object = RenderService::get();
}
$responseData = array(
'jsonrpc' => '2.0',
'id' => $requestData['id']
);
if ($object == null) {
$responseData['error'] = array( 'code' => -32601, 'message' => "Object not found: $objectName" );
} else {
$methodName = $requestData['method'];
if (! $object->isRpcMethod($methodName)) {
$responseData['error'] = array( 'code' => -32601, 'message' => "Method not found or not public: $objectName.$methodName" );
} else {
try {
$responseData['result'] = call_user_func_array(array($object, $methodName), $requestData['params']);
} catch (Exception $exc) {
$msg = "Calling RPC $objectName.$methodName failed";
error_log($msg . ': ' . $exc->getMessage());
$responseData['error'] = array( 'code' => -32000, 'message' => $msg );
}
}
}
header('Content-Type: application/json');
echo json_encode($responseData);
} else {
header('HTTP/1.0 404 Not Found');
}
}
private function handleGet($baseUrl, $basePath, $requestPathArray, $config) {
$isEditMode = isset($requestPathArray[0]) && $requestPathArray[0] == 'edit';
if ($isEditMode) {
array_shift($requestPathArray);
}
$articleBaseDir = realpath(__DIR__ . '/../../articles') . '/';
$articleFilename = $this->getArticleFilename($articleBaseDir, $requestPathArray);
$articleFilename = $this->getArticleFilename($requestPathArray);
if ($articleFilename == null) {
header('HTTP/1.0 404 Not Found');
header('Content-Type:text/html; charset=utf-8');
@@ -32,18 +93,20 @@ class Main {
$data[$key] = $config[$key];
}
$data['breadcrumbs'] = $this->createBreadcrumbs($articleBaseDir, $requestPathArray, $config['wikiName']);
$data['breadcrumbs'] = $this->createBreadcrumbs($requestPathArray, $config['wikiName']);
$articleMarkdown = file_get_contents($articleFilename);
$data['articleMarkdown'] = $articleMarkdown;
$data['articleHtml'] = Parsedown::instance()->text($articleMarkdown);
require_once __DIR__ . '/RenderService.php';
$data['articleHtml'] = RenderService::get()->renderMarkdown($articleMarkdown);
$this->renderPage($data);
}
}
private function getArticleFilename($articleBaseDir, $requestPathArray) {
$articleFilename = $articleBaseDir . implode('/', $requestPathArray);
private function getArticleFilename($requestPathArray) {
$articleFilename = $this->articleBaseDir . implode('/', $requestPathArray);
// Support `index.md` for directories
if (is_dir($articleFilename)) {
@@ -78,7 +141,7 @@ class Main {
return $config;
}
private function createBreadcrumbs($articleBaseDir, $requestPathArray, $wikiName) {
private function createBreadcrumbs($requestPathArray, $wikiName) {
$pathCount = count($requestPathArray);
$breadcrumbArray = array(array('name' => $wikiName, 'path' => '', 'active' => ($pathCount == 0)));
@@ -88,7 +151,7 @@ class Main {
$currentPath .= ($i == 0 ? '' : '/') . $pathPart;
$isLast = ($i == $pathCount - 1);
if ($isLast || file_exists($articleBaseDir . $currentPath . '/index.md')) {
if ($isLast || file_exists($this->articleBaseDir . $currentPath . '/index.md')) {
// This is the requested file or an directory having an index -> Add it
$breadcrumbArray[] = array(
'name' => str_replace('_', ' ', $pathPart),