-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.php
100 lines (77 loc) · 2.59 KB
/
generate.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
<?php
require 'vendor/autoload.php';
use Philo\Blade\Blade;
class SSG
{
/**
* Run the static site generator
*
* @return void
*/
private $data;
private $blade;
private $content;
private $outputDir;
private $outputPath;
private $contentDir;
private $contentFiles;
private function set_contentDir($content_path){
$contentDir = __DIR__ . $content_path;
return $contentDir;
}
private function get_content_files(){
// get all the content files
return $contentFiles = array_diff(scandir($this->contentDir), array('.', '..'));
}
private function get_json_data_from_content_file($file){
// ./content/index.json
return json_decode(file_get_contents($this->contentDir . '/' . $file));
}
private function creating_new_blade($view_path, $cache_path){
// pass this data to a template engine
return new Blade(__DIR__ . $view_path, __DIR__ . $cache_path);
}
private function rendering_new_blade($blade, $template, $data){
// get html back from the template
return $blade->view()
->make($template, get_object_vars($data))
->render();
}
private function handle_output_dir($path){
$this->data->slug == '/' ? $this->data->slug = '' : null;
$outputDir = __DIR__ . $path . $this->data->slug;
if(!is_dir($outputDir)){
mkdir($outputDir, 0755, true);
}
return $outputDir;
}
private function output($path){
// write html to a file in the desired folder structure
$this->outputPath = $this->outputDir . $path;
file_put_contents($this->outputPath, $this->content);
}
public function init()
{
$this->contentDir = $this->set_contentDir('/content');
$this->contentFiles = $this->get_content_files();
// foreach content file, pass all information to template
foreach($this->contentFiles as $file)
{
$this->data = $this->get_json_data_from_content_file($file);
$this->blade = $this->creating_new_blade('/resources/views', '/cache');
$this->content = $this->rendering_new_blade($this->blade, $this->data->view, $this->data);
/**
* Now that we have html, save it to the correct path
* the landing page should be index.html
* anyohter page should be saved as page-name/index.html,
* here page-name has to ba a directory
*/
$this->outputDir = $this->handle_output_dir('/OUTPUT');
$this->output('/index.html');
echo "Page is built in the following directory " . $this->outputPath . PHP_EOL;
}
}
}
$ssg = new SSG;
$ssg->init();
// $ssg->get_dir();