line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#============================================================= -*-Perl-*- |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Template::Plugin::URL |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# DESCRIPTION |
6
|
|
|
|
|
|
|
# Template Toolkit Plugin for constructing URL's from a base stem |
7
|
|
|
|
|
|
|
# and adaptable parameters. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# AUTHOR |
10
|
|
|
|
|
|
|
# Andy Wardley |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# COPYRIGHT |
13
|
|
|
|
|
|
|
# Copyright (C) 2000-2007 Andy Wardley. All Rights Reserved. |
14
|
|
|
|
|
|
|
# |
15
|
|
|
|
|
|
|
# This module is free software; you can redistribute it and/or |
16
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
17
|
|
|
|
|
|
|
# |
18
|
|
|
|
|
|
|
#============================================================================ |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
package Template::Plugin::URL; |
21
|
|
|
|
|
|
|
|
22
|
3
|
|
|
3
|
|
1283
|
use strict; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
81
|
|
23
|
3
|
|
|
3
|
|
13
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
86
|
|
24
|
3
|
|
|
3
|
|
11
|
use base 'Template::Plugin'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
1218
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $VERSION = 2.74; |
27
|
|
|
|
|
|
|
our $JOINT = '&'; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
31
|
|
|
|
|
|
|
# new($context, $baseurl, \%url_params) |
32
|
|
|
|
|
|
|
# |
33
|
|
|
|
|
|
|
# Constructor method which returns a sub-routine closure for constructing |
34
|
|
|
|
|
|
|
# complex URL's from a base part and hash of additional parameters. |
35
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub new { |
38
|
13
|
|
|
13
|
1
|
43
|
my ($class, $context, $base, $args) = @_; |
39
|
13
|
|
100
|
|
|
36
|
$args ||= { }; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
return sub { |
42
|
28
|
100
|
|
28
|
|
242
|
my $newbase = shift unless ref $_[0] eq 'HASH'; |
43
|
28
|
|
100
|
|
|
74
|
my $newargs = shift || { }; |
44
|
28
|
|
|
|
|
75
|
my $combo = { %$args, %$newargs }; |
45
|
|
|
|
|
|
|
my $urlargs = join($JOINT, |
46
|
33
|
|
|
|
|
46
|
map { args($_, $combo->{ $_ }) } |
47
|
28
|
50
|
|
|
|
90
|
grep { defined $combo->{ $_ } && length $combo->{ $_ } } |
|
33
|
|
|
|
|
117
|
|
48
|
|
|
|
|
|
|
sort keys %$combo); |
49
|
|
|
|
|
|
|
|
50
|
28
|
|
100
|
|
|
87
|
my $query = $newbase || $base || ''; |
51
|
28
|
100
|
100
|
|
|
92
|
$query .= '?' if length $query && length $urlargs; |
52
|
28
|
100
|
|
|
|
41
|
$query .= $urlargs if length $urlargs; |
53
|
|
|
|
|
|
|
|
54
|
28
|
|
|
|
|
113
|
return $query |
55
|
|
|
|
|
|
|
} |
56
|
13
|
|
|
|
|
100
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub args { |
60
|
33
|
|
|
33
|
0
|
37
|
my ($key, $val) = @_; |
61
|
33
|
|
|
|
|
34
|
$key = escape($key); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return map { |
64
|
33
|
100
|
|
|
|
50
|
"$key=" . escape($_); |
|
36
|
|
|
|
|
43
|
|
65
|
|
|
|
|
|
|
} ref $val eq 'ARRAY' ? @$val : $val; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
70
|
|
|
|
|
|
|
# escape($url) |
71
|
|
|
|
|
|
|
# |
72
|
|
|
|
|
|
|
# URL-encode data. Borrowed with minor modifications from CGI.pm. |
73
|
|
|
|
|
|
|
# Kudos to Lincold Stein. |
74
|
|
|
|
|
|
|
#------------------------------------------------------------------------ |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub escape { |
77
|
69
|
|
|
69
|
0
|
50
|
my $toencode = shift; |
78
|
69
|
50
|
|
|
|
89
|
return undef unless defined($toencode); |
79
|
69
|
|
|
|
|
79
|
utf8::encode($toencode); |
80
|
69
|
|
|
|
|
84
|
$toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg; |
|
7
|
|
|
|
|
24
|
|
81
|
69
|
|
|
|
|
132
|
return $toencode; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__END__ |