From 8e3ebb113d8b0b0ab34eebb97c3a8a3ac38a92f8 Mon Sep 17 00:00:00 2001 From: Vincent Douillet Date: Fri, 27 May 2022 17:41:24 +0200 Subject: generate article list automatically --- makesite.pl | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'makesite.pl') diff --git a/makesite.pl b/makesite.pl index 6980da8..2b6c7e1 100755 --- a/makesite.pl +++ b/makesite.pl @@ -7,7 +7,10 @@ use File::Slurp; use Text::Markdown "markdown"; sub page { - my ($pageFile, $pageTitle, $pageType, $pageContent) = @_; + my ($pageFile, $pageTitle, $pageContent, $pageType, $id) = @_; + unless(defined $pageType) { + $pageType = "body"; # default page type + } open my $outHandle, ">>", "output/$pageFile"; # Write header and insert page title open my $inHandle, "<", "template/header.html"; @@ -17,7 +20,12 @@ sub page { } close $inHandle; # Write content and footer - print $outHandle "<$pageType>"; + if(defined $id) { + print $outHandle "<$pageType id=\"$id\">"; + } + else { + print $outHandle "<$pageType>"; + } print $outHandle $pageContent; print $outHandle ""; cat "template/footer.html", $outHandle; @@ -27,33 +35,45 @@ sub page { `gzip -k output/$pageFile`; } -# Build index -print "Building index\n"; -my $pageContent = read_file "template/index.html"; -page "index.html", "blog.vdouillet.fr", "body", $pageContent; - # Build about print "Building about\n"; -$pageContent = read_file "static/about.md"; -$pageContent = markdown $pageContent; -page "about.html", "About", "body", $pageContent; +my $aboutContent = read_file "static/about.md"; +$aboutContent = markdown $aboutContent; +page "about.html", "About", $aboutContent; # Build articles +my @articleList; while(<*.md>) { - print "Processing $_\n"; + my $articleFile = $_; + print "Processing $articleFile\n"; # Extract article title from the title in MD article - open my $articleHandle, "<", $_; + open my $articleHandle, "<", $articleFile; my $headLine = <$articleHandle>; - $headLine = substr $headLine, 2; + $headLine = substr $headLine, 2, -1; close $articleHandle; # Extract output file name: MD file name without the date - my $pageFile = substr $_, 9, -3; + my $pageFile = substr $articleFile, 9, -3; $pageFile .= ".html"; # Build article - my $articleContent = read_file $_; + my $articleContent = read_file $articleFile; $articleContent = markdown $articleContent; - page $pageFile, $headLine, "article", $articleContent; + page $pageFile, $headLine, $articleContent, "article"; + + # Add article to index + my $year = substr $articleFile, 0, 4; + my $month = substr $articleFile, 4, 2; + my $day = substr $articleFile, 6, 2; + push @articleList, "* [$month-$day-$year $headLine]($pageFile \"$headLine\")\n"; +} + +# Build index with article list in reverse (most recent first) +print "Building index\n"; +my $indexContent = read_file "static/index.md"; +foreach(reverse @articleList) { + $indexContent .= $_; } +$indexContent = markdown $indexContent; +page "index.html", "blog.vdouillet.fr", $indexContent, "body", "index-body"; -- cgit v1.2.3