1 package SL::Presenter::Text;
5 use SL::Presenter::EscapedText qw(escape);
6 use SL::HTML::Restrict;
9 use Exporter qw(import);
10 our @EXPORT_OK = qw(format_man_days simple_format truncate restricted_html);
11 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
18 my ($text, %params) = @_;
20 escape(Common::truncate($text, %params));
24 my ($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 ($value, %params) = @_;
38 return '---' if $params{skip_zero} && !$value;
40 return 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;
52 $html_cleaner //= SL::HTML::Restrict->create;
53 return $html_cleaner->process($value);
58 return SL::HTML::Util::strip($value);
70 SL::Presenter::Text - Presenter module for assorted text helpers
74 use SL::Presenter::Text qw(truncate);
76 my $long_text = "This is very, very long. Need shorter, surely.";
77 my $truncated = truncate($long_text, at => 10);
78 # Result: "This is..."
84 =item C<format_man_days $value, [%params]>
86 C<$value> is interpreted to mean a number of hours (for C<$value> < 8)
87 / man days (if >= 8). Returns a translated, human-readable version of
88 it, e.g. C<2 PT 2 h> for the value C<18> and German.
90 If the parameter C<skip_zero> is trueish then C<---> is returned
91 instead of the normal formatting if C<$value> equals 0.
93 =item C<truncate $text, %params>
95 Returns the C<$text> truncated after a certain number of
96 characters. See L<Common/truncate> for the actual implementation and
99 =item C<simple_format $text>
101 Applies simple formatting rules to C<$text>: The text is put into
102 paragraph HTML tags. Two consecutive newlines are interpreted as a
103 paragraph change: they close the current paragraph tag and start a new
104 one. Single newlines are converted to line breaks. Carriage returns
107 =item C<restricted_html $unsafe_html>
109 Returns HTML code stripped from unwanted/unsupported content. This is
110 done via the module L<SL::HTML::Restrict>.
112 =item C<stripped_html $html>
114 Returns the raw text with all HTML tags and comments stripped. This is
115 done via L<SL::HTML::Util/strip>.
125 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>