-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfold_blog.pl
executable file
·216 lines (164 loc) · 5.15 KB
/
fold_blog.pl
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
#!/usr/bin/perl -w
use lib '/Users/pjw/prog/perl/blog';
use strict;
use utf8;
use Getopt::Std;
use File::Find;
use File::Path qw/make_path rmtree/;
use Data::Dumper;
use POSIX qw(strftime);
use Blog qw/ :all /;
# global options
use vars qw/ %opt /;
my $opt_string = 'd:c:av';
getopts( "$opt_string", \%opt ) or usage();
die "No configuration file..." unless $opt{'c'};
my $config = read_config($opt{'c'});
my $entry_dir = "";
if ($opt{'d'} ) {
$entry_dir = $opt{'d'} ;
} else {
$entry_dir = $config->{'entry_dir'};
}
my $verbose = $opt{'v'};
my $homepage_file = $config->{'target_home'}."/index.".$config->{'extension'};
my $rss_file = $config->{'target_home'}."/".$config->{'rss_file'};
my $index_file = $config->{'base_dir'}.$config->{'blog_id'}."/index.yml";
if ($opt{'a'}) {
warn "Rebuilding all...";
unlink $homepage_file;
unlink $rss_file;
unlink $index_file;
warn "would remove ", $config->{'target_home'}."/".$config->{'tag_prefix'};
warn "would remove ", $config->{'target_home'}."/".$config->{'path_prefix'};
}
# get the mtime of the homepage
my $homepage_mtime;
if (-e $homepage_file) {
open(HOMEPAGE, $homepage_file);
$homepage_mtime = (stat(HOMEPAGE))[9];
close(HOMEPAGE);
} else {
$homepage_mtime = 0;
}
# warn "Homepage mtime for $homepage_file: $homepage_mtime \n";
my $index = read_index($index_file);
my($files);
# get all the files that are html of md files
find( \&wanted, $entry_dir );
sub wanted
{
push(@{ $files }, $File::Find::name) if($File::Find::name=~m/\.(html|md)$/i);
}
$files = [sort {$b cmp $a} @{ $files }];
# warn join(", ", sort @{$files});
$config->{'blog_url'} =~ s[/$][];
my $entry_count = 0;
my $homepage_meta = {
'entries' => [],
'blog_url' => $config->{'blog_url'},
'blog_title' => $config->{'blog_title'},
'blog_author' => $config->{'blog_author'},
'blog_description' => $config->{'blog_description'},
'year' => (localtime())[5] + 1900,
'build_date' => scalar(localtime()),
};
my $rss_meta = {
'entries' => [],
'blog_url' => $config->{'blog_url'},
'blog_title' => $config->{'blog_title'},
'blog_author' => $config->{'blog_author'},
'blog_description' => $config->{'blog_description'},
'year' => (localtime())[5] + 1900,
'build_date' => strftime("%a, %d %b %Y %H:%M:%S %z", localtime()),
};
my $touched = {};
warn "Rebuilding entries";
foreach my $f (@{$files}) {
print "Processing $f\n" if $verbose;
my ($meta) = make_blog_entry($f, $config);
if($meta->{'status'} eq "draft") {
warn "Skipping entry with timestamp $meta->{'timestamp'} because status is 'draft'";
next
}
if(! $meta->{'filename'}) {
warn "Skipping entry with timestamp $meta->{'timestamp'} because filename is empty (check title)";
next
}
$index = index_entry($index, $meta);
# using mtime for homepage as indication of what needs updating
if ($meta->{'mtime'} > $homepage_mtime || $opt{'a'}) {
warn "Generating page" if $verbose;
output_blog_file($config, $meta);
foreach my $kw (split(/,\s*/,$meta->{'keywords'})) {
$touched->{'keywords'}->{$kw} = 1;
}
$touched->{'archives'}->{$meta->{'archive'}} = 1;
} else {
warn "Not generating page since $meta->{'mtime'} <= $homepage_mtime" if $verbose;
}
if ($entry_count < $config->{'homepage_entries'}) {
warn "Adding $meta->{'timestamp'} to homepage...\n";
push(@{ $homepage_meta->{'entries'} }, $meta);
push(@{ $rss_meta->{'entries'} }, $meta);
} else {
last unless $opt{'a'};
}
$entry_count++;
}
# print Dumper $index;
write_index($index_file, $index);
# output recents
my $recents_file = $config->{'target_home'}."/inc/".$config->{'includes'}->{'recents'};
open(RECENTS, '>', $recents_file);
print RECENTS <<_EOF_;
<h3>From the Home Page</h3>
<ul>
_EOF_
my $recent_count = 0;
foreach my $entry (@{ $homepage_meta->{'entries'} }) {
# warn $entry->{entry_url};
print RECENTS " <li><a href='$entry->{entry_url}'>$entry->{title}</a></li>\n";
$recent_count++;
last if ($recent_count > $config->{'recent_entries'});
}
print RECENTS "</ul>";
close(RECENTS);
# print hompage
warn "Printing homepage...";
my $homepage = $config->{'homepage_template_file'}->fill_in(HASH => $homepage_meta);
if ($homepage) {
open(HOMEPAGE, '>', $homepage_file);
print HOMEPAGE $homepage;
close(HOMEPAGE);
} else {
die "Couldn't fill in template: $Text::Template::ERROR"
}
# print rss page
warn "Printing $rss_file";
my $rss = $config->{'rss_template_file'}->fill_in(HASH => $rss_meta);
open(RSS, '>', $rss_file);
print RSS $rss;
close(RSS);
# do archives (creates multiple files including inc file)
warn "Processing archive files...";
output_archive_files($config, $index, $touched, $verbose);
warn "Processing keyword files...";
output_keyword_files($config, $index, $touched, $verbose);
warn "Creating tagcloud...";
output_tagcloud($config, $index, $verbose);
1;
#
# Message about this program and how to use it
#
sub usage {
print STDERR << "EOF";
Formats blog enties.
usage: $0 [-h?] -f individual_entry
-h|? : this (help) message
-c file : configuration file
-d dir : directory holding blog entries
example: $0 -d "2013/02/23/131456.html"
EOF
exit;
}