forked from nacmartin/silexslides
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
69 lines (58 loc) · 2.03 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
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once __DIR__.'/silex.phar';
require_once __DIR__.'/markdown.php';
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
$app = new Silex\Application();
$app['slides_dir'] = __DIR__.'/slides/';
$app->register(new Silex\Extension\HttpCacheExtension(), array(
'http_cache.cache_dir' => __DIR__.'/cache/',
));
$app->register(new Silex\Extension\TwigExtension(), array(
'twig.path' => __DIR__.'/views',
'twig.class_path' => __DIR__.'/vendor/twig/lib',
));
$app->get('/', function () use ($app){
$slides = array();
$files = array();
if ($handle = opendir($app['slides_dir'])) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
array_push($files, $file);;
}
}
closedir($handle);
}
sort($files, SORT_NUMERIC);
foreach($files as $file){
$content = file_get_contents($app['slides_dir'].$file);
//Possible metadata in first line
$matches = array();
preg_match('/%(.*)%/', $content, $matches);
$classslide = null;
if($matches){
$classslide = $matches[1];
$content = preg_replace('/%(.*)%/', '', $content);
}
$slide['slide'] = Markdown($content);
$slide['class'] = $classslide;
array_push($slides, $slide);
}
$resp = $app['twig']->render('slide.twig', array(
'slides' => $slides,
));
return new Response($resp, 200, array(
'Cache-Control' => 's-maxage=0',
));
});
$app->error(function (\Exception $e) {
if ($e instanceof NotFoundHttpException) {
return new Response('La página que buscas no está aquí.', 404);
}
$code = ($e instanceof HttpException) ? $e->getStatusCode() : 500;
return new Response('Algo ha fallado en nuestra sala de máquinas.', $code);
});
$app['http_cache']->run();