1
0
mirror of https://github.com/til-schneider/slim-wiki.git synced 2025-08-06 08:37:31 +02:00

Added basic markdown page rendering

This commit is contained in:
til-schneider
2015-12-19 19:59:20 +01:00
parent 4d04fe0b6c
commit dbc509c004
6 changed files with 92 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/.idea/

6
src/.htaccess Normal file
View File

@@ -0,0 +1,6 @@
RewriteEngine On
RewriteCond %{THE_REQUEST} !^GET\ /.*?client/(css|js|img)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]

5
src/articles/index.md Normal file
View File

@@ -0,0 +1,5 @@
Example Wiki
============
Place the content of you *slim wiki* into this directory. Write it using
[GitHub flavored](https://help.github.com/articles/github-flavored-markdown) [Markdown](https://daringfireball.net/projects/markdown/).

40
src/index.php Normal file
View File

@@ -0,0 +1,40 @@
<?php
ini_set('display_errors', 1);
// Split URI example: 'http://localhost/slim-wiki/myfolder/mypage?action=bla'
// into $appPath example: '/slim-wiki'
// and $requestPathArray example: array('myfolder', 'mypage')
$uriPathArray = explode("/", parse_url($_SERVER['REQUEST_URI'])['path']);
$scriptPathArray = explode("/", dirname($_SERVER['SCRIPT_NAME']));
$appPathArray = array();
$requestPathArray = array();
$isAppPath = true;
foreach ($uriPathArray as $level => $uriDir) {
$scriptDir = isset($scriptPathArray[$level]) ? $scriptPathArray[$level] : null;
if ($isAppPath && $scriptDir != $uriDir) {
// The URI path differs from the script path here -> We arrived at the level where the app is installed
$isAppPath = false;
}
if ($isAppPath) {
$appPathArray[] = $uriDir;
} else {
$requestPathArray[] = $uriDir;
}
}
$appPath = rtrim(implode('/', $appPathArray), '/');
$https = false;
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
$https = true;
}
$baseUrl = 'http' . ($https ? 's' : '') . '://' . $_SERVER['HTTP_HOST'] . $appPath;
unset($uriPathArray, $scriptPathArray, $appPathArray, $isAppPath, $https);
require_once __DIR__ . '/server/logic/main.php';
(new Main())->dispatch($baseUrl, $appPath, $requestPathArray);

View File

@@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=0">
<title>Slim Wiki</title>
</head>
<body>
<article><?php echo $articleMarkup ?></article>
</body>
</html>

27
src/server/logic/Main.php Normal file
View File

@@ -0,0 +1,27 @@
<?php
require_once __DIR__ . '/../lib/parsedown/Parsedown.php';
class Main {
// Parameters:
// - $baseUrl: E.g. 'http://localhost/slim-wiki/'
// - $appPath: E.g. '/slim-wiki'
// - $requestPathArray: E.g. array('myfolder', 'mypage')
public function dispatch($baseUrl, $appPath, $requestPathArray) {
$articleBaseDir = realpath(__DIR__ . '/../../articles');
$articleFilename = $articleBaseDir . '/' . implode('/', $requestPathArray);
if (($articleFilename == realpath($articleFilename)) && file_exists($articleFilename) && is_readable($articleFilename)) {
$articleContent = file_get_contents($articleFilename);
$articleMarkup = Parsedown::instance()->text($articleContent);
include(__DIR__ . '/../layout/page.php');
} else {
// TODO: Show error page
echo '<p style="color:#990000">File does not exist or is not readable: '.$articleFilename.'</p>';
}
}
}