| 1 | package Yukki::Web::Plugin::Role::Formatter; | ||
| 2 | $Yukki::Web::Plugin::Role::Formatter::VERSION = '0.991_002'; # TRIAL | ||
| 3 | |||
| 4 | $Yukki::Web::Plugin::Role::Formatter::VERSION = '0.991002';use v5.24; | ||
| 5 | use utf8; | ||
| 6 | use Moo::Role; | ||
| 7 | |||
| 8 | # ABSTRACT: interface for HTML formatters | ||
| 9 | |||
| 10 | |||
| 11 | requires qw( html_formatters ); | ||
| 12 | |||
| 13 | |||
| 14 | sub has_format { | ||
| 15 | my ($self, $media_type) = @_; | ||
| 16 | return unless defined $media_type; | ||
| 17 | return defined $self->html_formatters->{$media_type}; | ||
| 18 | } | ||
| 19 | |||
| 20 | |||
| 21 | sub format { | ||
| 22 | my ($self, $params) = @_; | ||
| 23 | |||
| 24 | my $media_type = $params->{file}->media_type; | ||
| 25 | return unless $self->has_format($media_type); | ||
| 26 | |||
| 27 | my $format = $self->html_formatters->{$media_type}; | ||
| 28 | return $self->$format($params); | ||
| 29 | } | ||
| 30 | |||
| 31 | 1; | ||
| 32 | |||
| 33 | __END__ | ||
| 34 | |||
| 35 | =pod | ||
| 36 | |||
| 37 | =encoding UTF-8 | ||
| 38 | |||
| 39 | =head1 NAME | ||
| 40 | |||
| 41 | Yukki::Web::Plugin::Role::Formatter - interface for HTML formatters | ||
| 42 | |||
| 43 | =head1 VERSION | ||
| 44 | |||
| 45 | version 0.991_002 | ||
| 46 | |||
| 47 | =head1 SYNOPSIS | ||
| 48 | |||
| 49 | package MyPlugins::SimpleText; | ||
| 50 | use 5.12.1; | ||
| 51 | use Moo; | ||
| 52 | |||
| 53 | use Types::Standard qw( HashRef Str ); | ||
| 54 | |||
| 55 | extends 'Yukki::Web::Plugin'; | ||
| 56 | |||
| 57 | has html_formatters => ( | ||
| 58 | is => 'ro', | ||
| 59 | isa => HashRef[Str], | ||
| 60 | default => sub { +{ | ||
| 61 | 'text/simple' => 'format_simple', | ||
| 62 | } }, | ||
| 63 | ); | ||
| 64 | |||
| 65 | with 'Yukki::Web::Plugin::Role::Formatter; | ||
| 66 | |||
| 67 | sub format_simple { | ||
| 68 | my ($self, $file) = @_; | ||
| 69 | |||
| 70 | my $html = $file->fetch; | ||
| 71 | $html =~ s/$/<br>/g; | ||
| 72 | |||
| 73 | return [ { title => 'Simple' }, $html ]; | ||
| 74 | } | ||
| 75 | |||
| 76 | =head1 DESCRIPTION | ||
| 77 | |||
| 78 | This role defines the interface for file formatters. The first formatter matching the MIME type for a file will be used to format a page's contents as HTML. | ||
| 79 | |||
| 80 | =head1 REQUIRED METHODS | ||
| 81 | |||
| 82 | =head2 html_formatters | ||
| 83 | |||
| 84 | This must return a reference to a hash mapping MIME-types to method names. | ||
| 85 | |||
| 86 | The methods will be called with a hashref parameter containing the following: | ||
| 87 | |||
| 88 | =over | ||
| 89 | |||
| 90 | =item context | ||
| 91 | |||
| 92 | The current L<Yukki::Web::Context> object. | ||
| 93 | |||
| 94 | =item repository | ||
| 95 | |||
| 96 | The name of the repository this file is in. | ||
| 97 | |||
| 98 | =item page | ||
| 99 | |||
| 100 | The full path to the name of the file being formatted. | ||
| 101 | |||
| 102 | =item media_type | ||
| 103 | |||
| 104 | This is the media type that Yukki has detected for the file. | ||
| 105 | |||
| 106 | =item content | ||
| 107 | |||
| 108 | The body of the page as a string. | ||
| 109 | |||
| 110 | =back | ||
| 111 | |||
| 112 | The method should return an HTML document. | ||
| 113 | |||
| 114 | =head1 METHOD | ||
| 115 | |||
| 116 | =head2 has_format | ||
| 117 | |||
| 118 | my $yes_or_no = $formatter->has_format($media_type); | ||
| 119 | |||
| 120 | Returns true if this formatter plugin has a formatter for the named media type. | ||
| 121 | |||
| 122 | =head2 format | ||
| 123 | |||
| 124 | my $html = $self->format({ | ||
| 125 | context => $ctx, | ||
| 126 | repository => $repository, | ||
| 127 | page => $full_path, | ||
| 128 | media_type => $media_type, | ||
| 129 | content => $content, | ||
| 130 | }); | ||
| 131 | |||
| 132 | Renders the text as HTML. If this plugin cannot format this media type, it | ||
| 133 | returns C<undef>. | ||
| 134 | |||
| 135 | =head1 AUTHOR | ||
| 136 | |||
| 137 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||
| 138 | |||
| 139 | =head1 COPYRIGHT AND LICENSE | ||
| 140 | |||
| 141 | This software is copyright (c) 2017 by Qubling Software LLC. | ||
| 142 | |||
| 143 | This is free software; you can redistribute it and/or modify it under | ||
| 144 | the same terms as the Perl 5 programming language system itself. | ||
| 145 | |||
| 146 | =cut |