-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-static.php
More file actions
96 lines (79 loc) · 2.58 KB
/
Copy pathbuild-static.php
File metadata and controls
96 lines (79 loc) · 2.58 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
declare(strict_types=1);
$root = __DIR__;
$out = $root . DIRECTORY_SEPARATOR . 'public';
function rrmdir(string $dir): void
{
if (!is_dir($dir)) {
return;
}
$items = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($items as $item) {
$item->isDir() ? rmdir($item->getPathname()) : unlink($item->getPathname());
}
rmdir($dir);
}
function copy_path(string $source, string $dest): void
{
if (!file_exists($source)) {
return;
}
if (is_file($source)) {
$parent = dirname($dest);
if (!is_dir($parent)) {
mkdir($parent, 0777, true);
}
copy($source, $dest);
return;
}
$items = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($items as $item) {
$target = $dest . DIRECTORY_SEPARATOR . $items->getSubPathName();
if ($item->isDir()) {
if (!is_dir($target)) {
mkdir($target, 0777, true);
}
} else {
$parent = dirname($target);
if (!is_dir($parent)) {
mkdir($parent, 0777, true);
}
copy($item->getPathname(), $target);
}
}
}
rrmdir($out);
mkdir($out, 0777, true);
ob_start();
require $root . DIRECTORY_SEPARATOR . 'index.php';
$html = ob_get_clean();
$html = preg_replace_callback(
'/href="preview\.php\?file=([^"]+)"/',
static function (array $matches): string {
$file = rawurldecode($matches[1]);
return 'href="' . htmlspecialchars($file, ENT_QUOTES) . '"';
},
$html
);
$html = preg_replace_callback(
"/window\.open\('preview\.php\?file=([^']+)', '_blank'\)/",
static function (array $matches): string {
$file = rawurldecode($matches[1]);
return "window.open('" . addslashes($file) . "', '_blank')";
},
$html
);
file_put_contents($out . DIRECTORY_SEPARATOR . 'index.html', $html);
file_put_contents($out . DIRECTORY_SEPARATOR . '.nojekyll', '');
copy_path($root . DIRECTORY_SEPARATOR . 'assets', $out . DIRECTORY_SEPARATOR . 'assets');
copy_path($root . DIRECTORY_SEPARATOR . 'journal', $out . DIRECTORY_SEPARATOR . 'journal');
foreach (['README.md', 'improved_routine.md', 'FILE_STRUCTURE.md', 'readme.txt'] as $file) {
copy_path($root . DIRECTORY_SEPARATOR . $file, $out . DIRECTORY_SEPARATOR . $file);
}
echo "Static site written to public/\n";