-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerateDocs
More file actions
78 lines (59 loc) · 2.17 KB
/
generateDocs
File metadata and controls
78 lines (59 loc) · 2.17 KB
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
#!/usr/bin/env php
<?php
$sourceDirectory = './docsUncompiled';
$destinationDirectory = './docs';
$excludes = [
'_partials',
'images',
];
removeOldFiles($destinationDirectory, $excludes);
compileMdFiles($sourceDirectory, $destinationDirectory, $excludes);
function removeOldFiles(string $destinationDirectory, array $excludes): void
{
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($destinationDirectory));
foreach ($iterator as $file) {
if ($file->isDir()) continue;
foreach ($excludes as $exclude) {
if (str_starts_with($file->getPathname(), $destinationDirectory . '/' . $exclude) !== false) {
continue 2;
}
}
unlink($file->getPathname());
}
}
function compileMdFiles(string $sourceDirectory, string $destinationDirectory, array $excludes = []): void
{
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($sourceDirectory));
foreach ($iterator as $file) {
if ($file->isDir()) continue;
foreach ($excludes as $exclude) {
if (str_starts_with($file->getPathname(), $sourceDirectory . '/' . $exclude) !== false) {
continue 2;
}
}
processFile($file->getPathname(), $sourceDirectory, $destinationDirectory);
}
}
function processFile(string $path, string $sourceDirectory, string $destinationDirectory): void {
$relativePath = str_replace($sourceDirectory, '', $path);
$destinationPath = $destinationDirectory . $relativePath;
$content = file_get_contents($path);
$content = preg_replace_callback(
'/!\((.*?)\.md\)/',
function ($matches) use ($sourceDirectory) {
$partialPath = $sourceDirectory . '/' . $matches[1] . '.md';
if (file_exists($partialPath)) {
return file_get_contents($partialPath);
} else {
return $matches[0];
}
},
$content
);
$destinationDir = dirname($destinationPath);
if (!is_dir($destinationDir)) {
mkdir($destinationDir);
}
file_put_contents($destinationPath, $content);
echo "Compiled {$path}\n";
}