File Coverage

File:lib/Yukki/Web/Plugin/Attachment.pm
Coverage:49.0%

linestmtbrancondsubpodtimecode
1package Yukki::Web::Plugin::Attachment;
2
3
1
1
2992
8
use v5.24;
4
1
1
1
9
8
20
use utf8;
5
1
1
1
29
7
18
use Moo;
6
7
1
1
1
537
3
19
use Types::Standard qw( HashRef Str );
8
9
1
1
1
1237
4
23
use namespace::clean;
10
11extends 'Yukki::Web::Plugin';
12
13# ABSTRACT: plugin for attachments
14
15
1
1
1
428
5
571
use URI::Escape qw( uri_escape );
16
17 - 31
=head1 SYNOPSIS

  {{attachment:main:Path/To/Attachment.pdf}}

=head1 DESCRIPTION

This provides a tool for generating URLs to link to attachments on the current page or from other pages.

=head1 ATTRIBUTES

=head2 format_helpers

Links the "attachment" format helper to L</attachment_url>.

=cut
32
33has format_helpers => (
34    is          => 'ro',
35    isa         => HashRef[Str],
36    required    => 1,
37    default     => sub { +{
38        'attachment' => 'attachment_url',
39    } },
40);
41
42with 'Yukki::Web::Plugin::Role::FormatHelper';
43
44 - 50
=head1 METHODS

=head2 attachment_url

Generates a URL for an attachment path.

=cut
51
52sub attachment_url {
53
0
1
    my ($self, $params) = @_;
54
55
0
    my $ctx  = $params->{context};
56
0
    my $file = $params->{file};
57
0
    my $arg  = $params->{arg};
58
59
0
    if ($arg =~ m{
60
61            ^\s*
62
63                (?: ([\w]+) : )?    # repository: is optional
64                (.+)                # link/to/page is mandatory
65
66            \s*$
67
68            }x) {
69
70
0
        my $repository = $1 // $file->repository_name;
71
0
        my $page       = $file->full_path;
72
0
        my $link       = $2;
73
74
0
0
        $link =~ s/^\s+//; $link =~ s/\s+$//;
75
76
0
        $page =~ s{\.yukki$}{};
77
0
0
        $link = join "/", map { uri_escape($_) } split m{/}, $link;
78
79
0
        if ($link =~ m{^/}) {
80
0
            return $ctx->rebae_url("attachment/view/$repository$link");
81        }
82        else {
83
0
            return $ctx->rebase_url("attachment/view/$repository/$page/$link");
84        }
85    }
86
87
0
    return;
88}
89
901;