This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
88 lines (75 loc) · 1.86 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* KickAss - DBS-Projekt SoSe 2014 FU-Berlin
*
* Routing für die Weboberfläche der Anwendung
*
* @author David Bohn
* @author Luca Keidel
*
*/
/**
* Autoloader
*/
require 'vendor/autoload.php';
/**
* @var \Slim\Slim Die Slim Application
*/
$app = new \Slim\Slim(array(
'view' => new \Slim\Views\Twig(),
'templates.path' => 'views'
));
$app->setName('KickAss');
$app->view()->parserExtensions = array(
new \Slim\Views\TwigExtension(),
);
$app->get('/', function () use ($app) {
$app->render('index.twig');
});
$app->get('/documentation(/:page)', function($page = null) use ($app) {
$response = $app->response();
if ($page === null) {
$files = glob('docs/*.md');
$files = array_map(function($el) {
$base = basename($el);
$ref = (substr($base, 0, strpos($base, '.md')));
$title = titleForFile($el, $ref);
return array('ref' => $ref, 'title' => $title);
}, $files);
$resp = array('pages' => $files);
} else {
$file = basename($page);
$path = 'docs/' . $file . '.md';
if (@file_exists($path)) {
$markdown = file_get_contents($path);
$parsed = \Michelf\MarkdownExtra::defaultTransform($markdown);
$title = titleForFile($path, $file);
$resp = array('docs' => $parsed, 'title' => $title, 'ref' => $file);
} else {
$response->setStatus(404);
$resp = array('error' => 'Page not found!');
}
}
$response['Content-Type'] = 'application/json';
$response->body(json_encode($resp, JSON_PRETTY_PRINT));
});
/**
* Liest den Titel aus einer Datei im Markdown Format aus.
*
* @param string $file Dateipfad zur Markdown-Datei
* @param string $default Standardwert für den Titel
*
* @return string Titel für die angegebene Datei
*/
function titleForFile($file, $default)
{
$fp = fopen($file, 'r');
$title = $default;
if ($fp) {
$title = ltrim(fgets($fp), '#');
fclose($fp);
}
return $title;
}
$app->run();
?>