#!/usr/bin/env perl use strict; use warnings; use File::Cat; use File::Slurp; use Text::Markdown "markdown"; use Time::Piece; my $BASE_URL="http://blog.vdouillet.fr/"; sub page { 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"; while(<$inHandle>) { $_ =~ s/\$title/$pageTitle/; print $outHandle $_; } close $inHandle; # Write content and footer if(defined $id) { print $outHandle "<$pageType id=\"$id\">"; } else { print $outHandle "<$pageType>"; } print $outHandle $pageContent; print $outHandle ""; if($pageType eq "article") { cat "template/back.html", $outHandle; } cat "template/footer.html", $outHandle; close $outHandle; # Create gzip version `gzip -kf output/$pageFile`; } # Build about print "Building about\n"; my $aboutContent = read_file "static/about.md"; $aboutContent = markdown $aboutContent; page "about.html", "About", $aboutContent; # Build articles my @articleList; while(<*.md>) { my $articleFile = $_; print "Processing $articleFile\n"; # Extract article title from the title in MD article open my $articleHandle, "<", $articleFile; my $headLine = <$articleHandle>; $headLine = substr $headLine, 2, -1; # Extract description : first paragraph of the article my $description = ""; while(my $line = <$articleHandle>) { if($. == 5) { $description = $line; last; } } $description =~ s/\R//; # remove any new line $description .= ".."; # to have "..." at the end of description close $articleHandle; # Extract output file name: MD file name without the date my $pageFile = substr $articleFile, 9, -3; $pageFile .= ".html"; # Build article my $articleContent = read_file $articleFile; $articleContent = markdown $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; my $articleData = { date => "$month-$day-$year", title => $headLine, address => $pageFile, description => $description }; push @articleList, $articleData; } # Build index with article list in reverse (most recent first) print "Building index\n"; my $indexContent = read_file "static/index.md"; for(my $i = $#articleList; $i >= 0; $i--) { $indexContent .= "* [$articleList[$i]{date} $articleList[$i]{title}]($articleList[$i]{address} \"$articleList[$i]{title}\")\n"; } $indexContent = markdown $indexContent; page "index.html", "blog.vdouillet.fr", $indexContent, "body", "index-body"; # Build RSS feed with article list in reverse (most recent first) print "Building RSS feed\n"; open my $outHandle, ">", "output/rss.xml"; # Write channel info and insert publication and build dates open my $inHandle, "<", "template/rss.xml"; my $pubDate = Time::Piece->strptime($articleList[$#articleList]{date}, "%m-%d-%Y"); # Last article date $pubDate = $pubDate->strftime(); my $buildDate = gmtime->strftime(); # Current date while(<$inHandle>) { $_ =~ s/\$link/$BASE_URL/; $_ =~ s/\$pubDate/$pubDate/; $_ =~ s/\$lastBuildDate/$buildDate/; print $outHandle $_; } close $inHandle; for(my $i = $#articleList; $i >= 0; $i--) { open my $inHandle, "<", "template/rss-item.xml"; my $url = $BASE_URL . $articleList[$i]{address}; my $pubDate = Time::Piece->strptime($articleList[$i]{date}, "%m-%d-%Y"); $pubDate = $pubDate->strftime(); while(<$inHandle>) { $_ =~ s/\$title/$articleList[$i]{title}/; $_ =~ s/\$link/$url/; $_ =~ s/\$description/$articleList[$i]{description}/; $_ =~ s/\$guid/$articleList[$i]{address}/; $_ =~ s/\$pubDate/$pubDate/; print $outHandle $_; } close $inHandle; } print $outHandle "\n\n"; close $outHandle;