-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
138 lines (114 loc) · 3.85 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
umask(0002);
require __DIR__ . '/vendor/autoload.php';
const CACHE_FILE = 'cache.html';
const CACHE_JSON = 'cache.json';
const DEFAULT_COLOR = '#35a0d6';
$needRender = false;
$renderedCounter = 0;
if (file_exists(CACHE_JSON)) {
$cache = json_decode(file_get_contents(CACHE_JSON));
} else {
$needRender = true;
}
if (!file_exists(CACHE_FILE)) {
$needRender = true;
}
$users = array_diff(scandir('/home'), array('..', '.'));
use League\CommonMark\Environment\Environment;
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
use League\CommonMark\Extension\FrontMatter\FrontMatterExtension;
use League\CommonMark\Extension\FrontMatter\Output\RenderedContentWithFrontMatter;
use League\CommonMark\Extension\ExternalLink\ExternalLinkExtension;
use League\CommonMark\MarkdownConverter;
// Define your configuration, if needed
$config = [
'html_input' => 'strip',
'external_link' => [
'internal_hosts' => '', // TODO: Don't forget to set this!
'open_in_new_window' => true,
'html_class' => 'external-link',
'nofollow' => '',
'noopener' => 'external',
'noreferrer' => 'external',
],
];
// Configure the Environment with all the CommonMark parsers/renderers
$environment = new Environment($config);
$environment->addExtension(new CommonMarkCoreExtension());
$environment->addExtension(new FrontMatterExtension());
$environment->addExtension(new ExternalLinkExtension());
// Instantiate the converter engine and start converting some Markdown!
$converter = new MarkdownConverter($environment);
function presentationDir(string $user)
{
return "/home/$user/PRESENTATION.md";
}
// Check if cache file is outdated
if (!$needRender) {
$presentationCounter = 0;
$renderTimestamp = filemtime(CACHE_FILE);
foreach ($users as $user) {
$presentation = presentationDir($user);
if (file_exists($presentation) && !empty($presentation)) {
$timestamp = filemtime($presentation);
if (is_int($timestamp)) {
if ($timestamp >= $renderTimestamp) {
$needRender = true;
break;
}
}
$presentationCounter ++;
}
}
if (!$needRender) {
$needRender = $presentationCounter !== $cache->renderedCounter;
}
}
// Render the user's div. Used in template.php
function renderUsers()
{
global $converter;
global $users;
global $renderedCounter;
shuffle($users);
foreach ($users as $user) {
$presentation = presentationDir($user);
if (file_exists($presentation) && !empty($presentation)) {
$md = file_get_contents($presentation);
$name = $user;
$color = DEFAULT_COLOR;
try {
$result = $converter->convert($md);
$content = $result->getContent();
if ($result instanceof RenderedContentWithFrontMatter) {
$frontMatter = $result->getFrontMatter();
if (!empty($frontMatter['name'])) {
$name = $frontMatter['name'];
}
if (!empty($frontMatter['color'])) {
$color = $frontMatter['color'];
}
}
} catch (RuntimeException $e) {
$message = $e->getMessage();
$content = "render error : $message";
}
$userId = $user;
include 'templateUser.php';
$renderedCounter ++;
}
}
}
if ($needRender) {
ob_start();
include 'template.php';
$render = ob_get_contents();
ob_end_clean();
if (is_string($render)) {
file_put_contents(CACHE_FILE, $render);
file_put_contents(CACHE_JSON, json_encode(['renderedCounter' => $renderedCounter]));
}
}
include 'cache.html';
?>