1 package SL::Presenter::Text;
5 use parent qw(Exporter);
7 use Exporter qw(import);
8 our @EXPORT = qw(format_man_days simple_format truncate);
13 my ($self, $text, %params) = @_;
16 $params{at} = 3 if 3 > $params{at};
19 return $text if length($text) < $params{at};
20 return substr($text, 0, $params{at}) . '...';
24 my ($self, $text, %params) = @_;
26 $text = $::locale->quote_special_chars('HTML', $text || '');
28 $text =~ s{\r\n?}{\n}g; # \r\n and \r -> \n
29 $text =~ s{\n\n+}{</p>\n\n<p>}g; # 2+ newline -> paragraph
30 $text =~ s{([^\n]\n)(?=[^\n])}{$1<br />}g; # 1 newline -> br
36 my ($self, $value, %params) = @_;
38 return '---' if $params{skip_zero} && !$value;
40 return $self->escape($::locale->text('#1 h', $::form->format_amount(\%::myconfig, $value, 2))) if 8.0 > $value;
43 my $output = $::locale->text('#1 MD', int($value));
44 my $rest = ($value - int($value)) * 8.0;
45 $output .= ' ' . $::locale->text('#1 h', $::form->format_amount(\%::myconfig, $rest)) if $rest > 0.0;
47 return $self->escape($output);
59 SL::Presenter::Text - Presenter module for assorted text helpers
63 my $long_text = "This is very, very long. Need shorter, surely.";
64 my $truncated = $::request->presenter->truncate($long_text, at => 10);
65 # Result: "This is..."
71 =item C<format_man_days $value, [%params]>
73 C<$value> is interpreted to mean a number of hours (for C<$value> < 8)
74 / man days (if >= 8). Returns a translated, human-readable version of
75 it, e.g. C<2 PT 2 h> for the value C<18> and German.
77 If the parameter C<skip_zero> is trueish then C<---> is returned
78 instead of the normal formatting if C<$value> equals 0.
80 =item C<truncate $text, [%params]>
82 Returns the C<$text> truncated after a certain number of
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.
90 =item C<simple_format $text>
92 Applies simple formatting rules to C<$text>: The text is put into
93 paragraph HTML tags. Two consecutive newlines are interpreted as a
94 paragraph change: they close the current paragraph tag and start a new
95 one. Single newlines are converted to line breaks. Carriage returns
106 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>