aboutsummaryrefslogtreecommitdiffstats
path: root/generate.php
blob: f1c9c40d59a8069253c5b8018f92810dfb549f6f (plain)
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
<?php

require_once 'vendor/autoload.php';

use Michelf\MarkdownExtra;
use fkooman\Tpl\Twig\TwigTemplateManager;

$postDir = sprintf('%s/posts', __DIR__);
$outputDir = sprintf('%s/output', __DIR__);
$templateDir = sprintf('%s/views', __DIR__);

$blogTitle = "Fran├žois' Weblog";
$blogDescription = 'Just another boring weblog.';
$blogUrl = 'https://www.tuxed.net/fkooman/blog/';
$blogAuthor = 'Fran├žois Kooman';
$blogAuthorTwitter = 'fkooman';
$blogAuthorMail = 'fkooman@tuxed.net';

$blogPosts = [];

foreach (glob(sprintf('%s/*.md', $postDir)) as $postFile) {
    $postInfo = [];

    // obtain postInfo
    $f = fopen($postFile, 'r');
    $line = fgets($f);
    if (0 !== strpos($line, '---')) {
        throw new Exception('invalid file!');
    }
    $line = fgets($f);
    do {
        $xx = explode(':', $line);
        $postInfo[trim($xx[0])] = trim($xx[1]);
        $line = fgets($f);
    } while (0 !== strpos($line, '---'));

    // read rest of the post         
    $buffer = '';
    while (!feof($f)) {
        $buffer .= fgets($f);
    }

    fclose($f);
    $postOutputFile = basename($postFile, '.md').'.html';

    $parser = new MarkdownExtra();

    $blogPost = [
        'htmlContent' => $parser->transform($buffer),
        'published' => $postInfo['published'],
        'title' => $postInfo['title'],
        'modified' => isset($postInfo['modified']) ? $postInfo['modified'] : null,
        'fileName' => $postOutputFile,
    ];
    $postsList[] = $blogPost;
}

usort($postsList, function ($a, $b) {
    return strtotime($a['published']) < strtotime($b['published']);
});

$tpl = new TwigTemplateManager([$templateDir]);
$indexPage = $tpl->render(
    'index',
    [
        'postsList' => $postsList,
        'pageTitle' => 'Index',
        'blogTitle' => $blogTitle,
        'blogDescription' => $blogDescription,
        'blogAuthor' => $blogAuthor,
        'blogAuthorMail' => $blogAuthorMail,
        'blogAuthorTwitter' => $blogAuthorTwitter,
    ]
);

foreach ($postsList as $post) {
    $postPage = $tpl->render(
        'post',
        [
            'blogTitle' => $blogTitle,
            'pageTitle' => $post['title'],
            'post' => $post,
            'blogAuthor' => $blogAuthor,
            'blogAuthorMail' => $blogAuthorMail,
            'blogAuthorTwitter' => $blogAuthorTwitter,
        ]
    );
    file_put_contents($outputDir.'/'.$post['fileName'], $postPage);
}

file_put_contents($outputDir.'/index.html', $indexPage);

// copy css
@mkdir($outputDir.'/css');
copy(__DIR__.'/screen.css', $outputDir.'/css/screen.css');