Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 6794ddd4

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

  • ID 6794ddd488626fce34fb07bb0d5bb4878fa3c02a
  • Vorgänger 9b3133b3
  • Nachfolger 8836016b

Text-Funktion "truncate" nach Common verschoben, dokumentiert, getestet

Unterschiede anzeigen:

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
SL/Presenter/Text.pm
12 12
sub truncate {
13 13
  my ($self, $text, %params) = @_;
14 14

  
15
  $params{at}             ||= 50;
16
  $params{at}               =  3 if 3 > $params{at};
17
  $params{at}              -= 3;
18

  
19
  return $text if length($text) < $params{at};
20
  return substr($text, 0, $params{at}) . '...';
15
  return Common::truncate($text, %params);
21 16
}
22 17

  
23 18
sub simple_format {
......
77 72
If the parameter C<skip_zero> is trueish then C<---> is returned
78 73
instead of the normal formatting if C<$value> equals 0.
79 74

  
80
=item C<truncate $text, [%params]>
75
=item C<truncate $text, %params>
81 76

  
82 77
Returns the C<$text> truncated after a certain number of
83
characters.
84

  
85
The number of characters to truncate at is determined by the parameter
86
C<at> which defaults to 50. If the text is longer than C<$params{at}>
87
then it will be truncated and postfixed with '...'. Otherwise it will
88
be returned unmodified.
78
characters. See L<Common/truncate> for the actual implementation and
79
supported parameters.
89 80

  
90 81
=item C<simple_format $text>
91 82

  
t/common.t
1
use strict;
2

  
3
use Test::More;
4

  
5
use lib 't';
6
use Support::TestSetup;
7

  
8
Support::TestSetup::login();
9

  
10
use SL::Common;
11

  
12
sub test_truncate {
13
  is(Common::truncate('nothing to do', at => -1),  '...',           'truncation length < 0: at least 3');
14
  is(Common::truncate('nothing to do', at => 0),   '...',           'truncation length = 0: at least 3');
15
  is(Common::truncate('nothing to do', at => 1),   '...',           'truncation length = 1: at least 3');
16
  is(Common::truncate('nothing to do', at => 2),   '...',           'truncation length = 2: at least 3');
17
  is(Common::truncate('nothing to do', at => 3),   '...',           'truncation length = 3: at least 3');
18
  is(Common::truncate('nothing to do', at => 4),   'n...',          'truncation length = 4');
19
  is(Common::truncate('nothing to do', at => 9),   'nothin...',     'text length equal to truncation + 4');
20
  is(Common::truncate('nothing to do', at => 10),  'nothing...',    'text length equal to truncation + 3');
21
  is(Common::truncate('nothing to do', at => 11),  'nothing ...',   'text length equal to truncation + 2');
22
  is(Common::truncate('nothing to do', at => 12),  'nothing t...',  'text length equal to truncation + 1');
23
  is(Common::truncate('nothing to do', at => 13),  'nothing to do', 'text length equal to truncation');
24
  is(Common::truncate('nothing to do', at => 14),  'nothing to do', 'text length equal to truncation - 1');
25
  is(Common::truncate('nothing to do', at => 15),  'nothing to do', 'text length equal to truncation - 2');
26
  is(Common::truncate('nothing to do', at => 16),  'nothing to do', 'text length equal to truncation - 3');
27
  is(Common::truncate('nothing to do', at => 200), 'nothing to do', 'text length smaller than truncation');
28

  
29
  is(Common::truncate('012345678901234567890123456789012345678901234567890123456789'), '01234567890123456789012345678901234567890123456...', 'default truncation length of 50');
30

  
31
  # Test stripping
32
  is(Common::truncate("nothing\n\rat\rall\n\n", at => 50, strip => 1), "nothing\n\rat\rall", 'strip = 1, at = 50');
33
  is(Common::truncate("nothing\n\rat\rall\n\n", at => 13, strip => 1), "nothing\n\ra...",    'strip = 1, at = 13');
34

  
35
  is(Common::truncate("nothing\n\rat\rall\n\n", at => 50, strip => 'full'), "nothing at all", 'strip = full, at = 50');
36
  is(Common::truncate("nothing\n\rat\rall\n\n", at => 13, strip => 'full'), "nothing at...",  'strip = full, at = 13');
37

  
38
  is(Common::truncate("nothing\n\rat\rall\n\n", at => 50, strip => 'newlines'), "nothing at all", 'strip = newlines, at = 50');
39
  is(Common::truncate("nothing\n\rat\rall\n\n", at => 13, strip => 'newlines'), "nothing at...",  'strip = newlines, at = 13');
40

  
41
  is(Common::truncate("nothing\n\rat\rall\n\n", at => 50, strip => 'newline'), "nothing at all", 'strip = newline, at = 50');
42
  is(Common::truncate("nothing\n\rat\rall\n\n", at => 13, strip => 'newline'), "nothing at...",  'strip = newline, at = 13');
43
}
44

  
45
test_truncate();
46

  
47
done_testing;
48

  
49
1;

Auch abrufbar als: Unified diff