aboutsummaryrefslogtreecommitdiffstats
path: root/generate.php
blob: 91807900f505e5b68b2d221fb6bc3eeed430950e (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php

require_once 'vendor/autoload.php';

use Michelf\MarkdownExtra;
use Suin\RSSWriter\Channel;
use Suin\RSSWriter\Feed;
use Suin\RSSWriter\Item;

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

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

$blogAuthorTwitter = 'fkooman';
$blogAuthorGitHub = 'fkooman';
$blogAuthorMail = 'fkooman@tuxed.net';
$blogAuthorMailFingerprint = '6237 BAF1 418A 907D AA98  EAA7 9C5E DD64 5A57 1EB2';

$loader = new Twig_Loader_Filesystem($templateDir);
$twig = new Twig_Environment($loader, ['strict_variables' => true]);

$dateTime = new DateTime();

$feed = new Feed();
$channel = new Channel();
$channel
    ->title($blogTitle)
    ->description($blogDescription)
    ->url($blogUrl)
    ->language('en-US')
    ->pubDate($dateTime->getTimestamp())
    ->lastBuildDate($dateTime->getTimestamp())
    ->appendTo($feed);

$postsList = [];
$pagesList = [];

@mkdir($outputDir, 0755, true);
@mkdir($blogOutputDir, 0755, true);

foreach (glob(sprintf('%s/*.md', $pageDir)) as $pageFile) {
    $pageInfo = [];

    $f = fopen($pageFile, 'r');
    $line = fgets($f);
    if (0 !== strpos($line, '---')) {
        throw new Exception(sprintf('invalid file "%s"!', $pageFile));
    }
    $line = fgets($f);
    do {
        $xx = explode(':', $line, 2);
        $pageInfo[trim($xx[0])] = trim($xx[1]);
        $line = fgets($f);
    } while (0 !== strpos($line, '---'));

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

    fclose($f);
    $pageOutputFile = basename($pageFile, '.md').'.html';

    $parser = new MarkdownExtra();
    $page = [
        'htmlContent' => $parser->transform($buffer),
        'publish' => isset($pageInfo['publish']) ? 'no' !== $pageInfo['publish'] : true,
        'title' => $pageInfo['title'],
        'fileName' => $pageOutputFile,
    ];
    $pagesList[] = $page;
}

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(sprintf('invalid file! "%s"', $postFile));
    }
    $line = fgets($f);
    do {
        $xx = explode(':', $line, 2);
        $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),
        'description' => isset($postInfo['description']) ? $postInfo['description'] : $postInfo['title'],
        'published' => $postInfo['published'],
        'publish' => isset($postInfo['publish']) ? 'no' !== $postInfo['publish'] : true,
        'title' => $postInfo['title'],
        'modified' => isset($postInfo['modified']) ? $postInfo['modified'] : null,
        'fileName' => $postOutputFile,
    ];
    $postsList[] = $blogPost;

    if ($blogPost['publish']) {
        $item = new Item();
        $item
            ->title($blogPost['title'])
            ->description($blogPost['description'])
            ->contentEncoded($blogPost['htmlContent'])
            ->url(sprintf('%s%s', $blogUrl, $blogPost['fileName']))
            ->author($blogAuthorMail)
            ->pubDate(strtotime($blogPost['published']))
            ->guid(sprintf('%s%s', $blogUrl, $blogPost['fileName']))
            ->preferCdata(true) // By this, title and description become CDATA wrapped HTML.
            ->appendTo($channel);
    }
}

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

$siteIndexPage = $twig->render(
    'index.twig',
    [
        'toRoot' => '',
        'pagesList' => $pagesList,
        'activePage' => 'index.html',
        'postsList' => $postsList,
        'pageTitle' => 'Blog',
        'blogTitle' => $blogTitle,
        'blogDescription' => $blogDescription,
        'blogAuthor' => $blogAuthor,
    ]
);

$blogIndexPage = $twig->render(
    'index.twig',
    [
        'toRoot' => '../',
        'pagesList' => $pagesList,
        'activePage' => 'index.html',
        'postsList' => $postsList,
        'pageTitle' => 'Blog',
        'blogTitle' => $blogTitle,
        'blogDescription' => $blogDescription,
        'blogAuthor' => $blogAuthor,
    ]
);

foreach ($postsList as $post) {
    if ($post['publish']) {
        $postPage = $twig->render(
            'post.twig',
            [
                'toRoot' => '../',
                'pagesList' => $pagesList,
                'activePage' => 'index.html',
                'blogTitle' => $blogTitle,
                'pageTitle' => $post['title'],
                'post' => $post,
                'blogDescription' => $blogDescription,
                'blogAuthor' => $blogAuthor,
            ]
        );
        file_put_contents($blogOutputDir.'/'.$post['fileName'], $postPage);
    }
}

foreach ($pagesList as $page) {
    if ($post['publish']) {
        $pagePage = $twig->render(
            'page.twig',
            [
                'toRoot' => '',
                'activePage' => $page['fileName'],
                'pagesList' => $pagesList,
                'blogTitle' => $blogTitle,
                'pageTitle' => $page['title'],
                'pageContent' => $page,
                'blogDescription' => $blogDescription,
                'blogAuthor' => $blogAuthor,
            ]
        );
        file_put_contents($outputDir.'/'.$page['fileName'], $pagePage);
    }
}

file_put_contents($outputDir.'/index.html', $siteIndexPage);
file_put_contents($blogOutputDir.'/index.html', $blogIndexPage);

// copy img
@mkdir($outputDir.'/img');
foreach (glob(__DIR__.'/img/*') as $imgFile) {
    copy($imgFile, $outputDir.'/img/'.basename($imgFile));
}

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

file_put_contents($outputDir.'/feed.xml', $feed);