| 1 | package File::Serialize::Serializer::Markdown; | ||
| 2 | 7 | our $AUTHORITY = 'cpan:YANICK'; | |
| 3 | #ABSTRACT: Markdown (with frontmatter) serializer for File::Serialize | ||
| 4 | 7 | $File::Serialize::Serializer::Markdown::VERSION = '1.5.0'; | |
| 5 | 7 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 7 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 7 | ▶ | use File::Serialize qw/ deserialize_file serialize_file /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 7 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 7 | with 'File::Serialize::Serializer'; | |
| 11 | |||
| 12 | 66 | ▶ | sub required_modules { return qw//; } |
|
Subroutine
required_modules: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 13 | |||
| 14 | 22 | ▶ | sub extensions { qw/ md markdown / }; |
|
Subroutine
extensions: called CC=1 SCAR=0.0
Pod
documented
| |||
| 15 | |||
| 16 | sub serialize { | ||
| 17 | my( $self, $data, $options ) = @_; | ||
| 18 | |||
| 19 | my $content = delete $data->{_content}; | ||
| 20 | |||
| 21 | my $yaml = ''; | ||
| 22 | |||
| 23 | return $content unless keys %$data; | ||
| 24 | |||
| 25 | serialize_file \$yaml, $data, { format => 'yaml' }; | ||
| 26 | |||
| 27 | return join "---\n", $yaml, $content//''; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | sub deserialize { | ||
| 32 | my( $self, $data, $options ) = @_; | ||
| 33 | |||
| 34 | # remote the potential leading `---` | ||
| 35 | $data =~ s/^---\n//; | ||
| 36 | |||
| 37 | |||
| 38 | return { _content => $data } if $data !~ /^---\n?$/m; | ||
| 39 | |||
| 40 | my( $frontmatter, $content ) = split /^---\n?$/m, $data, 2; | ||
| 41 | |||
| 42 | my $struct = deserialize_file( \$frontmatter, { format => 'yml' } ); | ||
| 43 | |||
| 44 | $struct->{_content} = $content if $content =~ /\S/; | ||
| 45 | |||
| 46 | return $struct; | ||
| 47 | } | ||
| 48 | |||
| 49 | 7 | 1; | |
| 50 | |||
| 51 | __END__ | ||