Revision 6794ddd4
Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt
SL/Common.pm | ||
---|---|---|
46 | 46 |
return "/tmp/kivitendo-tmp-" . unique_id(); |
47 | 47 |
} |
48 | 48 |
|
49 |
sub truncate { |
|
50 |
my ($text, %params) = @_; |
|
51 |
|
|
52 |
$params{at} //= 50; |
|
53 |
$params{at} = 3 if 3 > $params{at}; |
|
54 |
|
|
55 |
$params{strip} //= ''; |
|
56 |
|
|
57 |
$text =~ s/[\r\n]+$//g if $params{strip} =~ m/^(?: 1 | newlines? | full )$/x; |
|
58 |
$text =~ s/[\r\n]+/ /g if $params{strip} =~ m/^(?: newlines? | full )$/x; |
|
59 |
|
|
60 |
return $text if length($text) <= $params{at}; |
|
61 |
return substr($text, 0, $params{at} - 3) . '...'; |
|
62 |
} |
|
63 |
|
|
49 | 64 |
sub retrieve_parts { |
50 | 65 |
$main::lxdebug->enter_sub(); |
51 | 66 |
|
... | ... | |
576 | 591 |
} |
577 | 592 |
|
578 | 593 |
1; |
594 |
__END__ |
|
595 |
|
|
596 |
=pod |
|
597 |
|
|
598 |
=encoding utf8 |
|
599 |
|
|
600 |
=head1 NAME |
|
601 |
|
|
602 |
Common - Common routines used in a lot of places. |
|
603 |
|
|
604 |
=head1 SYNOPSIS |
|
605 |
|
|
606 |
my $short_text = Common::truncate($long_text, at => 10); |
|
607 |
|
|
608 |
=head1 FUNCTIONS |
|
609 |
|
|
610 |
=over 4 |
|
611 |
|
|
612 |
=item C<truncate $text, %params> |
|
613 |
|
|
614 |
Truncates C<$text> at a position and insert an ellipsis if the text is |
|
615 |
longer. The maximum number of characters to return is given with the |
|
616 |
paramter C<at> which defaults to 50. |
|
617 |
|
|
618 |
The optional parameter C<strip> can be used to remove unwanted line |
|
619 |
feed/carriage return characters from the text before truncation. It |
|
620 |
can be set to C<1> (only strip those at the end of C<$text>) or |
|
621 |
C<full> (replace consecutive line feed/carriage return characters in |
|
622 |
the middle by a single space and remove tailing line feed/carriage |
|
623 |
return characters). |
|
624 |
|
|
625 |
=back |
|
626 |
|
|
627 |
=head1 BUGS |
|
628 |
|
|
629 |
Nothing here yet. |
|
630 |
|
|
631 |
=head1 AUTHOR |
|
632 |
|
|
633 |
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>, |
|
634 |
Sven Schöling E<lt>s.schoeling@linet-services.deE<gt> |
|
635 |
|
|
636 |
=cut |
Auch abrufbar als: Unified diff
Text-Funktion "truncate" nach Common verschoben, dokumentiert, getestet