Skip to content

Commit

Permalink
Improving UTF-8 handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacdotorg committed Feb 17, 2018
1 parent baf0762 commit 8496f90
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ Once you've configured your blog as described above, you can add these attribute

* __image_alt__: A textual description of the image referenced by the `image` attribute. (Equivalent in usage to the "alt" attribute in an HTML `<img>` tag.) Plerd will just leave this blank, if you don't define it yourself.

## A note about encoding

Plerd assumes that all your source and template files are encoded as UTF-8.

## Support

To report bugs or file pull requests, visit [Plerd's GitHub repository](https://github.com/jmacdotorg/plerd).
Expand Down
16 changes: 8 additions & 8 deletions lib/Plerd.pm
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ sub publish_recent_page {
my $self = shift;

$self->template->process(
$self->post_template_file->openr,
$self->post_template_file->open('<:encoding(utf8)'),
{
plerd => $self,
posts => $self->recent_posts,
title => $self->title,
},
$self->recent_file->openw,
$self->recent_file->open('>:encoding(utf8)'),
);
}

Expand Down Expand Up @@ -250,13 +250,13 @@ sub _publish_feed {
;

$self->template->process(
$self->$template_file_method->openr,
$self->$template_file_method->open('<:encoding(utf8)'),
{
plerd => $self,
posts => $self->recent_posts,
timestamp => $timestamp,
},
$self->$file_method->openw,
$self->$file_method->open('>:encoding(utf8)'),
);
}

Expand All @@ -266,12 +266,12 @@ sub publish_archive_page {
my $posts_ref = $self->posts;

$self->template->process(
$self->archive_template_file->openr,
$self->archive_template_file->open('<:encoding(utf8)'),
{
plerd => $self,
posts => $posts_ref,
},
$self->archive_file->openw,
$self->archive_file->open('>:encoding(utf8)'),
);

}
Expand Down Expand Up @@ -336,9 +336,9 @@ sub _build_template {
$text =~ s/"/\\"/g;
$text =~ s/\n/\\n/g;
return $text;
}
},
},

ENCODING => 'utf8',
} );
}

Expand Down
13 changes: 9 additions & 4 deletions lib/Plerd/Post.pm
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ 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 $fh = $self->source_file->open('<:encoding(utf8)');
my %attributes;
my @ordered_attribute_names = qw( title time published_filename guid );
while ( my $line = <$fh> ) {
Expand Down Expand Up @@ -497,7 +497,7 @@ sub _process_source_file {
$new_content .= "$attribute_name: $attributes{ $attribute_name }\n";
}
$new_content .= "\n$body\n";
$self->source_file->spew( $new_content );
$self->source_file->spew( iomode=>'>:encoding(utf8)', $new_content );
}
}

Expand All @@ -508,15 +508,20 @@ sub publish {
my $stripped_title = $self->title;
$stripped_title =~ s{</?(em|strong)>}{}g;

my $html_fh = $self->publication_file->openw;
my $template_fh = $self->plerd->post_template_file->openr;
foreach( $html_fh, $template_fh ) {
$_->binmode(':utf8');
}
$self->plerd->template->process(
$self->plerd->post_template_file->openr,
$template_fh,
{
plerd => $self->plerd,
posts => [ $self ],
title => $stripped_title,
context_post => $self,
},
$self->publication_file->openw,
$html_fh,
);
}

Expand Down
3 changes: 3 additions & 0 deletions t/source_model/1999-01-02-unicode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title: Subject lines can contain any characters at all. 😺

And so can posts. 🏆

0 comments on commit 8496f90

Please sign in to comment.