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) = @_;
15 return Common::truncate($text, %params);
19 my ($self, $text, %params) = @_;
21 $text = $::locale->quote_special_chars('HTML', $text || '');
23 $text =~ s{\r\n?}{\n}g; # \r\n and \r -> \n
24 $text =~ s{\n\n+}{</p>\n\n<p>}g; # 2+ newline -> paragraph
25 $text =~ s{([^\n]\n)(?=[^\n])}{$1<br />}g; # 1 newline -> br
31 my ($self, $value, %params) = @_;
33 return '---' if $params{skip_zero} && !$value;
35 return $self->escape($::locale->text('#1 h', $::form->format_amount(\%::myconfig, $value, 2))) if 8.0 > $value;
38 my $output = $::locale->text('#1 MD', int($value));
39 my $rest = ($value - int($value)) * 8.0;
40 $output .= ' ' . $::locale->text('#1 h', $::form->format_amount(\%::myconfig, $rest)) if $rest > 0.0;
42 return $self->escape($output);
54 SL::Presenter::Text - Presenter module for assorted text helpers
58 my $long_text = "This is very, very long. Need shorter, surely.";
59 my $truncated = $::request->presenter->truncate($long_text, at => 10);
60 # Result: "This is..."
66 =item C<format_man_days $value, [%params]>
68 C<$value> is interpreted to mean a number of hours (for C<$value> < 8)
69 / man days (if >= 8). Returns a translated, human-readable version of
70 it, e.g. C<2 PT 2 h> for the value C<18> and German.
72 If the parameter C<skip_zero> is trueish then C<---> is returned
73 instead of the normal formatting if C<$value> equals 0.
75 =item C<truncate $text, %params>
77 Returns the C<$text> truncated after a certain number of
78 characters. See L<Common/truncate> for the actual implementation and
81 =item C<simple_format $text>
83 Applies simple formatting rules to C<$text>: The text is put into
84 paragraph HTML tags. Two consecutive newlines are interpreted as a
85 paragraph change: they close the current paragraph tag and start a new
86 one. Single newlines are converted to line breaks. Carriage returns
97 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>