/
generate.php
59 lines (49 loc) · 1.88 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
<?php declare(strict_types=1);
namespace ajf\blog;
require_once __DIR__ . '/vendor/autoload.php';
use League\CommonMark\CommonMarkConverter;
$posts = [];
$files = glob('posts/*.post.md');
rsort($files);
$posts = array_map(function ($file) {
$fields = [];
$fields['url'] = basename($file, ".post.md");
$fields['content'] = file_get_contents($file);
if (($pos = strpos($fields['content'], "\n")) !== FALSE) {
$fields['title'] = substr($fields['content'], 0, $pos);
} else {
$fields['title'] = $fields['url'];
}
// strip markdown title formatting, newlines
$fields['title'] = rtrim(ltrim($fields['title'], '#='), "=\n\r");
if (preg_match('/^((\d\d\d\d)-(\d\d)-\d\d)-/', $fields['url'], $matches)) {
$fields['date'] = $matches[1]; // whole YYYY-MM-DD date
$fields['year'] = $matches[2]; // YYYY
$month = (int)$matches[3]; // MM
$fields['season'] = [
3 => '🌱', 4 => '🌱', 5 => '🌱',
6 => '☀️', 7 => '☀️', 8 => '☀️',
9 => '🍁', 10 => '🍁', 11 => '🍁',
12 => '🎄', 1 => '❄️', 2 => '❄️',
][$month];
}
return $fields;
}, $files);
if (file_exists('posts/blog-description.md')) {
$blogDescription = file_get_contents("posts/blog-description.md");
$converter = new CommonMarkConverter();
$blogDescription = $converter->convertToHtml($blogDescription);
} else {
$blogDescription = NULL;
}
ob_start();
require_once "templates/home.php";
file_put_contents("out/index.html", ob_get_clean());
foreach ($posts as $post) {
$postTitle = $post["title"];
$converter = new CommonMarkConverter();
$postContent = $converter->convertToHtml($post["content"]);
ob_start();
require "templates/post.php";
file_put_contents("out/$post[url].html", ob_get_clean());
}