| 1 | package Yukki::Web::Plugin::Attachment; | ||
| 2 | { | ||
| 3 | $Yukki::Web::Plugin::Attachment::VERSION = '0.140290'; | ||
| 4 | } | ||
| 5 | use 5.12.1; | ||
| 6 | use Moose; | ||
| 7 | |||
| 8 | extends 'Yukki::Web::Plugin'; | ||
| 9 | |||
| 10 | # ABSTRACT: plugin for attachments | ||
| 11 | |||
| 12 | use URI::Escape qw( uri_escape ); | ||
| 13 | |||
| 14 | |||
| 15 | has format_helpers => ( | ||
| 16 | is => 'ro', | ||
| 17 | isa => 'HashRef[Str]', | ||
| 18 | required => 1, | ||
| 19 | default => sub { +{ | ||
| 20 | 'attachment' => 'attachment_url', | ||
| 21 | } }, | ||
| 22 | ); | ||
| 23 | |||
| 24 | with 'Yukki::Web::Plugin::Role::FormatHelper'; | ||
| 25 | |||
| 26 | |||
| 27 | sub attachment_url { | ||
| 28 | my ($self, $params) = @_; | ||
| 29 | |||
| 30 | my $ctx = $params->{context}; | ||
| 31 | my $file = $params->{file}; | ||
| 32 | my $arg = $params->{arg}; | ||
| 33 | |||
| 34 | if ($arg =~ m{ | ||
| 35 | |||
| 36 | ^\s* | ||
| 37 | |||
| 38 | (?: ([\w]+) : )? # repository: is optional | ||
| 39 | (.+) # link/to/page is mandatory | ||
| 40 | |||
| 41 | \s*$ | ||
| 42 | |||
| 43 | }x) { | ||
| 44 | |||
| 45 | my $repository = $1 // $file->repository_name; | ||
| 46 | my $page = $file->full_path; | ||
| 47 | my $link = $2; | ||
| 48 | |||
| 49 | $link =~ s/^\s+//; $link =~ s/\s+$//; | ||
| 50 | |||
| 51 | $page =~ s{\.yukki$}{}; | ||
| 52 | $link = join "/", map { uri_escape($_) } split m{/}, $link; | ||
| 53 | |||
| 54 | if ($link =~ m{^/}) { | ||
| 55 | return $ctx->rebae_url("attachment/view/$repository$link"); | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | return $ctx->rebase_url("attachment/view/$repository/$page/$link"); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | return; | ||
| 63 | } | ||
| 64 | |||
| 65 | 1; | ||
| 66 | |||
| 67 | __END__ |