| 1 | #============================================================= -*-Perl-*- | ||
| 2 | # | ||
| 3 | # Template::Plugin::String | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Template Toolkit plugin to implement a basic String object. | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Andy Wardley <abw@wardley.org> | ||
| 10 | # | ||
| 11 | # COPYRIGHT | ||
| 12 | # Copyright (C) 2001-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::Plugin::String; | ||
| 20 | |||
| 21 | use strict; | ||
| 22 | use warnings; | ||
| 23 | use base 'Template::Plugin'; | ||
| 24 | use Template::Exception; | ||
| 25 | |||
| 26 | use overload q|""| => "text", | ||
| 27 | fallback => 1; | ||
| 28 | |||
| 29 | our $VERSION = '3.105'; | ||
| 30 | our $ERROR = ''; | ||
| 31 | |||
| 32 | *centre = \*center; | ||
| 33 | *append = \*push; | ||
| 34 | *prepend = \*unshift; | ||
| 35 | |||
| 36 | #------------------------------------------------------------------------ | ||
| 37 | |||
| 38 | sub new { | ||
| 39 | my ($class, @args) = @_; | ||
| 40 | my $context = ref $class ? undef : shift(@args); | ||
| 41 | my $config = @args && ref $args[-1] eq 'HASH' ? pop(@args) : { }; | ||
| 42 | |||
| 43 | $class = ref($class) || $class; | ||
| 44 | |||
| 45 | my $text = $config->{ text } // (@args ? shift(@args) : ''); | ||
| 46 | |||
| 47 | # print STDERR "text: [$text]\n"; | ||
| 48 | # print STDERR "class: [$class]\n"; | ||
| 49 | |||
| 50 | my $self = bless { | ||
| 51 | text => $text, | ||
| 52 | filters => [ ], | ||
| 53 | _CONTEXT => $context, | ||
| 54 | }, $class; | ||
| 55 | |||
| 56 | my $filter = $config->{ filter } || $config->{ filters }; | ||
| 57 | |||
| 58 | # install any output filters specified as 'filter' or 'filters' option | ||
| 59 | $self->output_filter($filter) | ||
| 60 | if $filter; | ||
| 61 | |||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | sub text { | ||
| 67 | my $self = shift; | ||
| 68 | return $self->{ text } unless @{ $self->{ filters } }; | ||
| 69 | |||
| 70 | my $text = $self->{ text }; | ||
| 71 | my $context = $self->{ _CONTEXT }; | ||
| 72 | |||
| 73 | foreach my $dispatch (@{ $self->{ filters } }) { | ||
| 74 | my ($name, $args) = @$dispatch; | ||
| 75 | my $code = $context->filter($name, $args) | ||
| 76 | || $self->throw($context->error()); | ||
| 77 | $text = &$code($text); | ||
| 78 | } | ||
| 79 | return $text; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | sub copy { | ||
| 84 | my $self = shift; | ||
| 85 | $self->new($self->{ text }); | ||
| 86 | } | ||
| 87 | |||
| 88 | |||
| 89 | sub throw { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | die (Template::Exception->new('String', join('', @_))); | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | #------------------------------------------------------------------------ | ||
| 97 | # output_filter($filter) | ||
| 98 | # | ||
| 99 | # Install automatic output filter(s) for the string. $filter can a list: | ||
| 100 | # [ 'name1', 'name2' => [ ..args.. ], name4 => { ..args.. } ] or a hash | ||
| 101 | # { name1 => '', name2 => [ args ], name3 => { args } } | ||
| 102 | #------------------------------------------------------------------------ | ||
| 103 | |||
| 104 | sub output_filter { | ||
| 105 | my ($self, $filter) = @_; | ||
| 106 | my ($name, $args, $dispatch); | ||
| 107 | my $filters = $self->{ filters }; | ||
| 108 | my $count = 0; | ||
| 109 | |||
| 110 | if (ref $filter eq 'HASH') { | ||
| 111 | $filter = [ %$filter ]; | ||
| 112 | } | ||
| 113 | elsif (ref $filter ne 'ARRAY') { | ||
| 114 | $filter = [ split(/\s*\W+\s*/, $filter) ]; | ||
| 115 | } | ||
| 116 | |||
| 117 | while (@$filter) { | ||
| 118 | $name = shift @$filter; | ||
| 119 | |||
| 120 | # args may follow as a reference (or empty string, e.g. { foo => '' } | ||
| 121 | if (@$filter && (ref($filter->[0]) || ! length $filter->[0])) { | ||
| 122 | $args = shift @$filter; | ||
| 123 | if ($args) { | ||
| 124 | $args = [ $args ] unless ref $args eq 'ARRAY'; | ||
| 125 | } | ||
| 126 | else { | ||
| 127 | $args = [ ]; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | else { | ||
| 131 | $args = [ ]; | ||
| 132 | } | ||
| 133 | |||
| 134 | # $self->DEBUG("adding output filter $name(@$args)\n"); | ||
| 135 | |||
| 136 | push(@$filters, [ $name, $args ]); | ||
| 137 | $count++; | ||
| 138 | } | ||
| 139 | |||
| 140 | return ''; | ||
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | #------------------------------------------------------------------------ | ||
| 145 | |||
| 146 | sub push { | ||
| 147 | my $self = shift; | ||
| 148 | $self->{ text } .= join('', @_); | ||
| 149 | return $self; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub unshift { | ||
| 154 | my $self = shift; | ||
| 155 | $self->{ text } = join('', @_) . $self->{ text }; | ||
| 156 | return $self; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | sub pop { | ||
| 161 | my $self = shift; | ||
| 162 | my $strip = shift || return $self; | ||
| 163 | $self->{ text } =~ s/$strip$//; | ||
| 164 | return $self; | ||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | sub shift { | ||
| 169 | my $self = shift; | ||
| 170 | my $strip = shift || return $self; | ||
| 171 | $self->{ text } =~ s/^$strip//; | ||
| 172 | return $self; | ||
| 173 | } | ||
| 174 | |||
| 175 | #------------------------------------------------------------------------ | ||
| 176 | |||
| 177 | sub center { | ||
| 178 | my ($self, $width) = @_; | ||
| 179 | my $text = $self->{ text }; | ||
| 180 | my $len = length $text; | ||
| 181 | $width ||= 0; | ||
| 182 | |||
| 183 | if ($len < $width) { | ||
| 184 | my $lpad = int(($width - $len) / 2); | ||
| 185 | my $rpad = $width - $len - $lpad; | ||
| 186 | $self->{ text } = (' ' x $lpad) . $self->{ text } . (' ' x $rpad); | ||
| 187 | } | ||
| 188 | |||
| 189 | return $self; | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | sub left { | ||
| 194 | my ($self, $width) = @_; | ||
| 195 | my $len = length $self->{ text }; | ||
| 196 | $width ||= 0; | ||
| 197 | |||
| 198 | $self->{ text } .= (' ' x ($width - $len)) | ||
| 199 | if $width > $len; | ||
| 200 | |||
| 201 | return $self; | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | sub right { | ||
| 206 | my ($self, $width) = @_; | ||
| 207 | my $len = length $self->{ text }; | ||
| 208 | $width ||= 0; | ||
| 209 | |||
| 210 | $self->{ text } = (' ' x ($width - $len)) . $self->{ text } | ||
| 211 | if $width > $len; | ||
| 212 | |||
| 213 | return $self; | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | sub format { | ||
| 218 | my ($self, $format) = @_; | ||
| 219 | $format //= '%s'; | ||
| 220 | $self->{ text } = sprintf($format, $self->{ text }); | ||
| 221 | return $self; | ||
| 222 | } | ||
| 223 | |||
| 224 | |||
| 225 | sub filter { | ||
| 226 | my ($self, $name, @args) = @_; | ||
| 227 | |||
| 228 | my $context = $self->{ _CONTEXT }; | ||
| 229 | |||
| 230 | my $code = $context->filter($name, \@args) | ||
| 231 | || $self->throw($context->error()); | ||
| 232 | return &$code($self->{ text }); | ||
| 233 | } | ||
| 234 | |||
| 235 | |||
| 236 | #------------------------------------------------------------------------ | ||
| 237 | |||
| 238 | sub upper { | ||
| 239 | my $self = CORE::shift; | ||
| 240 | $self->{ text } = uc $self->{ text }; | ||
| 241 | return $self; | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | sub lower { | ||
| 246 | my $self = CORE::shift; | ||
| 247 | $self->{ text } = lc $self->{ text }; | ||
| 248 | return $self; | ||
| 249 | } | ||
| 250 | |||
| 251 | |||
| 252 | sub capital { | ||
| 253 | my $self = CORE::shift; | ||
| 254 | $self->{ text } =~ s/^(.)/\U$1/; | ||
| 255 | return $self; | ||
| 256 | } | ||
| 257 | |||
| 258 | #------------------------------------------------------------------------ | ||
| 259 | |||
| 260 | sub chop { | ||
| 261 | my $self = CORE::shift; | ||
| 262 | chop $self->{ text }; | ||
| 263 | return $self; | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | sub chomp { | ||
| 268 | my $self = CORE::shift; | ||
| 269 | chomp $self->{ text }; | ||
| 270 | return $self; | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | sub trim { | ||
| 275 | my $self = CORE::shift; | ||
| 276 | for ($self->{ text }) { | ||
| 277 | s/^\s+//; | ||
| 278 | s/\s+$//; | ||
| 279 | } | ||
| 280 | return $self; | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | sub collapse { | ||
| 285 | my $self = CORE::shift; | ||
| 286 | for ($self->{ text }) { | ||
| 287 | s/^\s+//; | ||
| 288 | s/\s+$//; | ||
| 289 | s/\s+/ /g | ||
| 290 | } | ||
| 291 | return $self; | ||
| 292 | |||
| 293 | } | ||
| 294 | |||
| 295 | #------------------------------------------------------------------------ | ||
| 296 | |||
| 297 | sub length { | ||
| 298 | my $self = CORE::shift; | ||
| 299 | return length $self->{ text }; | ||
| 300 | } | ||
| 301 | |||
| 302 | |||
| 303 | sub truncate { | ||
| 304 | my ($self, $length, $suffix) = @_; | ||
| 305 | return $self unless defined $length; | ||
| 306 | $suffix ||= ''; | ||
| 307 | return $self if CORE::length $self->{ text } <= $length; | ||
| 308 | $self->{ text } = CORE::substr($self->{ text }, 0, | ||
| 309 | $length - CORE::length($suffix)) . $suffix; | ||
| 310 | return $self; | ||
| 311 | } | ||
| 312 | |||
| 313 | |||
| 314 | sub substr { | ||
| 315 | my ($self, $offset, $length, $replacement) = @_; | ||
| 316 | $offset ||= 0; | ||
| 317 | |||
| 318 | if(defined $length) { | ||
| 319 | if (defined $replacement) { | ||
| 320 | my $removed = CORE::substr( $self->{text}, $offset, $length ); | ||
| 321 | CORE::substr( $self->{text}, $offset, $length ) = $replacement; | ||
| 322 | return $removed; | ||
| 323 | } | ||
| 324 | else { | ||
| 325 | return CORE::substr( $self->{text}, $offset, $length ); | ||
| 326 | } | ||
| 327 | } | ||
| 328 | else { | ||
| 329 | return CORE::substr( $self->{text}, $offset ); | ||
| 330 | } | ||
| 331 | } | ||
| 332 | |||
| 333 | |||
| 334 | sub repeat { | ||
| 335 | my ($self, $n) = @_; | ||
| 336 | return $self unless defined $n; | ||
| 337 | $self->{ text } = $self->{ text } x $n; | ||
| 338 | return $self; | ||
| 339 | } | ||
| 340 | |||
| 341 | |||
| 342 | sub replace { | ||
| 343 | my ($self, $search, $replace) = @_; | ||
| 344 | return $self unless defined $search; | ||
| 345 | $replace //= ''; | ||
| 346 | my $prev_end = -1; | ||
| 347 | $self->{ text } =~ s{$search}{ | ||
| 348 | my $s = $-[0]; | ||
| 349 | my $e = $+[0]; | ||
| 350 | if ($s == $e && $s == $prev_end) { | ||
| 351 | ''; | ||
| 352 | } else { | ||
| 353 | $prev_end = $e; | ||
| 354 | $replace; | ||
| 355 | } | ||
| 356 | }eg; | ||
| 357 | return $self; | ||
| 358 | } | ||
| 359 | |||
| 360 | |||
| 361 | sub remove { | ||
| 362 | my ($self, $search) = @_; | ||
| 363 | $search //= ''; | ||
| 364 | $self->{ text } =~ s/$search//g; | ||
| 365 | return $self; | ||
| 366 | } | ||
| 367 | |||
| 368 | |||
| 369 | sub split { | ||
| 370 | my $self = CORE::shift; | ||
| 371 | my $split = CORE::shift // '\s+'; | ||
| 372 | my $limit = CORE::shift || 0; | ||
| 373 | |||
| 374 | return [ split($split, $self->{ text }, $limit) ]; | ||
| 375 | } | ||
| 376 | |||
| 377 | |||
| 378 | sub search { | ||
| 379 | my ($self, $pattern) = @_; | ||
| 380 | return $self->{ text } =~ /$pattern/; | ||
| 381 | } | ||
| 382 | |||
| 383 | |||
| 384 | sub equals { | ||
| 385 | my ($self, $comparison) = @_; | ||
| 386 | return $self->{ text } eq $comparison; | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | 1; | ||
| 391 | |||
| 392 | __END__ |