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
|
|
1234
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
96
|
|
23
|
3
|
|
|
3
|
|
14
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
83
|
|
24
|
3
|
|
|
3
|
|
16
|
use base 'Template::Plugin'; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
1750
|
|
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
|
12
|
|
|
12
|
1
|
86
|
my ($class, $context, $base, $args) = @_; |
39
|
12
|
|
100
|
|
|
55
|
$args ||= { }; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
return sub { |
42
|
27
|
100
|
|
27
|
|
396
|
my $newbase = shift unless ref $_[0] eq 'HASH'; |
43
|
27
|
|
100
|
|
|
91
|
my $newargs = shift || { }; |
44
|
27
|
|
|
|
|
96
|
my $combo = { %$args, %$newargs }; |
45
|
32
|
|
|
|
|
82
|
my $urlargs = join($JOINT, |
46
|
32
|
50
|
|
|
|
158
|
map { args($_, $combo->{ $_ }) } |
47
|
27
|
|
|
|
|
116
|
grep { defined $combo->{ $_ } && length $combo->{ $_ } } |
48
|
|
|
|
|
|
|
sort keys %$combo); |
49
|
|
|
|
|
|
|
|
50
|
27
|
|
100
|
|
|
127
|
my $query = $newbase || $base || ''; |
51
|
27
|
100
|
100
|
|
|
122
|
$query .= '?' if length $query && length $urlargs; |
52
|
27
|
100
|
|
|
|
70
|
$query .= $urlargs if length $urlargs; |
53
|
|
|
|
|
|
|
|
54
|
27
|
|
|
|
|
165
|
return $query |
55
|
|
|
|
|
|
|
} |
56
|
12
|
|
|
|
|
116
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub args { |
60
|
32
|
|
|
32
|
0
|
46
|
my ($key, $val) = @_; |
61
|
32
|
|
|
|
|
57
|
$key = escape($key); |
62
|
|
|
|
|
|
|
|
63
|
35
|
|
|
|
|
81
|
return map { |
64
|
32
|
100
|
|
|
|
85
|
"$key=" . escape($_); |
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
|
67
|
|
|
67
|
0
|
81
|
my $toencode = shift; |
78
|
67
|
50
|
|
|
|
130
|
return undef unless defined($toencode); |
79
|
67
|
|
|
|
|
105
|
$toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg; |
|
4
|
|
|
|
|
21
|
|
80
|
67
|
|
|
|
|
206
|
return $toencode; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
1; |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
__END__ |