From 08aa87adaf6c59131d01f8a4a078dc4e78475788 Mon Sep 17 00:00:00 2001 From: Frank Lichtenheld Date: Sun, 14 Oct 2007 03:08:22 +0200 Subject: [PATCH] Move common functions for template use to Packages::Template Previously they were duplicated in every caller which was very error prone. --- bin/create_index_pages | 16 ++++------------ bin/parse-debtags-voc | 6 ------ lib/Packages/Dispatcher.pm | 18 +----------------- lib/Packages/Template.pm | 17 +++++++++++++++++ 4 files changed, 22 insertions(+), 35 deletions(-) diff --git a/bin/create_index_pages b/bin/create_index_pages index 7f35b31..0eaf63e 100755 --- a/bin/create_index_pages +++ b/bin/create_index_pages @@ -50,18 +50,10 @@ my $priorities = retrieve "$DBDIR/priorities.info"; #use Data::Dumper; #print STDERR Dumper($sections, $subsections, $priorities); -my (%pages, %tt_vars); - -$tt_vars{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) }; -$tt_vars{make_url} = sub { return &Packages::CGI::make_url(@_) }; -$tt_vars{g} = sub { return &Packages::I18N::Locale::tt_gettext(@_) }; -# needed to work around the limitations of the the FILTER syntax -$tt_vars{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') }; -$tt_vars{uri_escape} = sub { return URI::Escape::uri_escape(@_) }; -$tt_vars{quotemeta} = sub { return quotemeta($_[0]) }; - -my $template = new Packages::Template( "$TOPDIR/templates", 'html', \%tt_vars ); -my $txt_template = new Packages::Template( "$TOPDIR/templates", 'txt', \%tt_vars ); +my (%pages); + +my $template = new Packages::Template( "$TOPDIR/templates", 'html'); +my $txt_template = new Packages::Template( "$TOPDIR/templates", 'txt'); print "write suite index files ...\n"; foreach my $s (@SUITES) { diff --git a/bin/parse-debtags-voc b/bin/parse-debtags-voc index 85649a6..b639156 100755 --- a/bin/parse-debtags-voc +++ b/bin/parse-debtags-voc @@ -111,12 +111,6 @@ foreach (@tags) { my %content = ( vocabulary => \%voc, facets => \@facets, tags => \@tags, tags_by_facet => \%tags_by_facet ); -# needed to work around the limitations of the the FILTER syntax -$content{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') }; -$content{uri_escape} = sub { return URI::Escape::uri_escape(@_) }; -$content{quotemeta} = sub { return quotemeta($_[0]) }; -$content{string2id} = sub { return &Packages::CGI::string2id(@_) }; - print TAGLST $template->page( 'tag_index', \%content ); close TAGLST or warn "Couldn't close tag list: $!"; diff --git a/lib/Packages/Dispatcher.pm b/lib/Packages/Dispatcher.pm index 3ae8004..e5c35d4 100755 --- a/lib/Packages/Dispatcher.pm +++ b/lib/Packages/Dispatcher.pm @@ -25,11 +25,9 @@ use warnings; use CGI; use POSIX; use File::Basename; -use URI; -use URI::Escape; -use HTML::Entities; use Template; use DB_File; +use URI::Escape; use Benchmark ':hireswallclock'; use I18N::AcceptLanguage; use Locale::gettext; @@ -332,20 +330,6 @@ sub do_dispatch { $page_content{opts} = \%opts; $page_content{params} = \%params; - $page_content{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) }; - $page_content{make_url} = sub { return &Packages::CGI::make_url(@_) }; - $page_content{extract_host} = sub { my $uri = URI->new($_[0]); - my $host = $uri->host; - $host .= ':'.$uri->port if $uri->port != $uri->default_port; - return $host; - }; - $page_content{g} = sub { return &Packages::I18N::Locale::tt_gettext(@_) }; - # needed to work around the limitations of the the FILTER syntax - $page_content{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') }; - $page_content{uri_escape} = sub { return URI::Escape::uri_escape(@_) }; - $page_content{quotemeta} = sub { return quotemeta($_[0]) }; - $page_content{string2id} = sub { return &Packages::CGI::string2id(@_) }; - unless (@Packages::CGI::fatal_errors) { print $input->header(-charset => $charset, -type => get_mime($opts{format}) ); #use Data::Dumper; diff --git a/lib/Packages/Template.pm b/lib/Packages/Template.pm index 0fe2481..be71d15 100644 --- a/lib/Packages/Template.pm +++ b/lib/Packages/Template.pm @@ -5,6 +5,9 @@ use warnings; use Template; use Locale::gettext; +use URI (); +use HTML::Entities (); +use URI::Escape (); use Benchmark ':hireswallclock'; use Packages::CGI; @@ -19,6 +22,7 @@ use constant COMPILE => 1; sub new { my ($classname, $include, $format, $vars, $options) = @_; + $vars ||= {}; $options ||= {}; my $self = {}; @@ -29,6 +33,19 @@ sub new { year => $timestamp[5]+1900, string => scalar gmtime() .' UTC', }; + $vars->{make_search_url} = sub { return &Packages::CGI::make_search_url(@_) }; + $vars->{make_url} = sub { return &Packages::CGI::make_url(@_) }; + $vars->{g} = sub { return &Packages::I18N::Locale::tt_gettext(@_) }; + $vars->{extract_host} = sub { my $uri = URI->new($_[0]); + my $host = $uri->host; + $host .= ':'.$uri->port if $uri->port != $uri->default_port; + return $host; + }; + # needed to work around the limitations of the the FILTER syntax + $vars->{html_encode} = sub { return HTML::Entities::encode_entities(@_,'<>&"') }; + $vars->{uri_escape} = sub { return URI::Escape::uri_escape(@_) }; + $vars->{quotemeta} = sub { return quotemeta($_[0]) }; + $vars->{string2id} = sub { return &Packages::CGI::string2id(@_) }; $self->{template} = Template->new( { PRE_PROCESS => [ 'config.tmpl' ], -- 2.39.2