Skip to content

Commit f46d346

Browse files
committed
Generator to create a static website
1 parent 7231989 commit f46d346

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

staticgen.php

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
define('FILES_DIR', "files/");
3+
define('OUT_FOLDER', "out-website/");
4+
5+
env_var('SITE_NAME');
6+
env_var('SAVE_ENABLED');
7+
env_var('REQUIRE_AUTH');
8+
env_var('USER');
9+
env_var('PASS');
10+
11+
function env_var($name) {
12+
$value = getenv($name);
13+
if($value)
14+
$_ENV[$name] = $value;
15+
}
16+
17+
# Create output folder
18+
if(!is_dir(OUT_FOLDER))
19+
mkdir(OUT_FOLDER);
20+
if(!is_dir(OUT_FOLDER.FILES_DIR))
21+
mkdir(OUT_FOLDER.FILES_DIR);
22+
23+
$files = scandir(FILES_DIR);
24+
foreach ($files as $file) {
25+
if($file === "..")
26+
continue;
27+
28+
if(!is_dir(FILES_DIR . $file))
29+
continue;
30+
31+
if($file === ".") # Homepage
32+
unset($_REQUEST['file']);
33+
else {
34+
$_REQUEST['file'] = $file;
35+
# Create folder for page
36+
if(!is_dir(OUT_FOLDER . $file))
37+
mkdir(OUT_FOLDER . $file);
38+
}
39+
40+
$outfile = OUT_FOLDER . ($file === "." ? "" : $file . "/") ."index.html";
41+
42+
ob_start();
43+
include('index.php');
44+
$output = ob_get_clean();
45+
file_put_contents($outfile, $output);
46+
47+
# Copy files
48+
if($file !== ".") {
49+
if(!is_dir(OUT_FOLDER.FILES_DIR.$file))
50+
mkdir(OUT_FOLDER.FILES_DIR.$file);
51+
foreach($list_files as $copyfile) {
52+
echo(FILES_DIR.$file."/".$copyfile." -> ".OUT_FOLDER.FILES_DIR.$file."/".$copyfile."\n");
53+
copy(FILES_DIR.$file."/".$copyfile, OUT_FOLDER.FILES_DIR.$file."/".$copyfile);
54+
}
55+
}
56+
}
57+
58+
?>

0 commit comments

Comments
 (0)