-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blog.pm
executable file
·513 lines (386 loc) · 14.2 KB
/
Blog.pm
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package Blog;
use strict;
#use warnings;
use Getopt::Std;
use Text::Template;
use YAML::XS;
use Date::Parse;
use File::Find;
use File::Path qw/make_path rmtree/;
use File::Basename qw/dirname/;
use File::Copy::Recursive qw/rcopy/;
use HTML::Strip;
use HTML::Entities;
use HTML::TagCloud;
use POSIX qw(strftime);
use Data::Dumper;
use DateTime;
$Data::Dumper::Indent = 1;
use utf8;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
our $VERSION = 1.00;
our @ISA = qw(Exporter);
our %EXPORT_TAGS = (all => [
qw(
read_config
make_blog_entry
output_blog_file
output_archive_files
output_keyword_files
output_tagcloud
read_index
write_index
index_entry
) ]);
our @EXPORT_OK =(@{ $EXPORT_TAGS{'all'} }) ;
use constant DEFAULT_CONFIG_FILE => './config.yml';
use constant DEFAULT_INDEX_FILE => './ixdex.yml';
my @month_name = qw(Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec);
our $config;
sub read_config {
my ($filename) = @_;
$filename ||= DEFAULT_CONFIG_FILE;
# print "File ", $filename;
my $config;
if ( -e $filename ) {
$config = YAML::XS::LoadFile($filename) ||
warn "Can't open configuration file $filename: $!";
}
my $base_dir = $config->{'base_dir'}.$config->{'blog_id'};
$config->{'entry_dir'} = $base_dir."/entries/";
$config->{'include_dir'} = $base_dir."/inc/";
$config->{'template_dir'} = $base_dir."/templates/";
$config->{'individual_entry_template_file'} =
Text::Template->new(SOURCE => $config->{'template_dir'}.$config->{'templates'}->{'individual_entry'})
or die "Couldn't construct template: $Text::Template::ERROR";
$config->{'homepage_template_file'} =
Text::Template->new(SOURCE => $config->{'template_dir'}.$config->{'templates'}->{'homepage'})
or die "Couldn't construct template: $Text::Template::ERROR";
$config->{'rss_template_file'} =
Text::Template->new(SOURCE => $config->{'template_dir'}.$config->{'templates'}->{'rss'})
or die "Couldn't construct template: $Text::Template::ERROR";
$config->{'archive_template_file'} =
Text::Template->new(SOURCE => $config->{'template_dir'}.$config->{'templates'}->{'archive'})
or die "Couldn't construct template: $Text::Template::ERROR";
$config->{'keyword_template_file'} =
Text::Template->new(SOURCE => $config->{'template_dir'}.$config->{'templates'}->{'keyword'})
or die "Couldn't construct template: $Text::Template::ERROR";
return $config;
}
sub make_blog_entry {
my($entry_file, $config) = @_;
my $meta = compute_meta($entry_file, $config);
# warn $meta->{'status'}, " ", $meta->{'timestamp'} if $meta->{'status'} eq 'draft';
# warn ">>>>Image url: ". $meta->{'postimage'} . "\n";
#print Dumper($meta);
my $result = $config->{'individual_entry_template_file'}->fill_in(HASH => $meta);
unless (defined $result) {
die "Couldn't fill in template: $Text::Template::ERROR"
}
$meta->{'result'} = $result;
return($meta);
}
sub output_blog_file {
my($config, $meta) = @_;
# make sure directory exists
my $directory = $config->{'target_home'}.$meta->{'entry_dir'};
unless (-d $directory ) {
make_path($directory);
}
# warn "Dir name: ", $meta->{"dirname"}, "\n", "Target: ", $directory, "\n" ;
my @aux_files = glob("$meta->{'dirname'}/*");
foreach my $f (@aux_files) {
rcopy($f, $directory) unless $f =~ /(html|md)$/s;
}
# warn Dumper \@aux_files;
open(OUT, '>', $directory.$meta->{'filename'});
print OUT $meta->{'result'};
close(OUT);
}
sub compute_meta {
my($entry_file, $config) = @_;
my $meta = {'blog_url' => $config->{'blog_url'},
'dirname' => dirname($entry_file)
};
# since meta vars in the entry can be multi-line, we have a little state machine here
my $in_meta = 0;
my $meta_type = "";
my $line;
open(ENTRY,$entry_file) or die "$!: $entry_file";
$meta->{'mtime'} = (stat(ENTRY))[9];
$meta->{'timestamp'} = localtime($meta->{'mtime'});
$meta->{'modified_timestamp'} = localtime($meta->{'mtime'}); # cause timestamp gets updated below
$meta->{'rfc822_time'} = strftime("%a, %d %b %Y %H:%M:%S %z", localtime($meta->{'mtime'}));
my $ext = ".".$config->{'extension'};
my $path_prefix = "/".$config->{'path_prefix'};
# my $path_prefix = $config->{'path_prefix'};
while ($line = <ENTRY>) {
# print "$line";
if ($in_meta) {
if ($line =~ m/-->/) {
$in_meta = 0;
next;
}
$meta->{$meta_type} .= $line;
} elsif ($line =~ m/^<!--/) {
$in_meta = 1;
($meta_type = $line) =~ s/<!--\s+(\w+):.*$/$1/s;
if ($line =~ m/-->/) {
(my $content = $line) =~ s/^.*:\s+(.*)\s+-->$/$1/s;
$meta->{$meta_type} .= $content;
$in_meta = 0;
}
} else {
$meta_type = "body";
$meta->{$meta_type} .= $line;
$in_meta = 0;
}
}
close(ENTRY);
warn "$entry_file produced empty meta" unless $meta->{'title'};
chomp($meta->{'title'});
$meta->{'title'} =~ s/^\s+//;
# compute the filename
($meta->{'filename'} = $meta->{'title'}) =~ s/[\r\n]+//g;
$meta->{'filename'} =~ s/\s$//g;
$meta->{'filename'} =~ s/\s/_/g;
$meta->{'filename'} =~ s/[^A-Za-z0-9-_]//g;
$meta->{'filename'} = lc($meta->{'filename'}).$ext;
# throw it all away if the base_name overrides
if (defined $meta->{'base_name'}) {
$meta->{'base_name'} =~ s/[\r\n]+//g;
$meta->{'filename'} = $meta->{'base_name'}.$ext;
}
# some excerpts have newlines that should be spaces
$meta->{'excerpt'} =~ s/[\r\n]+/ /g;
# want it to be empty if it's empty
$meta->{'excerpt'} =~ s/\s*$//g;
$meta->{'excerpt'} =~ s/\\n//g; # some got an escaped newline
$meta->{'keywords'} =~ s/[\s\r\n]+//g;
$meta->{'keywords'} =~ s/\\n//g; # some got an escaped newline
$meta->{'keywords'} =~ s/[^a-zA-Z0-9_+,.-]//g; # some got an escaped newline
$meta->{'keywords'} = lc($meta->{'keywords'}); # make all keywords lowercase
if ($meta->{"author_url"}) {
$meta->{'author_url'} =~ s/\s*$//g;
$meta->{'author_url'} =~ s/[\r\n]+/ /g;
}
$meta->{'body'} =~ s/\222/'/g;
$meta->{'body'} =~ s/[\223\224]/"/g;
$meta->{'body'} =~ s/\227/--/g;
# default to date, but use timestamp otherwise
if (defined $meta->{'date'}) {
# like this format better
$meta->{'timestamp'} = localtime(str2time($meta->{'date'}));
} else {
$meta->{'date'} = $meta->{'timestamp'};
}
$meta->{'time'} = str2time($meta->{'date'});
# cheating here and assuming MST
$meta->{'datetime'} = DateTime->from_epoch(epoch=>$meta->{"time"})->datetime()."+0600";
# warn $meta->{'timestamp'};
# for path
my ($ss,$mm,$hh,$day,$month,$year,$zone) = strptime($meta->{'timestamp'});
$year = $year + 1900;
$month = sprintf("%02d",$month+1);
$day = sprintf("%02d",$day);
#warn join (":",($ss,$mm,$hh,$day,$month,$year,$zone));
$meta->{'entry_dir'} = "$path_prefix/$year/$month/";
$meta->{'archive'} = "$year|$month";
$meta->{'entry_url'} = $meta->{'entry_dir'} . $meta->{'filename'};
# warn $meta->{'entry_url'};
# want it to be empty if it's empty
if (defined $meta->{'postimage'}) {
$meta->{'postimage'} =~ s/\s*$//g;
$meta->{'postimage'} =~ s/\\n//g; # some got an escaped newline
if($meta->{'postimage'} !~ m/^http/) {
my $image_url = $meta->{'blog_url'} .$meta->{'entry_dir'} . $meta->{'postimage'};
#warn ">>>>Image url: ". $image_url . "\n";
$meta->{'postimage'} = $image_url;
}
}
# warn ">>>>Image url: ". $meta->{'postimage'} . "\n";
# if ($meta->{'entry_url'} eq "/archives/2002/06/egovernment_on.shtml") {
# warn $meta->{'body'};
# }
$meta->{'status'} = 'publish' unless(defined $meta->{'status'});
$meta->{'status'} =~ s/[\s\r\n]+//g;
return $meta;
}
sub read_index {
my ($filename) = @_;
$filename ||= DEFAULT_INDEX_FILE;
my $index;
if ( -e $filename ) {
$index = YAML::XS::LoadFile($filename) ||
warn "Can't open index file $filename: $!";
}
return $index;
}
sub write_index {
my ($filename, $index) = @_;
# warn $filename;
$filename ||= DEFAULT_INDEX_FILE;
YAML::XS::DumpFile($filename, $index) ||
warn "Can't open index file $filename: $!";
}
sub index_entry {
my($index, $meta) = @_;
# create the main entry
$index->{$meta->{'entry_url'}}->{'title'} = $meta->{'title'};
$index->{$meta->{'entry_url'}}->{'timestamp'} = $meta->{'timestamp'};
$index->{$meta->{'entry_url'}}->{'entry_url'} = $meta->{'entry_url'};
if ($meta->{'excerpt'}) {
$index->{$meta->{'entry_url'}}->{'excerpt'} = $meta->{'excerpt'};
} else {
# warn $meta->{'entry_url'};
$index->{$meta->{'entry_url'}}->{'excerpt'} = make_excerpt($meta->{'body'});
}
# create cross-references for the monthly archives
$index->{'archives'}->{$meta->{'archive'}}->{'entries'}->{$meta->{'time'}} = $meta->{'entry_url'};
# create the cross-references for the keywords
foreach my $kw (split(/,\s*/,$meta->{'keywords'})) {
$index->{'keywords'}->{$kw}->{'entries'}->{$meta->{'time'}} = $meta->{'entry_url'};
}
return $index;
}
sub make_excerpt {
my($text) = @_;
my $hs = HTML::Strip->new(emit_spaces=>0);
$hs->set_decode_entities(0);
my $clean_text = $hs->parse( $text );
$hs->eof;
# get first 100 words
my $num_words=100;
my @text_array = split(/\s+/, $clean_text);
$num_words = scalar(@text_array) if (scalar(@text_array) < $num_words);
my $shortened_text = join(' ', @text_array[0..$num_words-1]);
$shortened_text =~ s/\s*$//g;
$shortened_text =~ s/ / /g;
$shortened_text =~ s/Image via Wikipedia //g;
# warn $shortened_text;
return $shortened_text;
}
sub output_archive_files {
my($config, $index, $touched, $verbose) = @_;
my $archives_inc_file = $config->{'target_home'}."/inc/".$config->{'includes'}->{'archives'};
print "Processing archive file $archives_inc_file\n" if $verbose;
open(ARCHIVES, '>', $archives_inc_file);
print ARCHIVES <<_EOF_;
<!-- Modal -->
<div id="ArchiveModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="ArchiveModalLabel" aria-hidden="true">
<div style="background-color: #6FA8DC; color: #FFFFFF" class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h1 id="ArchiveModalLabel">Archives</h1>
</div>
<div class="modal-body"><ul>
_EOF_
foreach my $archive (reverse sort keys %{ $index->{'archives'} }) {
my($year, $month) = split(/\|/, $archive);
my $archive_link = "/$config->{'path_prefix'}/$year/$month/";
my $mn = $month_name[$month-1];
print ARCHIVES <<_EOF_;
<li><a tabindex="-1" href="$archive_link">$mn $year</a></li>
_EOF_
}
print ARCHIVES <<_EOF_;
</ul>
</div>
<!--
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
-->
</div>
_EOF_
close(ARCHIVES);
for my $arc (sort keys %{$touched->{'archives'}}) {
warn "updating $arc" if $verbose;
my($year, $month) = split(/\|/, $arc);
my $archive_file = $config->{'target_home'}."/$config->{'path_prefix'}/$year/$month/index.".$config->{'extension'};
my $archive_meta = {'entries' => [],
'month' => $month_name[$month-1],
'year' => $year,
};
for my $archive_data (sort keys %{ $index->{'archives'}->{$arc}->{'entries'} }) {
my $url = $index->{'archives'}->{$arc}->{'entries'}->{$archive_data};
unshift(@{ $archive_meta->{'entries'} }, $index->{$url});
}
# warn Dumper $archive_meta;
$archive_meta->{'count'} = scalar (@{ $archive_meta->{'entries'} });
my $archive = $config->{'archive_template_file'}->fill_in(HASH => $archive_meta);
if ($archive) {
# warn "writing $archive_file";
open(ARCHIVE, '>', $archive_file);
print ARCHIVE $archive;
close(ARCHIVE);
} else {
die "Couldn't fill in template: $Text::Template::ERROR"
}
}
}
sub output_keyword_files {
my($config, $index, $touched) = @_;
my $directory = $config->{'target_home'}."/".$config->{'tag_prefix'};
unless (-d $directory ) {
make_path($directory);
}
for my $kw (sort keys %{$touched->{'keywords'}}) {
# warn "updating $kw";
my $keyword_file = "$directory/$kw.".$config->{'extension'};
my $keyword_meta = {'entries' => [],
'keyword' => $kw
};
for my $keyword_data (sort keys %{ $index->{'keywords'}->{$kw}->{'entries'} }) {
my $url = $index->{'keywords'}->{$kw}->{'entries'}->{$keyword_data};
unshift(@{ $keyword_meta->{'entries'} }, $index->{$url});
}
$keyword_meta->{'count'} = scalar (@{ $keyword_meta->{'entries'} });
my $keyword = $config->{'keyword_template_file'}->fill_in(HASH => $keyword_meta);
if ($keyword) {
# warn "writing $keyword_file with $keyword_meta->{'count'} entries";
open(KEYWORD, '>', $keyword_file);
print KEYWORD $keyword;
close(KEYWORD);
} else {
die "Couldn't fill in template: $Text::Template::ERROR"
}
}
}
sub output_tagcloud {
my($config, $index) = @_;
my $tag_directory = "/".$config->{'tag_prefix'};
my $cloud = HTML::TagCloud->new();
for my $kw (sort keys %{ $index->{'keywords'} }) {
my $keyword_url = "$tag_directory/$kw.".$config->{'extension'};
my $count = scalar(keys %{ $index->{'keywords'}->{$kw}->{'entries'} });
$cloud->add($kw, $keyword_url, $count);
}
my $tagcloud_inc_file = $config->{'target_home'}."/inc/".$config->{'includes'}->{'tagcloud'};
open(TAGCLOUD, '>', $tagcloud_inc_file);
print TAGCLOUD <<_EOF_;
<!-- Modal -->
<div id="TagModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="TagModalLabel" aria-hidden="true">
<div style="background-color: #6FA8DC; color: #FFFFFF" class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h1 id="TagModalLabel">Top $config->{'tagcloud_entries'} Tags</h1>
</div>
<div class="modal-body">
_EOF_
print TAGCLOUD $cloud->html($config->{'tagcloud_entries'});
print TAGCLOUD <<_EOF_;
</div>
<!--
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
-->
</div>
_EOF_
close(TAGCLOUD);
my $tagcloud_css_file = $config->{'target_home'}."/css/".$config->{'css'}->{'tagcloud'};
open(TAGCLOUD, '>', $tagcloud_css_file);
print TAGCLOUD $cloud->css();
close(TAGCLOUD);
}
1;