| 1 | package Feed::Data::Parser::Text; | ||
| 2 | |||
| 3 | use Moo; | ||
| 4 | extends 'Feed::Data::Parser::Base'; | ||
| 5 | use Compiled::Params::OO qw/cpo/; | ||
| 6 | use Types::Standard qw/Object HashRef Str/; | ||
| 7 | our $validate; | ||
| 8 | BEGIN { | ||
| 9 | $validate = cpo( | ||
| 10 | get_value => [Object, HashRef, Str], | ||
| 11 | ); | ||
| 12 | } | ||
| 13 | |||
| 14 | has '+parser' => ( | ||
| 15 | default => sub { | ||
| 16 | my $self = shift; | ||
| 17 | my $content = $self->content_ref; | ||
| 18 | my (@matches, %match); | ||
| 19 | while ($$content =~ s/([A-Za-z]+)\s*\:(.*)//) { | ||
| 20 | if (defined $match{$1}) { | ||
| 21 | push @matches, {%match}; | ||
| 22 | %match = (); | ||
| 23 | } | ||
| 24 | $match{$1} = $2; | ||
| 25 | } | ||
| 26 | push @matches, {%match}; | ||
| 27 | return { items => \@matches }; | ||
| 28 | }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has '+potential_fields' => ( | ||
| 32 | default => sub { | ||
| 33 | return { | ||
| 34 | title => 'title', | ||
| 35 | description => 'description', | ||
| 36 | date => 'date', | ||
| 37 | author => 'author', | ||
| 38 | category => 'category', | ||
| 39 | permalink => 'permalink', | ||
| 40 | comment => 'comment', | ||
| 41 | link => 'link', | ||
| 42 | content => 'content', | ||
| 43 | image => 'image', | ||
| 44 | }; | ||
| 45 | }, | ||
| 46 | ); | ||
| 47 | |||
| 48 | sub get_value { | ||
| 49 | my ($self, $item, $action) = $validate->get_value->(@_); | ||
| 50 | my $value = $item->{$action}; | ||
| 51 | return $value // ''; | ||
| 52 | } | ||
| 53 | |||
| 54 | 1; # End of Data::Feed |