| 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 <abw@wardley.org> | ||
| 11 | # | ||
| 12 | # COPYRIGHT | ||
| 13 | # Copyright (C) 2000-2022 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 | use strict; | ||
| 23 | use warnings; | ||
| 24 | use base 'Template::Plugin'; | ||
| 25 | |||
| 26 | our $VERSION = '3.105'; | ||
| 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 | my ($class, $context, $base, $args) = @_; | ||
| 39 | $args ||= { }; | ||
| 40 | |||
| 41 | return sub { | ||
| 42 | my $newbase = shift unless ref $_[0] eq 'HASH'; | ||
| 43 | my $newargs = shift || { }; | ||
| 44 | my $combo = { %$args, %$newargs }; | ||
| 45 | my $urlargs = join( | ||
| 46 | $JOINT, | ||
| 47 | map { args($_, $combo->{ $_ }) } | ||
| 48 | grep { defined $combo->{ $_ } } | ||
| 49 | sort keys %$combo | ||
| 50 | ); | ||
| 51 | |||
| 52 | my $query = $newbase || $base || ''; | ||
| 53 | $query .= '?' if length $query && length $urlargs; | ||
| 54 | $query .= $urlargs if length $urlargs; | ||
| 55 | |||
| 56 | return $query | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub args { | ||
| 62 | my ($key, $val) = @_; | ||
| 63 | $key = escape($key); | ||
| 64 | |||
| 65 | return map { | ||
| 66 | "$key=" . escape($_); | ||
| 67 | } ref $val eq 'ARRAY' ? @$val : $val; | ||
| 68 | |||
| 69 | } | ||
| 70 | |||
| 71 | #------------------------------------------------------------------------ | ||
| 72 | # escape($url) | ||
| 73 | # | ||
| 74 | # URL-encode data. Borrowed with minor modifications from CGI.pm. | ||
| 75 | # Kudos to Lincold Stein. | ||
| 76 | #------------------------------------------------------------------------ | ||
| 77 | |||
| 78 | sub escape { | ||
| 79 | my $toencode = shift; | ||
| 80 | return undef unless defined($toencode); | ||
| 81 | utf8::encode($toencode); | ||
| 82 | $toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg; | ||
| 83 | return $toencode; | ||
| 84 | } | ||
| 85 | |||
| 86 | 1; | ||
| 87 | |||
| 88 | __END__ |