A hosting service for Jekyll Blogs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

65 lines
1.2 KiB

package MJB::Backend::Jekyll::MarkdownFile;
use Moo;
use YAML::XS qw( Load Dump );
# File path we are read/write from
has path => (
is => 'ro',
required => 1,
);
has headers => (
is => 'rw',
default => sub { return +{} },
);
has markdown => (
is => 'rw',
);
sub read {
my ( $self ) = @_;
# Ensure any content we alread have is discarded before reading.
$self->markdown( undef );
$self->headers( { } );
open my $lf, "<", $self->path
or die "Failed to open " . $self->path . " for reading: $!";
my $sep_count = 0;
my ( $yaml, $markdown ) = ( undef, undef );
while ( defined( my $line = <$lf> ) ) {
$sep_count++ if $line =~ /^---$/;
if ( $sep_count < 2 ) {
$yaml .= $line;
} else {
$markdown .= $line;
}
}
$self->headers( Load($yaml) );
$self->markdown( $markdown );
return $self;
}
sub write {
my ( $self, $file ) = @_;
$file ||= $self->path;
open my $sf, ">", $file
or die "Failed to open $file for writing: $!";
print $sf Dump($self->headers);
print $sf $self->markdown;
close $sf;
return $self;
}
1;