| 1 | #============================================================= -*-perl-*- | ||
| 2 | # | ||
| 3 | # Template::Config | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Template Toolkit configuration module. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Andy Wardley <abw@wardley.org> | ||
| 10 | # | ||
| 11 | # COPYRIGHT | ||
| 12 | # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved. | ||
| 13 | # | ||
| 14 | # This module is free software; you can redistribute it and/or | ||
| 15 | # modify it under the same terms as Perl itself. | ||
| 16 | # | ||
| 17 | #======================================================================== | ||
| 18 | |||
| 19 | package Template::Config; | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use base 'Template::Base'; | ||
| 24 | |||
| 25 | our $VERSION = '3.105'; | ||
| 26 | |||
| 27 | our $DEBUG; | ||
| 28 | $DEBUG = 0 unless defined $DEBUG; | ||
| 29 | our $ERROR = ''; | ||
| 30 | our $CONTEXT = 'Template::Context'; | ||
| 31 | our $FILTERS = 'Template::Filters'; | ||
| 32 | our $ITERATOR = 'Template::Iterator'; | ||
| 33 | our $PARSER = 'Template::Parser'; | ||
| 34 | our $PLUGINS = 'Template::Plugins'; | ||
| 35 | our $PROVIDER = 'Template::Provider'; | ||
| 36 | our $SERVICE = 'Template::Service'; | ||
| 37 | our $STASH; | ||
| 38 | $STASH = 'Template::Stash::XS'; | ||
| 39 | our $CONSTANTS = 'Template::Namespace::Constants'; | ||
| 40 | |||
| 41 | our $LATEX_PATH; | ||
| 42 | our $PDFLATEX_PATH; | ||
| 43 | our $DVIPS_PATH; | ||
| 44 | |||
| 45 | our @PRELOAD = ( | ||
| 46 | $CONTEXT, $FILTERS, $ITERATOR, $PARSER, | ||
| 47 | $PLUGINS, $PROVIDER, $SERVICE, $STASH | ||
| 48 | ); | ||
| 49 | |||
| 50 | # the following is set at installation time by the Makefile.PL | ||
| 51 | our $INSTDIR = ''; | ||
| 52 | |||
| 53 | |||
| 54 | #======================================================================== | ||
| 55 | # --- CLASS METHODS --- | ||
| 56 | #======================================================================== | ||
| 57 | |||
| 58 | #------------------------------------------------------------------------ | ||
| 59 | # preload($module, $module, ...) | ||
| 60 | # | ||
| 61 | # Preloads all the standard TT modules that are likely to be used, along | ||
| 62 | # with any other passed as arguments. | ||
| 63 | #------------------------------------------------------------------------ | ||
| 64 | |||
| 65 | sub preload { | ||
| 66 | my $class = shift; | ||
| 67 | |||
| 68 | foreach my $module (@PRELOAD, @_) { | ||
| 69 | $class->load($module) || return; | ||
| 70 | }; | ||
| 71 | return 1; | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | #------------------------------------------------------------------------ | ||
| 76 | # load($module) | ||
| 77 | # | ||
| 78 | # Load a module via require(). Any occurrences of '::' in the module name | ||
| 79 | # are be converted to '/' and '.pm' is appended. Returns 1 on success | ||
| 80 | # or undef on error. Use $class->error() to examine the error string. | ||
| 81 | #------------------------------------------------------------------------ | ||
| 82 | |||
| 83 | sub load { | ||
| 84 | my ($class, $module) = @_; | ||
| 85 | $module =~ s[::][/]g; | ||
| 86 | $module .= '.pm'; | ||
| 87 | return 1 if $INC{$module}; | ||
| 88 | eval { require $module; }; | ||
| 89 | return $@ ? $class->error("failed to load $module: $@") : 1; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | #------------------------------------------------------------------------ | ||
| 94 | # parser(\%params) | ||
| 95 | # | ||
| 96 | # Instantiate a new parser object of the class whose name is denoted by | ||
| 97 | # the package variable $PARSER (default: Template::Parser). Returns | ||
| 98 | # a reference to a newly instantiated parser object or undef on error. | ||
| 99 | # The class error() method can be called without arguments to examine | ||
| 100 | # the error message generated by this failure. | ||
| 101 | #------------------------------------------------------------------------ | ||
| 102 | |||
| 103 | sub parser { | ||
| 104 | my $class = shift; | ||
| 105 | my $params = defined($_[0]) && ref($_[0]) eq 'HASH' | ||
| 106 | ? shift | ||
| 107 | : { @_ }; | ||
| 108 | |||
| 109 | return undef unless $class->load($PARSER); | ||
| 110 | return $PARSER->new($params) | ||
| 111 | || $class->error("failed to create parser: ", $PARSER->error); | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | #------------------------------------------------------------------------ | ||
| 116 | # provider(\%params) | ||
| 117 | # | ||
| 118 | # Instantiate a new template provider object (default: Template::Provider). | ||
| 119 | # Returns an object reference or undef on error, as above. | ||
| 120 | #------------------------------------------------------------------------ | ||
| 121 | |||
| 122 | sub provider { | ||
| 123 | my $class = shift; | ||
| 124 | my $params = defined($_[0]) && ref($_[0]) eq 'HASH' | ||
| 125 | ? shift | ||
| 126 | : { @_ }; | ||
| 127 | |||
| 128 | return undef unless $class->load($PROVIDER); | ||
| 129 | return $PROVIDER->new($params) | ||
| 130 | || $class->error( | ||
| 131 | "failed to create template provider: ", | ||
| 132 | $PROVIDER->error | ||
| 133 | ); | ||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | #------------------------------------------------------------------------ | ||
| 138 | # plugins(\%params) | ||
| 139 | # | ||
| 140 | # Instantiate a new plugins provider object (default: Template::Plugins). | ||
| 141 | # Returns an object reference or undef on error, as above. | ||
| 142 | #------------------------------------------------------------------------ | ||
| 143 | |||
| 144 | sub plugins { | ||
| 145 | my $class = shift; | ||
| 146 | my $params = defined($_[0]) && ref($_[0]) eq 'HASH' | ||
| 147 | ? shift | ||
| 148 | : { @_ }; | ||
| 149 | |||
| 150 | return undef unless $class->load($PLUGINS); | ||
| 151 | return $PLUGINS->new($params) | ||
| 152 | || $class->error( | ||
| 153 | "failed to create plugin provider: ", | ||
| 154 | $PLUGINS->error | ||
| 155 | ); | ||
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | #------------------------------------------------------------------------ | ||
| 160 | # filters(\%params) | ||
| 161 | # | ||
| 162 | # Instantiate a new filters provider object (default: Template::Filters). | ||
| 163 | # Returns an object reference or undef on error, as above. | ||
| 164 | #------------------------------------------------------------------------ | ||
| 165 | |||
| 166 | sub filters { | ||
| 167 | my $class = shift; | ||
| 168 | my $params = defined($_[0]) && ref($_[0]) eq 'HASH' | ||
| 169 | ? shift | ||
| 170 | : { @_ }; | ||
| 171 | |||
| 172 | return undef unless $class->load($FILTERS); | ||
| 173 | return $FILTERS->new($params) | ||
| 174 | || $class->error( | ||
| 175 | "failed to create filter provider: ", | ||
| 176 | $FILTERS->error | ||
| 177 | ); | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | #------------------------------------------------------------------------ | ||
| 182 | # iterator(\@list) | ||
| 183 | # | ||
| 184 | # Instantiate a new Template::Iterator object (default: Template::Iterator). | ||
| 185 | # Returns an object reference or undef on error, as above. | ||
| 186 | #------------------------------------------------------------------------ | ||
| 187 | |||
| 188 | sub iterator { | ||
| 189 | my $class = shift; | ||
| 190 | my $list = shift; | ||
| 191 | |||
| 192 | return undef unless $class->load($ITERATOR); | ||
| 193 | return $ITERATOR->new($list, @_) | ||
| 194 | || $class->error("failed to create iterator: ", $ITERATOR->error); | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | #------------------------------------------------------------------------ | ||
| 199 | # stash(\%vars) | ||
| 200 | # | ||
| 201 | # Instantiate a new template variable stash object (default: | ||
| 202 | # Template::Stash). Returns object or undef, as above. | ||
| 203 | #------------------------------------------------------------------------ | ||
| 204 | |||
| 205 | sub stash { | ||
| 206 | my $class = shift; | ||
| 207 | my $params = defined($_[0]) && ref($_[0]) eq 'HASH' | ||
| 208 | ? shift | ||
| 209 | : { @_ }; | ||
| 210 | |||
| 211 | return undef unless $class->load($STASH); | ||
| 212 | return $STASH->new($params) | ||
| 213 | || $class->error( | ||
| 214 | "failed to create stash: ", | ||
| 215 | $STASH->error | ||
| 216 | ); | ||
| 217 | } | ||
| 218 | |||
| 219 | |||
| 220 | #------------------------------------------------------------------------ | ||
| 221 | # context(\%params) | ||
| 222 | # | ||
| 223 | # Instantiate a new template context object (default: Template::Context). | ||
| 224 | # Returns object or undef, as above. | ||
| 225 | #------------------------------------------------------------------------ | ||
| 226 | |||
| 227 | sub context { | ||
| 228 | my $class = shift; | ||
| 229 | my $params = defined($_[0]) && ref($_[0]) eq 'HASH' | ||
| 230 | ? shift | ||
| 231 | : { @_ }; | ||
| 232 | |||
| 233 | return undef unless $class->load($CONTEXT); | ||
| 234 | return $CONTEXT->new($params) | ||
| 235 | || $class->error( | ||
| 236 | "failed to create context: ", | ||
| 237 | $CONTEXT->error | ||
| 238 | ); | ||
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | #------------------------------------------------------------------------ | ||
| 243 | # service(\%params) | ||
| 244 | # | ||
| 245 | # Instantiate a new template context object (default: Template::Service). | ||
| 246 | # Returns object or undef, as above. | ||
| 247 | #------------------------------------------------------------------------ | ||
| 248 | |||
| 249 | sub service { | ||
| 250 | my $class = shift; | ||
| 251 | my $params = defined($_[0]) && ref($_[0]) eq 'HASH' | ||
| 252 | ? shift | ||
| 253 | : { @_ }; | ||
| 254 | |||
| 255 | return undef unless $class->load($SERVICE); | ||
| 256 | return $SERVICE->new($params) | ||
| 257 | || $class->error( | ||
| 258 | "failed to create context: ", | ||
| 259 | $SERVICE->error | ||
| 260 | ); | ||
| 261 | } | ||
| 262 | |||
| 263 | |||
| 264 | #------------------------------------------------------------------------ | ||
| 265 | # constants(\%params) | ||
| 266 | # | ||
| 267 | # Instantiate a new namespace handler for compile time constant folding | ||
| 268 | # (default: Template::Namespace::Constants). | ||
| 269 | # Returns object or undef, as above. | ||
| 270 | #------------------------------------------------------------------------ | ||
| 271 | |||
| 272 | sub constants { | ||
| 273 | my $class = shift; | ||
| 274 | my $params = defined($_[0]) && ref($_[0]) eq 'HASH' | ||
| 275 | ? shift | ||
| 276 | : { @_ }; | ||
| 277 | |||
| 278 | return undef unless $class->load($CONSTANTS); | ||
| 279 | return $CONSTANTS->new($params) | ||
| 280 | || $class->error( | ||
| 281 | "failed to create constants namespace: ", | ||
| 282 | $CONSTANTS->error | ||
| 283 | ); | ||
| 284 | } | ||
| 285 | |||
| 286 | |||
| 287 | #------------------------------------------------------------------------ | ||
| 288 | # instdir($dir) | ||
| 289 | # | ||
| 290 | # Returns the root installation directory appended with any local | ||
| 291 | # component directory passed as an argument. | ||
| 292 | #------------------------------------------------------------------------ | ||
| 293 | |||
| 294 | sub instdir { | ||
| 295 | my ($class, $dir) = @_; | ||
| 296 | my $inst = $INSTDIR | ||
| 297 | || return $class->error("no installation directory"); | ||
| 298 | chop $inst while substr($inst,-1) eq '/'; | ||
| 299 | $inst .= "/$dir" if $dir; | ||
| 300 | return $inst; | ||
| 301 | } | ||
| 302 | |||
| 303 | |||
| 304 | #======================================================================== | ||
| 305 | # This should probably be moved somewhere else in the long term, but for | ||
| 306 | # now it ensures that Template::TieString is available even if the | ||
| 307 | # Template::Directive module hasn't been loaded, as is the case when | ||
| 308 | # using compiled templates and Template::Parser hasn't yet been loaded | ||
| 309 | # on demand. | ||
| 310 | #======================================================================== | ||
| 311 | |||
| 312 | #------------------------------------------------------------------------ | ||
| 313 | # simple package for tying $output variable to STDOUT, used by perl() | ||
| 314 | #------------------------------------------------------------------------ | ||
| 315 | |||
| 316 | package Template::TieString; | ||
| 317 | |||
| 318 | sub TIEHANDLE { | ||
| 319 | my ($class, $textref) = @_; | ||
| 320 | bless $textref, $class; | ||
| 321 | } | ||
| 322 | sub PRINT { | ||
| 323 | my $self = shift; | ||
| 324 | $$self .= join('', @_); | ||
| 325 | } | ||
| 326 | sub PRINTF { | ||
| 327 | my $self = shift; | ||
| 328 | my $fmt = shift; | ||
| 329 | $$self .= sprintf($fmt, @_); | ||
| 330 | } | ||
| 331 | |||
| 332 | |||
| 333 | |||
| 334 | 1; | ||
| 335 | |||
| 336 | __END__ |