-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
239 lines (200 loc) · 7.36 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
require 'include/Parsedown.php';
$ini = parse_ini_file("config.ini");
//
$title = $ini['title'];
$description = $ini['description'];
$keywords = $ini['keywords'];
$favicon = $ini['favicon'];
$defaultTheme = $ini['defaultTheme'];
$modules = explode(',', $ini['modules']);
// Tricks //
// State
$searchQuery = isset($_GET['q']) ? $_GET['q'] : '';
$page = isset($_GET['p']) ? $_GET['p'] : 'index';
if (!$searchQuery) {
if (is_dir('content/' . $page)) // if this is true, it's a category
$page = $page . '/index';
$md_path = 'content/' . $page . '.md';
$page_exists = file_exists($md_path);
}
else
$page = null;
// variables that modules can edit to add additional content to each respective tag
$additionalHead = '';
$additionalContent = '';
$additionalBody = '';
// load the selected modules' *.php files
foreach ($modules as $module) {
// include the .php file in modules/$module/
include_once 'modules/' . $module . '/main.php';
}
function create_page_contents()
{
$Parsedown = new Parsedown();
if ($GLOBALS['searchQuery']) {
$startTime = microtime(true);
echo "<h1>Search results for \"" . $GLOBALS['searchQuery'] . "\"</h1>";
// Get all the files and subdirectories in the content directory
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator('content/')
);
$matchingTitles = [];
// Iterate through each file in the content directory
foreach ($files as $file) {
if ($file->isFile()) {
// extract the file name without the extension
$fileName = pathinfo($file->getFilename(), PATHINFO_FILENAME);
// levenshtein distance between the search query and the filename
$distance = levenshtein(strtolower($GLOBALS['searchQuery']), strtolower($fileName));
// similarity match
$threshold = 3;
if ($distance !== false && $distance <= $threshold) {
// Store the file name and its distance for later use
$matchingTitles[$fileName] = $distance;
}
}
}
// Sort the matching titles based on their Levenshtein distance (optional step)
asort($matchingTitles);
$numResults = count($matchingTitles);
$endTime = microtime(true);
$timeTaken = number_format(($endTime - $startTime),5);
echo "<p>About $numResults results ($timeTaken seconds)</p>";
echo "<h2>Matching titles</h2>";
echo "<ol>";
$i = 1;
foreach ($matchingTitles as $title => $distance) {
$urlTitle = strtolower(str_replace(' ', '-', $title));
echo "<li><a href=\"$urlTitle\" class=\"searchlink\">$title</a>";
// Read the contents of the Markdown file
$md_path = 'content/' . $urlTitle . '.md';
$md_contents = file_get_contents($md_path);
// number of characters to show in the sample
$numCharacters = 200;
$sample = strip_tags($Parsedown->text($md_contents));
$sample = substr($sample, 0, $numCharacters);
// If the content is longer than the defined number of characters, find the last space and truncate the text
if (strlen($md_contents) > $numCharacters) {
$lastSpacePos = strrpos($sample, ' ');
if ($lastSpacePos !== false) {
$sample = substr($sample, 0, $lastSpacePos);
}
$sample .= '...';
}
// file data
$fileSize = formatFileSize(filesize($md_path));
echo "<p class=\"search-sample\">$sample</p>";
echo "<p class=\"search-result-data\">$fileSize</p>";
echo "</li>";
}
echo "</ol>";
}
else
if (!$GLOBALS['page_exists']) {
?>
<h1>Error 404</h1>
<p>The page "
<?php echo $GLOBALS['page']; ?>" does not exist.
</p>
<p>Return to <a href="/">Home</a></p>
<?php
}
else
echo $Parsedown->text(file_get_contents($GLOBALS['md_path']));
}
function formatFileSize($bytes) {
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$base = 1024;
$index = floor(log($bytes, $base));
$formattedSize = round($bytes / pow($base, $index), 2) . ' ' . $units[$index];
return $formattedSize;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>
<?php echo $title; ?>
</title>
<link rel="icon" type="image/x-icon" href="<?php echo $favicon; ?>">
<link rel="stylesheet" href="styles/main.css">
<link rel="stylesheet" href="styles/themes/<?php echo $defaultTheme; ?>">
<script src="js/cms.js" defer></script>
<meta name="keywords" content="<?php echo $description; ?>">
<?php
echo $additionalHead;
?>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
hljs.highlightAll();
</script>
</head>
<body>
<!-- Left column -->
<div>
<div class="navigation">
<div class="blank">
</div>
</div>
<table id="toc" style="<?php echo $GLOBALS['searchQuery'] ? "display:none" : "" ?>">
<tbody>
<tr>
<td id="toc-content">
<h2>Contents</h2>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Middle column -->
<div>
<div class="navigation">
<div class="left">
<p class="breadcrumbs">
<?php
// Breadcrumbs
$page = isset($_GET['p']) ? $_GET['p'] : '';
$breadcrumbs = explode('/', $page);
if (count($breadcrumbs) === 1 && empty($breadcrumbs[0]))
echo '<a href="/">' . $title . '</a>'; // Homepage
else {
$breadcrumbLinks = [];
$currentPath = '';
foreach ($breadcrumbs as $breadcrumb) {
if (!empty($breadcrumb)) {
$currentPath .= '/' . $breadcrumb;
$breadcrumbLinks[] = '<a href="' . ($currentPath === '/' ? '/' : '?p=' . ltrim($currentPath, '/')) . '">' . ucfirst($breadcrumb) . '</a>';
}
}
echo '<a href="/">' . $title . '</a> / ' . implode(' / ', $breadcrumbLinks);
}
?>
</p>
</div>
<form method="GET" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div class="right">
<input type="search" name="q" placeholder="Search <?php echo $title ?> "><input class="search" type="submit" value="Search">
</div>
</form>
</div>
<div class="content">
<?php
create_page_contents();
echo $additionalContent;
?>
</div>
<footer>
<p>
<?php
if (isset($GLOBALS['page_exists']))
echo "Page last modified on " . date('F d Y H:i:s', filemtime($md_path))
?>
</p>
</footer>
</div>
<?php
echo $additionalBody;
?>
</body>
</html>