aboutsummaryrefslogtreecommitdiffstats
path: root/generate.php
diff options
context:
space:
mode:
Diffstat (limited to 'generate.php')
-rw-r--r--generate.php95
1 files changed, 95 insertions, 0 deletions
diff --git a/generate.php b/generate.php
new file mode 100644
index 0000000..f1c9c40
--- /dev/null
+++ b/generate.php
@@ -0,0 +1,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');