-
Notifications
You must be signed in to change notification settings - Fork 20
/
Post.pm
638 lines (488 loc) · 14.8 KB
/
Post.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
package Plerd::Post;
use Moose;
use DateTime;
use DateTime::Format::W3CDTF;
use Text::Markdown qw( markdown );
use Text::SmartyPants;
use URI;
use HTML::Strip;
use Data::GUID;
use HTML::SocialMeta;
use Try::Tiny;
use Readonly;
Readonly my $WPM => 200; # The words-per-minute reading speed to assume
Readonly my $SOCIALMETA_MODE => 'featured_image'; # SocialMeta image mode
has 'plerd' => (
is => 'ro',
required => 1,
isa => 'Plerd',
weak_ref => 1,
);
has 'source_file' => (
is => 'ro',
isa => 'Path::Class::File',
required => 1,
trigger => \&_process_source_file,
);
has 'publication_file' => (
is => 'ro',
isa => 'Path::Class::File',
lazy_build => 1,
);
has 'title' => (
is => 'rw',
isa => 'Str',
);
has 'body' => (
is => 'rw',
isa => 'Str',
);
has 'stripped_body' => (
is => 'ro',
isa => 'Str',
lazy_build => 1,
);
has 'attributes' => (
is => 'rw',
isa => 'HashRef',
);
has 'image' => (
is => 'rw',
isa => 'Maybe[URI]',
default => undef,
);
has 'description' => (
is => 'rw',
isa => 'Str',
default => '',
);
has 'date' => (
is => 'rw',
isa => 'DateTime',
handles => [ qw(
month
month_name
day
year
ymd
) ],
);
has 'published_filename' => (
is => 'rw',
isa => 'Str',
lazy_build => 1,
);
has 'uri' => (
is => 'ro',
isa => 'URI',
lazy_build => 1,
);
has 'guid' => (
is => 'rw',
isa => 'Data::GUID',
);
has 'updated_timestamp' => (
is => 'ro',
isa => 'Str',
lazy_build => 1,
);
has 'published_timestamp' => (
is => 'ro',
isa => 'Str',
lazy_build => 1,
);
has 'newer_post' => (
is => 'ro',
isa => 'Maybe[Plerd::Post]',
lazy_build => 1,
);
has 'older_post' => (
is => 'ro',
isa => 'Maybe[Plerd::Post]',
lazy_build => 1,
);
has 'reading_time' => (
is => 'ro',
isa => 'Num',
lazy_build => 1,
);
has 'socialmeta' => (
is => 'ro',
isa => 'Maybe[HTML::SocialMeta]',
lazy_build => 1,
);
has 'social_meta_tags' => (
is => 'ro',
isa => 'Str',
lazy_build => 1,
);
sub _build_publication_file {
my $self = shift;
return Path::Class::File->new(
$self->plerd->publication_directory,
$self->published_filename,
);
}
sub _build_published_filename {
my $self = shift;
my $filename = $self->source_file->basename;
# If the source filename already seems Plerdish, just replace its extension.
# Else, generate a Plerdish filename based on the post's date and title.
if ( $filename =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/ ) {
$filename =~ s/\..*$/.html/;
}
else {
$filename = $self->title;
my $stripper = HTML::Strip->new( emit_spaces => 0 );
$filename = $stripper->parse( $filename );
$filename =~ s/\s+/-/g;
$filename =~ s/--+/-/g;
$filename =~ s/[^\w\-]+//g;
$filename = lc $filename;
$filename = $self->date->ymd( q{-} ) . q{-} . $filename;
$filename .= '.html';
}
return $filename;
}
sub _build_uri {
my $self = shift;
my $base_uri = $self->plerd->base_uri;
if ($base_uri =~ /[^\/]$/) {
$base_uri .= '/';
}
return URI->new_abs(
$self->published_filename,
$base_uri,
);
}
sub _build_updated_timestamp {
my $self = shift;
my $mtime = $self->source_file->stat->mtime;
my $formatter = DateTime::Format::W3CDTF->new;
my $timestamp = $formatter->format_datetime(
DateTime->from_epoch(
epoch => $mtime,
time_zone => 'local',
),
);
return $timestamp;
}
sub _build_newer_post {
my $self = shift;
my $index = $self->plerd->index_of_post_with_guid->{ $self->guid };
my $newer_post;
if ( $index - 1 >= 0 ) {
$newer_post = $self->plerd->posts->[ $index - 1 ];
}
return $newer_post;
}
sub _build_older_post {
my $self = shift;
my $index = $self->plerd->index_of_post_with_guid->{ $self->guid };
my $older_post = $self->plerd->posts->[ $index + 1 ];
return $older_post;
}
sub _build_published_timestamp {
my $self = shift;
my $formatter = DateTime::Format::W3CDTF->new;
my $timestamp = $formatter->format_datetime( $self->date );
return $timestamp;
}
sub _build_guid {
my $self = shift;
return Data::GUID->new;
}
sub _build_reading_time {
my $self = shift;
my @words = $self->stripped_body =~ /(\w+)\W*/g;
return int ( scalar(@words) / $WPM ) + 1;
}
sub _build_stripped_body {
my $self = shift;
my $stripper = HTML::Strip->new;
my $body = $stripper->parse( $self->body );
return $body;
}
sub _build_socialmeta {
my $self = shift;
unless ( $self->image ) {
# Neither this post nor this whole blog defines an image URL.
# So, no social meta-tags for this post.
return;
}
my %args = (
site_name => $self->plerd->title,
title => $self->title,
description => $self->description,
image => $self->image->as_string,
url => $self->uri->as_string,
fb_app_id => $self->plerd->facebook_id || '',
site => $self->plerd->twitter_id || '',
);
$args{ site } = '@' . $args{ site } if $args{ site };
my $socialmeta;
try {
$socialmeta = HTML::SocialMeta->new( %args );
}
catch {
warn "Couldn't build an HTML::SocialMeta object for post "
. $self->source_file->basename
. ": $_\n";
};
return $socialmeta;
}
sub _build_social_meta_tags {
my $self = shift;
my $tags = '';
my %targets = (
twitter => 'twitter_id',
opengraph => 'facebook_id',
);
if ( $self->socialmeta ) {
for my $target ( keys %targets ) {
my $id_method = $targets{ $target };
if ( $self->plerd->$id_method ) {
try {
$tags .=
$self->socialmeta->$target->create( $SOCIALMETA_MODE );
}
catch {
warn "Couldn't create $target meta tags for "
. $self->source_file->basename
. ": $_\n";
};
}
}
}
return $tags;
}
# This next internal method does a bunch of stuff.
# It's called via Moose-trigger when the object's source_file attribute is set.
# * Read and store the file's data (body) and metadata
# * Figure out the publication timestamp, based on possible (not guaranteed!)
# presence of date in the filename AND/OR "time" metadata attribute
# * If the file lacks a various required attributes, rewrite the file so that
# it has them.
sub _process_source_file {
my $self = shift;
# Slurp the file, storing the title and time metadata, and the body.
my $fh = $self->source_file->openr;
my %attributes;
my @ordered_attribute_names = qw( title time published_filename guid );
while ( my $line = <$fh> ) {
chomp $line;
last unless $line =~ /\S/;
my ($key, $value) = $line =~ /^\s*(\w+?)\s*:\s*(.*)$/;
if ( $key ) {
$key = lc $key;
$attributes{ $key } = $value;
unless ( grep { $_ eq $key } @ordered_attribute_names ) {
push @ordered_attribute_names, $key;
}
}
}
$self->attributes( \%attributes );
my $body;
while ( <$fh> ) {
$body .= $_;
}
close $fh;
if ( $attributes{ title } ) {
$self->title( $attributes{ title } );
}
else {
die 'Error processing ' . $self->source_file . ': '
. 'File content does not define a post title.'
;
}
$self->body( $body );
if ( $attributes{ description } ) {
$self->description( $attributes{ description } );
}
else {
my $body = $self->stripped_body;
my ( $description ) = $body =~ /^(.*)\n/;
$self->description( $description || '' );
}
if ( $attributes{ image } ) {
$self->image( URI->new( $attributes{ image } ) );
}
else {
$self->image( $self->plerd->image );
}
foreach ( qw( title body ) ) {
if ( defined( $self->$_ ) ) {
$self->$_( Text::SmartyPants::process( markdown( $self->$_ ) ) );
}
}
# Strip unnecessary <p> tags that the markdown processor just added to the title.
my $stripped_title = $self->title;
$stripped_title =~ s{</?p>\s*}{}g;
$self->title( $stripped_title );
# Note whether the filename asserts the post's publication date.
my ( $filename_year, $filename_month, $filename_day ) =
$self->source_file->basename =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/;
# Set the post's date, using these rules:
# * If the post has a time attribute in W3 format, use that
# * Elsif the post's filename asserts a date, use midnight of that date,
# and also add a time attribute to the file.
# * Else use right now, and also add a time attribute to the file.
my $attributes_need_to_be_written_out = 0;
if ( $attributes{ time } ) {
eval {
$self->date(
$self->plerd->datetime_formatter->parse_datetime( $attributes{ time } )
);
$self->date->set_time_zone( 'local' );
};
unless ( $self->date ) {
die 'Error processing ' . $self->source_file . ': '
. 'The "time" attribute is not in W3C format.'
;
}
}
else {
my $publication_dt;
if ( $filename_year ) {
# The post specifies its day in the filename, but we still don't have a
# publication hour.
# If the filename's date is today (locally), use the current time.
# Otherwise, use midnight of the provided date.
my $now = DateTime->now( time_zone => 'local' );
my $ymd = $now->ymd( q{-} );
if ( $self->source_file->basename =~ /^$ymd/ ) {
$publication_dt = $now;
}
else {
$publication_dt = DateTime->new(
year => $filename_year,
month => $filename_month,
day => $filename_day,
time_zone => 'local',
);
}
}
else {
# The file doesn't name the time, *and* the file doesn't contain the date
# in metadata (or else we wouldn't be here), so we'll just use right-now.
$publication_dt = DateTime->now( time_zone => 'local' );
}
$self->date( $publication_dt );
my $date_string =
$self->plerd->datetime_formatter->format_datetime( $publication_dt );
$attributes{ time } = $date_string;
$attributes_need_to_be_written_out = 1;
}
if ( $attributes{ published_filename } ) {
$self->published_filename( $attributes{ published_filename } );
}
else {
$attributes{ published_filename } = $self->published_filename;
$attributes_need_to_be_written_out = 1;
}
if ( $attributes{ guid } ) {
$self->guid( Data::GUID->from_string( $attributes{ guid } ) );
}
else {
$attributes{ guid } = Data::GUID->new;
$self->guid( $attributes{ guid } );
$attributes_need_to_be_written_out = 1;
}
if ( $attributes_need_to_be_written_out ) {
my $new_content = '';
for my $attribute_name ( @ordered_attribute_names ) {
$new_content .= "$attribute_name: $attributes{ $attribute_name }\n";
}
$new_content .= "\n$body\n";
$self->source_file->spew( $new_content );
}
}
sub publish {
my $self = shift;
# Make <title>-ready text free of possible Markdown-generated HTML tags.
my $stripped_title = $self->title;
$stripped_title =~ s{</?(em|strong)>}{}g;
$self->plerd->template->process(
$self->plerd->post_template_file->openr,
{
plerd => $self->plerd,
posts => [ $self ],
title => $stripped_title,
context_post => $self,
},
$self->publication_file->openw,
);
}
1;
=head1 NAME
Plerd::Post - A Plerd blog post
=head1 DESCRIPTION
An object of the class Plerd::Post represents a single post to a
Plerd-based blog, with Markdown source and HTML output.
=head1 CLASS METHODS
=over
=item new( \%config )
Object constructor. The single config hashref I<must> include the following keys:
=over
=item plerd
The parent Plerd object.
=item source_file
A Path::Class::File object representing this post's Markdown source file.
=back
=back
=head1 OBJECT ATTRIBUTES
=head2 Read-only attributes
=over
=item newer_post
A Plerd::Post object representing the next-newer post to the blog.
Is the current object represents the newest post in the blog, then this method
returns undef.
=item older_post
A Plerd::Post object representing the next-older post to the blog.
Is the current object represents the oldest post in the blog, then this method
returns undef.
=item published_filename
The local filename (without parent directory path) of the HTML file that this post
will generate upon publication.
=item published_timestamp
This post's date, in W3C format, set to midnight in the local timezone.
=item reading_time
An estimated reading-time for this post, measured in whole minutes, and based
on an assumed (and fairly conservative) reading pace of 200 words per minute.
=item updated_timestamp
The modification time of this this post's source file, in W3C format, set to
the local timezone.
=item uri
The L<URI> of the of the HTML file that this post will generate upon publication.
=back
=head2 Read-write attributes
=over
=item attributes
A hashref of all the attributes defined in the source document's metadata
section, whether or not Plerd takes any special meaning from them.
For example, if a source document defines both C<title> and C<favorite_color>
key-value pairs in its metadata, both keys and values will appear in this
hashref, even though Plerd pays no mind to the latter key.
=item body
String representing the post's body text.
=item date
L<DateTime> object representing this post's presented publication date.
=item description
String representing a short, descriptive summary of this post. This value affects
the metadata attached to this post, for use by social media and such.
If you don't set this value yourself by the time Plerd needs it, then it will
set it to the first paragraph of the post's body text (with all markup removed).
=item image
(Optional) L<URI> object referencing an illustrative image for this post.
Setting this value affects the metadata attached to this post, for use by social
media and such.
=item title
String representing this post's title.
=back
=head1 OBJECT METHODS
=over
=item publish
Publishes the post.
=back
=head1 AUTHOR
Jason McIntosh <jmac@jmac.org>