summaryrefslogtreecommitdiff
path: root/makesite.pl
blob: 92064c55bc1b8ee2a6c47864eae65e8963b52995 (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
#!/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 "</$pageType>";
	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;
	close $articleHandle;

	# Extract output file name: MD file name without the date
	my $pageFile = substr $articleFile, 9, -3;
	$pageFile .= ".html";

	# Extract description
	# TODO
	my $description = "";

	# 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 "</channel>\n</rss>\n";
close $outHandle;