| 1 | package Yukki::Web::Plugin::Attachment; | ||
| 2 | $Yukki::Web::Plugin::Attachment::VERSION = '0.991_002'; # TRIAL | ||
| 3 | |||
| 4 | $Yukki::Web::Plugin::Attachment::VERSION = '0.991002';use v5.24; | ||
| 5 | use utf8; | ||
| 6 | use Moo; | ||
| 7 | |||
| 8 | use Types::Standard qw( HashRef Str ); | ||
| 9 | |||
| 10 | use namespace::clean; | ||
| 11 | |||
| 12 | extends 'Yukki::Web::Plugin'; | ||
| 13 | |||
| 14 | # ABSTRACT: plugin for attachments | ||
| 15 | |||
| 16 | use URI::Escape qw( uri_escape ); | ||
| 17 | |||
| 18 | |||
| 19 | has format_helpers => ( | ||
| 20 | is => 'ro', | ||
| 21 | isa => HashRef[Str], | ||
| 22 | required => 1, | ||
| 23 | default => sub { +{ | ||
| 24 | 'attachment' => 'attachment_url', | ||
| 25 | } }, | ||
| 26 | ); | ||
| 27 | |||
| 28 | with 'Yukki::Web::Plugin::Role::FormatHelper'; | ||
| 29 | |||
| 30 | |||
| 31 | sub attachment_url { | ||
| 32 | my ($self, $params) = @_; | ||
| 33 | |||
| 34 | my $ctx = $params->{context}; | ||
| 35 | my $file = $params->{file}; | ||
| 36 | my $arg = $params->{arg}; | ||
| 37 | |||
| 38 | if ($arg =~ m{ | ||
| 39 | |||
| 40 | ^\s* | ||
| 41 | |||
| 42 | (?: ([\w]+) : )? # repository: is optional | ||
| 43 | (.+) # link/to/page is mandatory | ||
| 44 | |||
| 45 | \s*$ | ||
| 46 | |||
| 47 | }x) { | ||
| 48 | |||
| 49 | my $repository = $1 // $file->repository_name; | ||
| 50 | my $page = $file->full_path; | ||
| 51 | my $link = $2; | ||
| 52 | |||
| 53 | $link =~ s/^\s+//; $link =~ s/\s+$//; | ||
| 54 | |||
| 55 | $page =~ s{\.yukki$}{}; | ||
| 56 | $link = join "/", map { uri_escape($_) } split m{/}, $link; | ||
| 57 | |||
| 58 | if ($link =~ m{^/}) { | ||
| 59 | return $ctx->rebae_url("attachment/view/$repository$link"); | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | return $ctx->rebase_url("attachment/view/$repository/$page/$link"); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | 1; | ||
| 70 | |||
| 71 | __END__ | ||
| 72 | |||
| 73 | =pod | ||
| 74 | |||
| 75 | =encoding UTF-8 | ||
| 76 | |||
| 77 | =head1 NAME | ||
| 78 | |||
| 79 | Yukki::Web::Plugin::Attachment - plugin for attachments | ||
| 80 | |||
| 81 | =head1 VERSION | ||
| 82 | |||
| 83 | version 0.991_002 | ||
| 84 | |||
| 85 | =head1 SYNOPSIS | ||
| 86 | |||
| 87 | {{attachment:main:Path/To/Attachment.pdf}} | ||
| 88 | |||
| 89 | =head1 DESCRIPTION | ||
| 90 | |||
| 91 | This provides a tool for generating URLs to link to attachments on the current page or from other pages. | ||
| 92 | |||
| 93 | =head1 ATTRIBUTES | ||
| 94 | |||
| 95 | =head2 format_helpers | ||
| 96 | |||
| 97 | Links the "attachment" format helper to L</attachment_url>. | ||
| 98 | |||
| 99 | =head1 METHODS | ||
| 100 | |||
| 101 | =head2 attachment_url | ||
| 102 | |||
| 103 | Generates a URL for an attachment path. | ||
| 104 | |||
| 105 | =head1 AUTHOR | ||
| 106 | |||
| 107 | Andrew Sterling Hanenkamp <hanenkamp@cpan.org> | ||
| 108 | |||
| 109 | =head1 COPYRIGHT AND LICENSE | ||
| 110 | |||
| 111 | This software is copyright (c) 2017 by Qubling Software LLC. | ||
| 112 | |||
| 113 | This is free software; you can redistribute it and/or modify it under | ||
| 114 | the same terms as the Perl 5 programming language system itself. | ||
| 115 | |||
| 116 | =cut |