| 1 | package Plerd::Post; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 13 | ▶ | use Moose; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 13 | ▶ | use DateTime; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 13 | ▶ | use DateTime::Format::W3CDTF; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 13 | ▶ | use Markdown::Perl; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 13 | ▶ | use URI; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 13 | ▶ | use HTML::Strip; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 13 | ▶ | use Data::GUID; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 13 | ▶ | use HTML::SocialMeta; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 11 | 13 | ▶ | use Try::Tiny; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 13 | ▶ | use JSON; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | 13 | ▶ | use Path::Class::File; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 14 | 13 | ▶ | use Digest::MD5 qw( md5_hex ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 15 | 13 | ▶ | use Encode qw( encode_utf8 ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 13 | ▶ | use Plerd::SmartyPants; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 18 | 13 | ▶ | use Web::Mention; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 19 | |||||||||||||||||||||||
| 20 | 13 | ▶ | use Readonly; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 21 | 13 | Readonly my $WPM => 200; # The words-per-minute reading speed to assume | |||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | 13 | my $markdown = Markdown::Perl->new( | |||||||||||||||||||||
| 24 | mode => 'github', | ||||||||||||||||||||||
| 25 | # Drop '"' so literal quotes survive for Plerd::SmartyPants to curl. | ||||||||||||||||||||||
| 26 | html_escaped_characters => '&<>', | ||||||||||||||||||||||
| 27 | # GitHub-Flavored Markdown escapes raw <iframe>, <script>, <style> and | ||||||||||||||||||||||
| 28 | # similar tags, since GitHub renders untrusted input. A blog author writes | ||||||||||||||||||||||
| 29 | # their own source files, so let these through. | ||||||||||||||||||||||
| 30 | disallowed_html_tags => [], | ||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||
| 32 | 26 | ▶ | sub markdown { $markdown->convert( $_[0] ) } | ||||||||||||||||||||
|
Subroutine
markdown: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||
| 33 | |||||||||||||||||||||||
| 34 | 13 | has 'plerd' => ( | |||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||
| 36 | required => 1, | ||||||||||||||||||||||
| 37 | isa => 'Plerd', | ||||||||||||||||||||||
| 38 | weak_ref => 1, | ||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | 13 | has 'source_file' => ( | |||||||||||||||||||||
| 42 | is => 'ro', | ||||||||||||||||||||||
| 43 | isa => 'Path::Class::File', | ||||||||||||||||||||||
| 44 | required => 1, | ||||||||||||||||||||||
| 45 | trigger => \&_process_source_file, | ||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||
| 47 | |||||||||||||||||||||||
| 48 | 13 | has 'publication_file' => ( | |||||||||||||||||||||
| 49 | is => 'ro', | ||||||||||||||||||||||
| 50 | isa => 'Path::Class::File', | ||||||||||||||||||||||
| 51 | lazy_build => 1, | ||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||
| 53 | |||||||||||||||||||||||
| 54 | 13 | has 'title' => ( | |||||||||||||||||||||
| 55 | is => 'rw', | ||||||||||||||||||||||
| 56 | isa => 'Str', | ||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | 13 | has 'body' => ( | |||||||||||||||||||||
| 60 | is => 'rw', | ||||||||||||||||||||||
| 61 | isa => 'Str', | ||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||
| 63 | |||||||||||||||||||||||
| 64 | 13 | has 'stripped_body' => ( | |||||||||||||||||||||
| 65 | is => 'ro', | ||||||||||||||||||||||
| 66 | isa => 'Str', | ||||||||||||||||||||||
| 67 | lazy_build => 1, | ||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||
| 69 | |||||||||||||||||||||||
| 70 | 13 | has 'stripped_title' => ( | |||||||||||||||||||||
| 71 | is => 'ro', | ||||||||||||||||||||||
| 72 | isa => 'Str', | ||||||||||||||||||||||
| 73 | lazy_build => 1, | ||||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | 13 | has 'attributes' => ( | |||||||||||||||||||||
| 77 | is => 'rw', | ||||||||||||||||||||||
| 78 | isa => 'HashRef', | ||||||||||||||||||||||
| 79 | ); | ||||||||||||||||||||||
| 80 | |||||||||||||||||||||||
| 81 | has 'tag_objects' => ( | ||||||||||||||||||||||
| 82 | is => 'rw', | ||||||||||||||||||||||
| 83 | isa => 'ArrayRef', | ||||||||||||||||||||||
| 84 | 13 | ▶ | default => sub {[]} | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 85 | 13 | ); | |||||||||||||||||||||
| 86 | |||||||||||||||||||||||
| 87 | 13 | has 'image' => ( | |||||||||||||||||||||
| 88 | is => 'rw', | ||||||||||||||||||||||
| 89 | isa => 'Maybe[URI]', | ||||||||||||||||||||||
| 90 | default => undef, | ||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | 13 | has 'image_alt' => ( | |||||||||||||||||||||
| 94 | is => 'rw', | ||||||||||||||||||||||
| 95 | isa => 'Maybe[Str]', | ||||||||||||||||||||||
| 96 | default => undef, | ||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | 13 | has 'description' => ( | |||||||||||||||||||||
| 100 | is => 'rw', | ||||||||||||||||||||||
| 101 | isa => 'Str', | ||||||||||||||||||||||
| 102 | default => '', | ||||||||||||||||||||||
| 103 | ); | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | 13 | has 'date' => ( | |||||||||||||||||||||
| 106 | is => 'rw', | ||||||||||||||||||||||
| 107 | isa => 'DateTime', | ||||||||||||||||||||||
| 108 | handles => [ qw( | ||||||||||||||||||||||
| 109 | month | ||||||||||||||||||||||
| 110 | month_name | ||||||||||||||||||||||
| 111 | day | ||||||||||||||||||||||
| 112 | year | ||||||||||||||||||||||
| 113 | ymd | ||||||||||||||||||||||
| 114 | hms | ||||||||||||||||||||||
| 115 | ) ], | ||||||||||||||||||||||
| 116 | trigger => \&_build_utc_date, | ||||||||||||||||||||||
| 117 | ); | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | 13 | has 'utc_date' => ( | |||||||||||||||||||||
| 120 | is => 'rw', | ||||||||||||||||||||||
| 121 | isa => 'DateTime', | ||||||||||||||||||||||
| 122 | lazy_build => 1, | ||||||||||||||||||||||
| 123 | ); | ||||||||||||||||||||||
| 124 | |||||||||||||||||||||||
| 125 | 13 | has 'published_filename' => ( | |||||||||||||||||||||
| 126 | is => 'rw', | ||||||||||||||||||||||
| 127 | isa => 'Str', | ||||||||||||||||||||||
| 128 | lazy_build => 1, | ||||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||||
| 130 | |||||||||||||||||||||||
| 131 | 13 | has 'uri' => ( | |||||||||||||||||||||
| 132 | is => 'ro', | ||||||||||||||||||||||
| 133 | isa => 'URI', | ||||||||||||||||||||||
| 134 | lazy_build => 1, | ||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||
| 136 | |||||||||||||||||||||||
| 137 | 13 | has 'guid' => ( | |||||||||||||||||||||
| 138 | is => 'rw', | ||||||||||||||||||||||
| 139 | isa => 'Data::GUID', | ||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||
| 141 | |||||||||||||||||||||||
| 142 | 13 | has 'updated_timestamp' => ( | |||||||||||||||||||||
| 143 | is => 'ro', | ||||||||||||||||||||||
| 144 | isa => 'Str', | ||||||||||||||||||||||
| 145 | lazy_build => 1, | ||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||
| 147 | |||||||||||||||||||||||
| 148 | 13 | has 'published_timestamp' => ( | |||||||||||||||||||||
| 149 | is => 'ro', | ||||||||||||||||||||||
| 150 | isa => 'Str', | ||||||||||||||||||||||
| 151 | lazy_build => 1, | ||||||||||||||||||||||
| 152 | ); | ||||||||||||||||||||||
| 153 | |||||||||||||||||||||||
| 154 | 13 | has 'newer_post' => ( | |||||||||||||||||||||
| 155 | is => 'ro', | ||||||||||||||||||||||
| 156 | isa => 'Maybe[Plerd::Post]', | ||||||||||||||||||||||
| 157 | lazy_build => 1, | ||||||||||||||||||||||
| 158 | ); | ||||||||||||||||||||||
| 159 | |||||||||||||||||||||||
| 160 | 13 | has 'older_post' => ( | |||||||||||||||||||||
| 161 | is => 'ro', | ||||||||||||||||||||||
| 162 | isa => 'Maybe[Plerd::Post]', | ||||||||||||||||||||||
| 163 | lazy_build => 1, | ||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||
| 165 | |||||||||||||||||||||||
| 166 | 13 | has 'reading_time' => ( | |||||||||||||||||||||
| 167 | is => 'ro', | ||||||||||||||||||||||
| 168 | isa => 'Num', | ||||||||||||||||||||||
| 169 | lazy_build => 1, | ||||||||||||||||||||||
| 170 | ); | ||||||||||||||||||||||
| 171 | |||||||||||||||||||||||
| 172 | 13 | has 'socialmeta' => ( | |||||||||||||||||||||
| 173 | is => 'ro', | ||||||||||||||||||||||
| 174 | isa => 'Maybe[HTML::SocialMeta]', | ||||||||||||||||||||||
| 175 | lazy_build => 1, | ||||||||||||||||||||||
| 176 | ); | ||||||||||||||||||||||
| 177 | |||||||||||||||||||||||
| 178 | 13 | has 'social_meta_tags' => ( | |||||||||||||||||||||
| 179 | is => 'ro', | ||||||||||||||||||||||
| 180 | isa => 'Str', | ||||||||||||||||||||||
| 181 | lazy_build => 1, | ||||||||||||||||||||||
| 182 | ); | ||||||||||||||||||||||
| 183 | |||||||||||||||||||||||
| 184 | 13 | has 'socialmeta_mode' => ( | |||||||||||||||||||||
| 185 | is => 'rw', | ||||||||||||||||||||||
| 186 | isa => 'Str', | ||||||||||||||||||||||
| 187 | default => 'summary', | ||||||||||||||||||||||
| 188 | ); | ||||||||||||||||||||||
| 189 | |||||||||||||||||||||||
| 190 | 3 | ▶ | sub _build_publication_file { | ||||||||||||||||||||
|
Subroutine
_build_publication_file: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 191 | 3 | my $self = shift; | |||||||||||||||||||||
| 192 | |||||||||||||||||||||||
| 193 | 3 | return Path::Class::File->new( | |||||||||||||||||||||
| 194 | $self->plerd->publication_directory, | ||||||||||||||||||||||
| 195 | $self->published_filename, | ||||||||||||||||||||||
| 196 | ); | ||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||
| 198 | |||||||||||||||||||||||
| 199 | 6 | ▶ | sub _build_published_filename { | ||||||||||||||||||||
|
Subroutine
_build_published_filename: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 200 | 6 | my $self = shift; | |||||||||||||||||||||
| 201 | |||||||||||||||||||||||
| 202 | 6 | my $filename = $self->source_file->basename; | |||||||||||||||||||||
| 203 | |||||||||||||||||||||||
| 204 | # If the source filename already seems Plerdish, just replace its extension. | ||||||||||||||||||||||
| 205 | # Else, generate a Plerdish filename based on the post's date and title. | ||||||||||||||||||||||
| 206 | 6 | ▶ | if ( $filename =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/ ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 207 | 3 | $filename =~ s/\..*$/.html/; | |||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||
| 209 | else { | ||||||||||||||||||||||
| 210 | 3 | my $stripper = HTML::Strip->new( emit_spaces => 0 ); | |||||||||||||||||||||
| 211 | 3 | my $slug = $stripper->parse( $self->title ); | |||||||||||||||||||||
| 212 | 3 | $slug =~ s/\s+/-/g; | |||||||||||||||||||||
| 213 | 3 | $slug =~ s/--+/-/g; | |||||||||||||||||||||
| 214 | 3 | $slug =~ s/[^\w\-]+//g; | |||||||||||||||||||||
| 215 | 3 | $slug = lc $slug; | |||||||||||||||||||||
| 216 | |||||||||||||||||||||||
| 217 | # A title made entirely of characters that can't appear in a filename | ||||||||||||||||||||||
| 218 | # (e.g. emoji) slugs down to nothing, which would make every such post | ||||||||||||||||||||||
| 219 | # on a given day collide on "YYYY-MM-DD-.html". Fall back to a short | ||||||||||||||||||||||
| 220 | # hash of the title, so distinct titles get distinct, stable filenames. | ||||||||||||||||||||||
| 221 | 3 | ▶ | unless ( length $slug ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 222 | 2 | $slug = substr( md5_hex( encode_utf8( $self->title ) ), 0, 10 ); | |||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||
| 224 | |||||||||||||||||||||||
| 225 | 3 | $filename = $self->date->ymd( q{-} ) . q{-} . $slug . '.html'; | |||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||
| 227 | |||||||||||||||||||||||
| 228 | 6 | return $filename; | |||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||
| 230 | |||||||||||||||||||||||
| 231 | 3 | ▶ | sub _build_uri { | ||||||||||||||||||||
|
Subroutine
_build_uri: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 232 | 3 | my $self = shift; | |||||||||||||||||||||
| 233 | |||||||||||||||||||||||
| 234 | 3 | return URI->new_abs( | |||||||||||||||||||||
| 235 | $self->published_filename, | ||||||||||||||||||||||
| 236 | $self->plerd->base_uri_with_slash, | ||||||||||||||||||||||
| 237 | ); | ||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||
| 239 | |||||||||||||||||||||||
| 240 | 0 | ▶ | sub _build_updated_timestamp { | ||||||||||||||||||||
|
Subroutine
_build_updated_timestamp: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 241 | 0 | my $self = shift; | |||||||||||||||||||||
| 242 | |||||||||||||||||||||||
| 243 | 0 | my $mtime = $self->source_file->stat->mtime; | |||||||||||||||||||||
| 244 | |||||||||||||||||||||||
| 245 | 0 | my $formatter = DateTime::Format::W3CDTF->new; | |||||||||||||||||||||
| 246 | 0 | my $timestamp = $formatter->format_datetime( | |||||||||||||||||||||
| 247 | DateTime->from_epoch( | ||||||||||||||||||||||
| 248 | epoch => $mtime, | ||||||||||||||||||||||
| 249 | time_zone => 'local', | ||||||||||||||||||||||
| 250 | ), | ||||||||||||||||||||||
| 251 | ); | ||||||||||||||||||||||
| 252 | |||||||||||||||||||||||
| 253 | 0 | return $timestamp; | |||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||
| 255 | |||||||||||||||||||||||
| 256 | 3 | ▶ | sub _build_newer_post { | ||||||||||||||||||||
|
Subroutine
_build_newer_post: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 257 | 3 | my $self = shift; | |||||||||||||||||||||
| 258 | |||||||||||||||||||||||
| 259 | 3 | ▶ | return $self->_neighbor_post( -1 ) unless $self->plerd->has_posts; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 260 | |||||||||||||||||||||||
| 261 | 3 | my $index = $self->plerd->index_of_post_with_guid->{ $self->guid }; | |||||||||||||||||||||
| 262 | |||||||||||||||||||||||
| 263 | 3 | my $newer_post; | |||||||||||||||||||||
| 264 | 3 | ▶ | if ( $index - 1 >= 0 ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 265 | 1 | $newer_post = $self->plerd->posts->[ $index - 1 ]; | |||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||
| 267 | |||||||||||||||||||||||
| 268 | 3 | return $newer_post; | |||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||
| 270 | |||||||||||||||||||||||
| 271 | 3 | ▶ | sub _build_older_post { | ||||||||||||||||||||
|
Subroutine
_build_older_post: called CC=2 SCAR=7.0
| |||||||||||||||||||||||
| 272 | 3 | my $self = shift; | |||||||||||||||||||||
| 273 | |||||||||||||||||||||||
| 274 | 3 | ▶ | return $self->_neighbor_post( +1 ) unless $self->plerd->has_posts; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 275 | |||||||||||||||||||||||
| 276 | 3 | my $index = $self->plerd->index_of_post_with_guid->{ $self->guid }; | |||||||||||||||||||||
| 277 | |||||||||||||||||||||||
| 278 | 3 | my $older_post = $self->plerd->posts->[ $index + 1 ]; | |||||||||||||||||||||
| 279 | |||||||||||||||||||||||
| 280 | 3 | return $older_post; | |||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||
| 282 | |||||||||||||||||||||||
| 283 | # Resolve a neighbor post (offset -1 = newer, +1 = older) from the blog's | ||||||||||||||||||||||
| 284 | # post index, constructing only that one neighbor. Used during an incremental | ||||||||||||||||||||||
| 285 | # publish, when the full post list isn't in memory. Returns undef at the ends | ||||||||||||||||||||||
| 286 | # of the blog or if the neighbor's source file has since vanished. | ||||||||||||||||||||||
| 287 | 0 | ▶ | sub _neighbor_post { | ||||||||||||||||||||
|
Subroutine
_neighbor_post: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||
| 288 | 0 | my $self = shift; | |||||||||||||||||||||
| 289 | 0 | my ( $offset ) = @_; | |||||||||||||||||||||
| 290 | |||||||||||||||||||||||
| 291 | 0 | my $basename = $self->plerd->neighbor_basename( | |||||||||||||||||||||
| 292 | $self->source_file->basename, | ||||||||||||||||||||||
| 293 | $offset, | ||||||||||||||||||||||
| 294 | ); | ||||||||||||||||||||||
| 295 | 0 | ▶ | return unless defined $basename; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 296 | |||||||||||||||||||||||
| 297 | 0 | my $file = $self->plerd->source_directory->file( $basename ); | |||||||||||||||||||||
| 298 | 0 | ▶ | return unless -e $file; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 299 | |||||||||||||||||||||||
| 300 | 0 | return Plerd::Post->new( | |||||||||||||||||||||
| 301 | plerd => $self->plerd, | ||||||||||||||||||||||
| 302 | source_file => $file, | ||||||||||||||||||||||
| 303 | ); | ||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||
| 305 | |||||||||||||||||||||||
| 306 | 0 | ▶ | sub _build_published_timestamp { | ||||||||||||||||||||
|
Subroutine
_build_published_timestamp: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 307 | 0 | my $self = shift; | |||||||||||||||||||||
| 308 | |||||||||||||||||||||||
| 309 | 0 | my $formatter = DateTime::Format::W3CDTF->new; | |||||||||||||||||||||
| 310 | 0 | my $timestamp = $formatter->format_datetime( $self->date ); | |||||||||||||||||||||
| 311 | |||||||||||||||||||||||
| 312 | 0 | return $timestamp; | |||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||
| 314 | |||||||||||||||||||||||
| 315 | 0 | ▶ | sub _build_guid { | ||||||||||||||||||||
|
Subroutine
_build_guid: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 316 | 0 | my $self = shift; | |||||||||||||||||||||
| 317 | |||||||||||||||||||||||
| 318 | 0 | return Data::GUID->new; | |||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||
| 320 | |||||||||||||||||||||||
| 321 | 0 | ▶ | sub _build_reading_time { | ||||||||||||||||||||
|
Subroutine
_build_reading_time: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 322 | 0 | my $self = shift; | |||||||||||||||||||||
| 323 | |||||||||||||||||||||||
| 324 | 0 | my @words = $self->stripped_body =~ /(\w+)\W*/g; | |||||||||||||||||||||
| 325 | |||||||||||||||||||||||
| 326 | 0 | return int ( scalar(@words) / $WPM ) + 1; | |||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||
| 328 | |||||||||||||||||||||||
| 329 | 13 | ▶ | sub _build_stripped_body { | ||||||||||||||||||||
|
Subroutine
_build_stripped_body: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 330 | 13 | my $self = shift; | |||||||||||||||||||||
| 331 | |||||||||||||||||||||||
| 332 | 13 | return $self->_strip_html( $self->body ); | |||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||
| 334 | |||||||||||||||||||||||
| 335 | 0 | ▶ | sub _build_stripped_title { | ||||||||||||||||||||
|
Subroutine
_build_stripped_title: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 336 | 0 | my $self = shift; | |||||||||||||||||||||
| 337 | |||||||||||||||||||||||
| 338 | 0 | return $self->_strip_html( $self->title ); | |||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||
| 340 | |||||||||||||||||||||||
| 341 | 13 | ▶ | sub _strip_html { | ||||||||||||||||||||
|
Subroutine
_strip_html: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 342 | 13 | my ($self, $raw_text) = @_; | |||||||||||||||||||||
| 343 | |||||||||||||||||||||||
| 344 | 13 | my $stripped = HTML::Strip->new->parse( $raw_text ); | |||||||||||||||||||||
| 345 | |||||||||||||||||||||||
| 346 | # Clean up apparently orphaned punctuation | ||||||||||||||||||||||
| 347 | 13 | $stripped =~ s{ ([;.,\?\!])}{$1}g; | |||||||||||||||||||||
| 348 | |||||||||||||||||||||||
| 349 | 13 | return $stripped; | |||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||
| 351 | |||||||||||||||||||||||
| 352 | 3 | ▶ | sub _build_socialmeta { | ||||||||||||||||||||
|
Subroutine
_build_socialmeta: called CC=5 SCAR=28.2
| |||||||||||||||||||||||
| 353 | 3 | my $self = shift; | |||||||||||||||||||||
| 354 | |||||||||||||||||||||||
| 355 | 3 | ▶ | unless ( $self->image ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 356 | # Neither this post nor this whole blog defines an image URL. | ||||||||||||||||||||||
| 357 | # So, no social meta-tags for this post. | ||||||||||||||||||||||
| 358 | 3 | return; | |||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||
| 360 | |||||||||||||||||||||||
| 361 | 0 | ▶ | my %args = ( | ||||||||||||||||||||
|
Condition0%0/4
$self->plerd->facebook_id || ''
$self->plerd->twitter_id || ''
MC/DC0%0/1
$self->plerd->facebook_id || ''
$self->plerd->facebook_id
MC/DC0%0/1
$self->plerd->twitter_id || ''
$self->plerd->twitter_id
Truth table0%0/2
A
A= $self->plerd->facebook_id
Truth table0%0/2
A
A= $self->plerd->twitter_id
| |||||||||||||||||||||||
| 362 | site_name => $self->plerd->title, | ||||||||||||||||||||||
| 363 | title => $self->title, | ||||||||||||||||||||||
| 364 | description => $self->description, | ||||||||||||||||||||||
| 365 | image => $self->image->as_string, | ||||||||||||||||||||||
| 366 | url => $self->uri->as_string, | ||||||||||||||||||||||
| 367 | fb_app_id => $self->plerd->facebook_id || '', | ||||||||||||||||||||||
| 368 | site => $self->plerd->twitter_id || '', | ||||||||||||||||||||||
| 369 | image_alt => $self->image_alt, | ||||||||||||||||||||||
| 370 | ); | ||||||||||||||||||||||
| 371 | |||||||||||||||||||||||
| 372 | 0 | ▶ | $args{ site } = '@' . $args{ site } if $args{ site }; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 373 | |||||||||||||||||||||||
| 374 | 0 | my $socialmeta; | |||||||||||||||||||||
| 375 | |||||||||||||||||||||||
| 376 | 0 | ▶ | try { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 377 | 0 | $socialmeta = HTML::SocialMeta->new( %args ); | |||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||
| 379 | 0 | ▶ | catch { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 380 | 0 | warn "Couldn't build an HTML::SocialMeta object for post " | |||||||||||||||||||||
| 381 | . $self->source_file->basename | ||||||||||||||||||||||
| 382 | . ": $_\n"; | ||||||||||||||||||||||
| 383 | 0 | }; | |||||||||||||||||||||
| 384 | |||||||||||||||||||||||
| 385 | 0 | return $socialmeta; | |||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||
| 387 | |||||||||||||||||||||||
| 388 | 3 | ▶ | sub _build_social_meta_tags { | ||||||||||||||||||||
|
Subroutine
_build_social_meta_tags: called CC=4 SCAR=22.9
| |||||||||||||||||||||||
| 389 | 3 | my $self = shift; | |||||||||||||||||||||
| 390 | |||||||||||||||||||||||
| 391 | 3 | my $tags = ''; | |||||||||||||||||||||
| 392 | |||||||||||||||||||||||
| 393 | # No image (post-specific or blog-wide) means no social metadata at all. | ||||||||||||||||||||||
| 394 | 3 | ▶ | return $tags unless $self->socialmeta; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 395 | |||||||||||||||||||||||
| 396 | # Open Graph tags get emitted for every post that has an image. A | ||||||||||||||||||||||
| 397 | # configured facebook_id, if any, populates the fb:app_id tag; without | ||||||||||||||||||||||
| 398 | # one, we drop that empty tag rather than emit it blank. | ||||||||||||||||||||||
| 399 | 0 | $tags .= $self->_create_social_meta_tags( 'opengraph' ); | |||||||||||||||||||||
| 400 | 0 | ▶ | unless ( $self->plerd->facebook_id ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 401 | 0 | $tags =~ s{\n?<meta property="fb:app_id" content=""/>}{}; | |||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||
| 403 | |||||||||||||||||||||||
| 404 | # Twitter Cards are a legacy extra, emitted only when the blog defines a | ||||||||||||||||||||||
| 405 | # twitter_id. | ||||||||||||||||||||||
| 406 | 0 | ▶ | $tags .= $self->_create_social_meta_tags( 'twitter' ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 407 | if $self->plerd->twitter_id; | ||||||||||||||||||||||
| 408 | |||||||||||||||||||||||
| 409 | 0 | return $tags; | |||||||||||||||||||||
| 410 | |||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||
| 412 | |||||||||||||||||||||||
| 413 | 0 | ▶ | sub _create_social_meta_tags { | ||||||||||||||||||||
|
Subroutine
_create_social_meta_tags: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 414 | 0 | my ( $self, $target ) = @_; | |||||||||||||||||||||
| 415 | |||||||||||||||||||||||
| 416 | 0 | my $tags = ''; | |||||||||||||||||||||
| 417 | 0 | ▶ | try { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 418 | 0 | $tags = $self->socialmeta->$target->create( $self->socialmeta_mode ); | |||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||
| 420 | 0 | ▶ | catch { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 421 | 0 | warn "Couldn't create $target meta tags for " | |||||||||||||||||||||
| 422 | . $self->source_file->basename | ||||||||||||||||||||||
| 423 | . ": $_\n"; | ||||||||||||||||||||||
| 424 | 0 | }; | |||||||||||||||||||||
| 425 | 0 | return $tags; | |||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||
| 427 | |||||||||||||||||||||||
| 428 | # Normalize CRLF ("Windows") and bare-CR ("classic Mac") newlines to LF, so | ||||||||||||||||||||||
| 429 | # source files publish cleanly and get rewritten with consistent line endings | ||||||||||||||||||||||
| 430 | # regardless of the editor that produced them. | ||||||||||||||||||||||
| 431 | 77 | ▶ | sub _normalize_newlines { | ||||||||||||||||||||
|
Subroutine
_normalize_newlines: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 432 | 77 | my ( $text ) = @_; | |||||||||||||||||||||
| 433 | 77 | $text =~ s/\r\n?/\n/g; | |||||||||||||||||||||
| 434 | 77 | return $text; | |||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||
| 436 | |||||||||||||||||||||||
| 437 | # This next internal method does a bunch of stuff. | ||||||||||||||||||||||
| 438 | # It's called via Moose-trigger when the object's source_file attribute is set. | ||||||||||||||||||||||
| 439 | # * Read and store the file's data (body) and metadata | ||||||||||||||||||||||
| 440 | # * Figure out the publication timestamp, based on possible (not guaranteed!) | ||||||||||||||||||||||
| 441 | # presence of date in the filename AND/OR "time" metadata attribute | ||||||||||||||||||||||
| 442 | # * If the file lacks a various required attributes, rewrite the file so that | ||||||||||||||||||||||
| 443 | # it has them. | ||||||||||||||||||||||
| 444 | 13 | ▶ | sub _process_source_file { | ||||||||||||||||||||
|
Subroutine
_process_source_file: called CC=25 SCAR=36.8
| |||||||||||||||||||||||
| 445 | 13 | my $self = shift; | |||||||||||||||||||||
| 446 | |||||||||||||||||||||||
| 447 | # Slurp the file, storing the title and time metadata, and the body. | ||||||||||||||||||||||
| 448 | 13 | my $fh = $self->source_file->open('<:encoding(utf8)'); | |||||||||||||||||||||
| 449 | 13 | my %attributes; | |||||||||||||||||||||
| 450 | 13 | my @ordered_attribute_names = qw( title time published_filename guid tags); | |||||||||||||||||||||
| 451 | 13 | ▶ | while ( my $line = <$fh> ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 452 | 34 | $line = _normalize_newlines( $line ); | |||||||||||||||||||||
| 453 | 34 | chomp $line; | |||||||||||||||||||||
| 454 | 34 | ▶ | last unless $line =~ /\S/; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 455 | 21 | my ($key, $value) = $line =~ /^\s*(\w+?)\s*:\s*(.*?)\s*$/; | |||||||||||||||||||||
| 456 | 21 | ▶ | if ( $key ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 457 | 21 | $key = lc $key; | |||||||||||||||||||||
| 458 | 21 | $attributes{ $key } = $value; | |||||||||||||||||||||
| 459 | 21 | ▶ | unless ( grep { $_ eq $key } @ordered_attribute_names ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 460 | 0 | push @ordered_attribute_names, $key; | |||||||||||||||||||||
| 461 | } | ||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||
| 464 | |||||||||||||||||||||||
| 465 | 13 | $self->attributes( \%attributes ); | |||||||||||||||||||||
| 466 | |||||||||||||||||||||||
| 467 | 13 | my $body; | |||||||||||||||||||||
| 468 | 13 | ▶ | while ( my $line = <$fh> ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 469 | 43 | $body .= _normalize_newlines( $line ); | |||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||
| 471 | |||||||||||||||||||||||
| 472 | 13 | close $fh; | |||||||||||||||||||||
| 473 | |||||||||||||||||||||||
| 474 | 13 | ▶ | if ( $attributes{ title } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 475 | 13 | $self->title( $attributes{ title } ); | |||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||
| 477 | else { | ||||||||||||||||||||||
| 478 | 0 | die 'Error processing ' . $self->source_file . ': ' | |||||||||||||||||||||
| 479 | . 'File content does not define a post title.' | ||||||||||||||||||||||
| 480 | ; | ||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||
| 482 | 13 | $self->body( $body ); | |||||||||||||||||||||
| 483 | |||||||||||||||||||||||
| 484 | 13 | foreach my $field ( qw( title body ) ) { | |||||||||||||||||||||
| 485 | 26 | ▶ | next unless defined $self->$field; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 486 | 26 | $self->$field( | |||||||||||||||||||||
| 487 | Plerd::SmartyPants::process( markdown( $self->$field ) ) | ||||||||||||||||||||||
| 488 | ); | ||||||||||||||||||||||
| 489 | } | ||||||||||||||||||||||
| 490 | |||||||||||||||||||||||
| 491 | # Strip unnecessary <p> tags that the markdown processor just added to the title. | ||||||||||||||||||||||
| 492 | 13 | my $stripped_title = $self->title; | |||||||||||||||||||||
| 493 | 13 | $stripped_title =~ s{</?p>\s*}{}g; | |||||||||||||||||||||
| 494 | 13 | $self->title( $stripped_title ); | |||||||||||||||||||||
| 495 | |||||||||||||||||||||||
| 496 | # Check and tune attributes used to render social-media metatags. | ||||||||||||||||||||||
| 497 | 13 | ▶ | if ( $attributes{ description } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 498 | 0 | $self->description( $attributes{ description } ); | |||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||
| 500 | else { | ||||||||||||||||||||||
| 501 | 13 | my $body = $self->stripped_body; | |||||||||||||||||||||
| 502 | 13 | my ( $description ) = $body =~ /^\s*(.*)\n/; | |||||||||||||||||||||
| 503 | 13 | ▶ | $self->description( $description || '' ); | ||||||||||||||||||||
|
Condition50%1/2
$description || ''
MC/DC0%0/1
$description || ''
$description
Truth table50%1/2
A
A= $description
| |||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||
| 505 | |||||||||||||||||||||||
| 506 | 13 | ▶ | if ( $attributes{ image } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 507 | 0 | $self->image( URI->new( $attributes{ image } ) ); | |||||||||||||||||||||
| 508 | 0 | ▶ | $self->image_alt( $attributes{ image_alt } || '' ); | ||||||||||||||||||||
|
Condition0%0/2
$attributes{'image_alt'} || ''
MC/DC0%0/1
$attributes{'image_alt'} || ''
$attributes{'image_alt'}
Truth table0%0/2
A
A= $attributes{'image_alt'}
| |||||||||||||||||||||||
| 509 | 0 | $self->socialmeta_mode( 'featured_image' ); | |||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||
| 511 | else { | ||||||||||||||||||||||
| 512 | 13 | $self->image( $self->plerd->image ); | |||||||||||||||||||||
| 513 | 13 | ▶ | $self->image_alt( $self->plerd->image_alt || '' ); | ||||||||||||||||||||
|
Condition50%1/2
$self->plerd->image_alt || ''
MC/DC0%0/1
$self->plerd->image_alt || ''
$self->plerd->image_alt
Truth table50%1/2
A
A= $self->plerd->image_alt
| |||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||
| 515 | |||||||||||||||||||||||
| 516 | # Note whether the filename asserts the post's publication date. | ||||||||||||||||||||||
| 517 | 13 | my ( $filename_year, $filename_month, $filename_day ) = | |||||||||||||||||||||
| 518 | $self->source_file->basename =~ /^(\d\d\d\d)-(\d\d)-(\d\d)/; | ||||||||||||||||||||||
| 519 | |||||||||||||||||||||||
| 520 | # Set the post's date, using these rules: | ||||||||||||||||||||||
| 521 | # * If the post has a time attribute in W3 format, use that | ||||||||||||||||||||||
| 522 | # * Elsif the post's filename asserts a date, use midnight of that date, | ||||||||||||||||||||||
| 523 | # and also add a time attribute to the file. | ||||||||||||||||||||||
| 524 | # * Else use right now, and also add a time attribute to the file. | ||||||||||||||||||||||
| 525 | 13 | my $attributes_need_to_be_written_out = 0; | |||||||||||||||||||||
| 526 | 13 | ▶ | if ( $attributes{ time } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 527 | 6 | eval { | |||||||||||||||||||||
| 528 | $self->date( | ||||||||||||||||||||||
| 529 | $self->plerd->datetime_formatter->parse_datetime( $attributes{ time } ) | ||||||||||||||||||||||
| 530 | 6 | ); | |||||||||||||||||||||
| 531 | 6 | $self->date->set_time_zone( 'local' ); | |||||||||||||||||||||
| 532 | }; | ||||||||||||||||||||||
| 533 | 6 | ▶ | unless ( $self->date ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 534 | 0 | die 'Error processing ' . $self->source_file . ': ' | |||||||||||||||||||||
| 535 | . 'The "time" attribute is not in W3C format.' | ||||||||||||||||||||||
| 536 | ; | ||||||||||||||||||||||
| 537 | } | ||||||||||||||||||||||
| 538 | } | ||||||||||||||||||||||
| 539 | else { | ||||||||||||||||||||||
| 540 | 7 | my $publication_dt; | |||||||||||||||||||||
| 541 | |||||||||||||||||||||||
| 542 | 7 | ▶ | if ( $filename_year ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 543 | # The post specifies its day in the filename, but we still don't have a | ||||||||||||||||||||||
| 544 | # publication hour. | ||||||||||||||||||||||
| 545 | # If the filename's date is today (locally), use the current time. | ||||||||||||||||||||||
| 546 | # Otherwise, use midnight of the provided date. | ||||||||||||||||||||||
| 547 | 7 | my $now = DateTime->now( time_zone => 'local' ); | |||||||||||||||||||||
| 548 | 0 | my $ymd = $now->ymd( q{-} ); | |||||||||||||||||||||
| 549 | 0 | ▶ | if ( $self->source_file->basename =~ /^$ymd/ ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 550 | 0 | $publication_dt = $now; | |||||||||||||||||||||
| 551 | } | ||||||||||||||||||||||
| 552 | else { | ||||||||||||||||||||||
| 553 | 0 | $publication_dt = DateTime->new( | |||||||||||||||||||||
| 554 | year => $filename_year, | ||||||||||||||||||||||
| 555 | month => $filename_month, | ||||||||||||||||||||||
| 556 | day => $filename_day, | ||||||||||||||||||||||
| 557 | time_zone => 'local', | ||||||||||||||||||||||
| 558 | ); | ||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||
| 561 | else { | ||||||||||||||||||||||
| 562 | # The file doesn't name the time, *and* the file doesn't contain the date | ||||||||||||||||||||||
| 563 | # in metadata (or else we wouldn't be here), so we'll just use right-now. | ||||||||||||||||||||||
| 564 | 0 | $publication_dt = DateTime->now( time_zone => 'local' ); | |||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||
| 566 | |||||||||||||||||||||||
| 567 | 0 | $self->date( $publication_dt ); | |||||||||||||||||||||
| 568 | |||||||||||||||||||||||
| 569 | 0 | my $date_string = | |||||||||||||||||||||
| 570 | $self->plerd->datetime_formatter->format_datetime( $publication_dt ); | ||||||||||||||||||||||
| 571 | |||||||||||||||||||||||
| 572 | 0 | $attributes{ time } = $date_string; | |||||||||||||||||||||
| 573 | 0 | $attributes_need_to_be_written_out = 1; | |||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||
| 575 | |||||||||||||||||||||||
| 576 | 6 | ▶ | if ( $attributes{ published_filename } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 577 | 0 | $self->published_filename( $attributes{ published_filename } ); | |||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||
| 579 | else { | ||||||||||||||||||||||
| 580 | 6 | $attributes{ published_filename } = $self->published_filename; | |||||||||||||||||||||
| 581 | 6 | $attributes_need_to_be_written_out = 1; | |||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||
| 583 | |||||||||||||||||||||||
| 584 | 6 | ▶ | if ( $attributes{ guid } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 585 | 0 | $self->guid( Data::GUID->from_string( $attributes{ guid } ) ); | |||||||||||||||||||||
| 586 | } | ||||||||||||||||||||||
| 587 | else { | ||||||||||||||||||||||
| 588 | 6 | $attributes{ guid } = Data::GUID->new; | |||||||||||||||||||||
| 589 | 6 | $self->guid( $attributes{ guid } ); | |||||||||||||||||||||
| 590 | 6 | $attributes_need_to_be_written_out = 1; | |||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||
| 592 | |||||||||||||||||||||||
| 593 | 6 | ▶ | if ( $attributes{ tags } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 594 | 1 | my @tag_names = split /\s*,\s*/, $attributes{ tags }; | |||||||||||||||||||||
| 595 | 1 | for my $tag_name (@tag_names) { | |||||||||||||||||||||
| 596 | 2 | my $tag = $self->plerd->tag_named( $tag_name ); | |||||||||||||||||||||
| 597 | 2 | $tag->add_post( $self ); | |||||||||||||||||||||
| 598 | 2 | push @{ $self->tag_objects }, $tag; | |||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||
| 601 | |||||||||||||||||||||||
| 602 | 6 | ▶ | if ( $attributes_need_to_be_written_out ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 603 | 6 | my $new_content = ''; | |||||||||||||||||||||
| 604 | 6 | for my $attribute_name ( @ordered_attribute_names ) { | |||||||||||||||||||||
| 605 | 30 | ▶ | if (defined $attributes{ $attribute_name } ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 606 | 25 | $new_content .= "$attribute_name: $attributes{ $attribute_name }\n"; | |||||||||||||||||||||
| 607 | } | ||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||
| 609 | 6 | $new_content .= "\n$body\n"; | |||||||||||||||||||||
| 610 | 6 | $self->source_file->spew( iomode=>'>:encoding(utf8)', $new_content ); | |||||||||||||||||||||
| 611 | } | ||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||
| 613 | |||||||||||||||||||||||
| 614 | 3 | ▶ | sub publish { | ||||||||||||||||||||
|
Subroutine
publish: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 615 | 3 | my $self = shift; | |||||||||||||||||||||
| 616 | |||||||||||||||||||||||
| 617 | # Make <title>-ready text free of possible Markdown-generated HTML tags. | ||||||||||||||||||||||
| 618 | 3 | my $stripped_title = $self->title; | |||||||||||||||||||||
| 619 | 3 | $stripped_title =~ s{</?(em|strong)>}{}g; | |||||||||||||||||||||
| 620 | |||||||||||||||||||||||
| 621 | 3 | $self->plerd->_publish_template_to_file( | |||||||||||||||||||||
| 622 | $self->plerd->post_template_file, | ||||||||||||||||||||||
| 623 | { | ||||||||||||||||||||||
| 624 | plerd => $self->plerd, | ||||||||||||||||||||||
| 625 | posts => [ $self ], | ||||||||||||||||||||||
| 626 | title => $stripped_title, | ||||||||||||||||||||||
| 627 | context_post => $self, | ||||||||||||||||||||||
| 628 | }, | ||||||||||||||||||||||
| 629 | $self->publication_file, | ||||||||||||||||||||||
| 630 | ); | ||||||||||||||||||||||
| 631 | |||||||||||||||||||||||
| 632 | } | ||||||||||||||||||||||
| 633 | |||||||||||||||||||||||
| 634 | 0 | ▶ | sub send_webmentions { | ||||||||||||||||||||
|
Subroutine
send_webmentions: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||
| 635 | 0 | my $self = shift; | |||||||||||||||||||||
| 636 | |||||||||||||||||||||||
| 637 | 0 | my @wms = Web::Mention->new_from_html( | |||||||||||||||||||||
| 638 | source => $self->uri, | ||||||||||||||||||||||
| 639 | html => $self->body, | ||||||||||||||||||||||
| 640 | ); | ||||||||||||||||||||||
| 641 | |||||||||||||||||||||||
| 642 | 0 | my %report = ( | |||||||||||||||||||||
| 643 | attempts => 0, | ||||||||||||||||||||||
| 644 | delivered => 0, | ||||||||||||||||||||||
| 645 | sent => 0, | ||||||||||||||||||||||
| 646 | ); | ||||||||||||||||||||||
| 647 | 0 | foreach ( @wms ) { | |||||||||||||||||||||
| 648 | 0 | $report{attempts}++; | |||||||||||||||||||||
| 649 | 0 | ▶ | if ( $_->send ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 650 | 0 | $report{delivered}++; | |||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||
| 652 | 0 | ▶ | if ( $_->endpoint ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 653 | 0 | $report{sent}++; | |||||||||||||||||||||
| 654 | } | ||||||||||||||||||||||
| 655 | } | ||||||||||||||||||||||
| 656 | |||||||||||||||||||||||
| 657 | 0 | return (\%report); | |||||||||||||||||||||
| 658 | } | ||||||||||||||||||||||
| 659 | |||||||||||||||||||||||
| 660 | 6 | ▶ | sub _build_utc_date { | ||||||||||||||||||||
|
Subroutine
_build_utc_date: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 661 | 6 | my $self = shift; | |||||||||||||||||||||
| 662 | |||||||||||||||||||||||
| 663 | 6 | my $dt = $self->date->clone; | |||||||||||||||||||||
| 664 | 6 | $dt->set_time_zone( 'UTC' ); | |||||||||||||||||||||
| 665 | 6 | return $dt; | |||||||||||||||||||||
| 666 | } | ||||||||||||||||||||||
| 667 | |||||||||||||||||||||||
| 668 | 8 | ▶ | sub tags { | ||||||||||||||||||||
|
Subroutine
tags: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 669 | 8 | my $self = shift; | |||||||||||||||||||||
| 670 | |||||||||||||||||||||||
| 671 | 8 | return [ map { $_->name } @{ $self->tag_objects } ]; | |||||||||||||||||||||
| 672 | } | ||||||||||||||||||||||
| 673 | |||||||||||||||||||||||
| 674 | 13 | 1; | |||||||||||||||||||||
| 675 | |||||||||||||||||||||||
| 676 | =head1 NAME | ||||||||||||||||||||||
| 677 | |||||||||||||||||||||||
| 678 | Plerd::Post - A Plerd blog post | ||||||||||||||||||||||
| 679 | |||||||||||||||||||||||
| 680 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 681 | |||||||||||||||||||||||
| 682 | An object of the class Plerd::Post represents a single post to a | ||||||||||||||||||||||
| 683 | Plerd-based blog, with Markdown source and HTML output. | ||||||||||||||||||||||
| 684 | |||||||||||||||||||||||
| 685 | =head1 CLASS METHODS | ||||||||||||||||||||||
| 686 | |||||||||||||||||||||||
| 687 | =over | ||||||||||||||||||||||
| 688 | |||||||||||||||||||||||
| 689 | =item new( \%config ) | ||||||||||||||||||||||
| 690 | |||||||||||||||||||||||
| 691 | Object constructor. The single config hashref I<must> include the | ||||||||||||||||||||||
| 692 | following keys: | ||||||||||||||||||||||
| 693 | |||||||||||||||||||||||
| 694 | =over | ||||||||||||||||||||||
| 695 | |||||||||||||||||||||||
| 696 | =item plerd | ||||||||||||||||||||||
| 697 | |||||||||||||||||||||||
| 698 | The parent Plerd object. | ||||||||||||||||||||||
| 699 | |||||||||||||||||||||||
| 700 | =item source_file | ||||||||||||||||||||||
| 701 | |||||||||||||||||||||||
| 702 | A Path::Class::File object representing this post's Markdown source | ||||||||||||||||||||||
| 703 | file. | ||||||||||||||||||||||
| 704 | |||||||||||||||||||||||
| 705 | =back | ||||||||||||||||||||||
| 706 | |||||||||||||||||||||||
| 707 | =back | ||||||||||||||||||||||
| 708 | |||||||||||||||||||||||
| 709 | =head1 OBJECT ATTRIBUTES | ||||||||||||||||||||||
| 710 | |||||||||||||||||||||||
| 711 | =head2 Read-only attributes | ||||||||||||||||||||||
| 712 | |||||||||||||||||||||||
| 713 | =over | ||||||||||||||||||||||
| 714 | |||||||||||||||||||||||
| 715 | =item newer_post | ||||||||||||||||||||||
| 716 | |||||||||||||||||||||||
| 717 | A Plerd::Post object representing the next-newer post to the blog. | ||||||||||||||||||||||
| 718 | |||||||||||||||||||||||
| 719 | Is the current object represents the newest post in the blog, then this | ||||||||||||||||||||||
| 720 | method returns undef. | ||||||||||||||||||||||
| 721 | |||||||||||||||||||||||
| 722 | =item older_post | ||||||||||||||||||||||
| 723 | |||||||||||||||||||||||
| 724 | A Plerd::Post object representing the next-older post to the blog. | ||||||||||||||||||||||
| 725 | |||||||||||||||||||||||
| 726 | Is the current object represents the oldest post in the blog, then this | ||||||||||||||||||||||
| 727 | method returns undef. | ||||||||||||||||||||||
| 728 | |||||||||||||||||||||||
| 729 | =item published_filename | ||||||||||||||||||||||
| 730 | |||||||||||||||||||||||
| 731 | The local filename (without parent directory path) of the HTML file that | ||||||||||||||||||||||
| 732 | this post will generate upon publication. | ||||||||||||||||||||||
| 733 | |||||||||||||||||||||||
| 734 | =item published_timestamp | ||||||||||||||||||||||
| 735 | |||||||||||||||||||||||
| 736 | This post's date, in W3C format, set to midnight in the local timezone. | ||||||||||||||||||||||
| 737 | |||||||||||||||||||||||
| 738 | =item reading_time | ||||||||||||||||||||||
| 739 | |||||||||||||||||||||||
| 740 | An estimated reading-time for this post, measured in whole minutes, and | ||||||||||||||||||||||
| 741 | based on an assumed (and fairly conservative) reading pace of 200 words | ||||||||||||||||||||||
| 742 | per minute. | ||||||||||||||||||||||
| 743 | |||||||||||||||||||||||
| 744 | =item updated_timestamp | ||||||||||||||||||||||
| 745 | |||||||||||||||||||||||
| 746 | The modification time of this this post's source file, in W3C format, | ||||||||||||||||||||||
| 747 | set to the local timezone. | ||||||||||||||||||||||
| 748 | |||||||||||||||||||||||
| 749 | =item uri | ||||||||||||||||||||||
| 750 | |||||||||||||||||||||||
| 751 | The L<URI> of the of the HTML file that this post will generate upon | ||||||||||||||||||||||
| 752 | publication. | ||||||||||||||||||||||
| 753 | |||||||||||||||||||||||
| 754 | =item utc_date | ||||||||||||||||||||||
| 755 | |||||||||||||||||||||||
| 756 | Returns the value of C<date> (see below), with the time zone set to UTC. | ||||||||||||||||||||||
| 757 | |||||||||||||||||||||||
| 758 | =back | ||||||||||||||||||||||
| 759 | |||||||||||||||||||||||
| 760 | =head2 Read-write attributes | ||||||||||||||||||||||
| 761 | |||||||||||||||||||||||
| 762 | =over | ||||||||||||||||||||||
| 763 | |||||||||||||||||||||||
| 764 | =item attributes | ||||||||||||||||||||||
| 765 | |||||||||||||||||||||||
| 766 | A hashref of all the attributes defined in the source document's | ||||||||||||||||||||||
| 767 | metadata section, whether or not Plerd takes any special meaning from | ||||||||||||||||||||||
| 768 | them. | ||||||||||||||||||||||
| 769 | |||||||||||||||||||||||
| 770 | For example, if a source document defines both C<title> and | ||||||||||||||||||||||
| 771 | C<favorite_color> key-value pairs in its metadata, both keys and values | ||||||||||||||||||||||
| 772 | will appear in this hashref, even though Plerd pays no mind to the | ||||||||||||||||||||||
| 773 | latter key. | ||||||||||||||||||||||
| 774 | |||||||||||||||||||||||
| 775 | =item body | ||||||||||||||||||||||
| 776 | |||||||||||||||||||||||
| 777 | String representing the post's body text. | ||||||||||||||||||||||
| 778 | |||||||||||||||||||||||
| 779 | =item date | ||||||||||||||||||||||
| 780 | |||||||||||||||||||||||
| 781 | L<DateTime> object representing this post's presented publication date. | ||||||||||||||||||||||
| 782 | |||||||||||||||||||||||
| 783 | Plerd usually sets this for you, based on the post's metadata, and sets | ||||||||||||||||||||||
| 784 | the time zone to local. If you'd like the object in UTC time instead, | ||||||||||||||||||||||
| 785 | use the C<utc_date> attribute. | ||||||||||||||||||||||
| 786 | |||||||||||||||||||||||
| 787 | =item description | ||||||||||||||||||||||
| 788 | |||||||||||||||||||||||
| 789 | String representing a short, descriptive summary of this post. This | ||||||||||||||||||||||
| 790 | value affects the metadata attached to this post, for use by social | ||||||||||||||||||||||
| 791 | media and such. | ||||||||||||||||||||||
| 792 | |||||||||||||||||||||||
| 793 | If you don't set this value yourself by the time Plerd needs it, then it | ||||||||||||||||||||||
| 794 | will set it to the first paragraph of the post's body text (with all | ||||||||||||||||||||||
| 795 | markup removed). | ||||||||||||||||||||||
| 796 | |||||||||||||||||||||||
| 797 | =item image | ||||||||||||||||||||||
| 798 | |||||||||||||||||||||||
| 799 | (Optional) L<URI> object referencing an illustrative image for this | ||||||||||||||||||||||
| 800 | post. | ||||||||||||||||||||||
| 801 | |||||||||||||||||||||||
| 802 | Setting this value affects the metadata attached to this post, for use | ||||||||||||||||||||||
| 803 | by social media and such. | ||||||||||||||||||||||
| 804 | |||||||||||||||||||||||
| 805 | =item image_alt | ||||||||||||||||||||||
| 806 | |||||||||||||||||||||||
| 807 | (Optional) A text description of the image referenced by the C<image> | ||||||||||||||||||||||
| 808 | atribute. | ||||||||||||||||||||||
| 809 | |||||||||||||||||||||||
| 810 | Setting this value affects the metadata attached to this post, for use | ||||||||||||||||||||||
| 811 | by social media and such. | ||||||||||||||||||||||
| 812 | |||||||||||||||||||||||
| 813 | =item title | ||||||||||||||||||||||
| 814 | |||||||||||||||||||||||
| 815 | String representing this post's title. | ||||||||||||||||||||||
| 816 | |||||||||||||||||||||||
| 817 | =item tags | ||||||||||||||||||||||
| 818 | |||||||||||||||||||||||
| 819 | An array reference to the list of tags (as plain-text strings) | ||||||||||||||||||||||
| 820 | associated with this post as set in the source file using the 'tags:' | ||||||||||||||||||||||
| 821 | header. | ||||||||||||||||||||||
| 822 | |||||||||||||||||||||||
| 823 | =item tag_objects | ||||||||||||||||||||||
| 824 | |||||||||||||||||||||||
| 825 | An array reference to the list of tags (as L<Plerd::Tag> objects) | ||||||||||||||||||||||
| 826 | associated with this post as set in the source file using the 'tags:' | ||||||||||||||||||||||
| 827 | header. | ||||||||||||||||||||||
| 828 | |||||||||||||||||||||||
| 829 | =back | ||||||||||||||||||||||
| 830 | |||||||||||||||||||||||
| 831 | =head1 OBJECT METHODS | ||||||||||||||||||||||
| 832 | |||||||||||||||||||||||
| 833 | =head2 publish | ||||||||||||||||||||||
| 834 | |||||||||||||||||||||||
| 835 | $post->publish | ||||||||||||||||||||||
| 836 | |||||||||||||||||||||||
| 837 | Publishes the post. | ||||||||||||||||||||||
| 838 | |||||||||||||||||||||||
| 839 | =head2 send_webmentions | ||||||||||||||||||||||
| 840 | |||||||||||||||||||||||
| 841 | $report = $post->send_webmentions | ||||||||||||||||||||||
| 842 | |||||||||||||||||||||||
| 843 | Attempts to send a webmention for every hyperlink contained in the post. | ||||||||||||||||||||||
| 844 | |||||||||||||||||||||||
| 845 | The return value is a hashref with the following keys: | ||||||||||||||||||||||
| 846 | |||||||||||||||||||||||
| 847 | =over | ||||||||||||||||||||||
| 848 | |||||||||||||||||||||||
| 849 | =item attempts | ||||||||||||||||||||||
| 850 | |||||||||||||||||||||||
| 851 | The number of webmentions this post attempted to send. | ||||||||||||||||||||||
| 852 | |||||||||||||||||||||||
| 853 | =item sent | ||||||||||||||||||||||
| 854 | |||||||||||||||||||||||
| 855 | The number of webmentions actually sent (due to webmention-endpoint URLs | ||||||||||||||||||||||
| 856 | advertised by the links' targets). | ||||||||||||||||||||||
| 857 | |||||||||||||||||||||||
| 858 | =item delivered | ||||||||||||||||||||||
| 859 | |||||||||||||||||||||||
| 860 | The number of webmentions whose delivery was acknowledged by the | ||||||||||||||||||||||
| 861 | receiving endpoint. | ||||||||||||||||||||||
| 862 | |||||||||||||||||||||||
| 863 | =back | ||||||||||||||||||||||
| 864 | |||||||||||||||||||||||
| 865 | =head1 AUTHOR | ||||||||||||||||||||||
| 866 | |||||||||||||||||||||||
| 867 | Jason McIntosh <jmac@jmac.org> | ||||||||||||||||||||||