Merge branch 'b-3.6.1' into mebil
[kivitendo-erp.git] / SL / Presenter / Text.pm
1 package SL::Presenter::Text;
2
3 use strict;
4
5 use SL::Presenter::EscapedText qw(escape);
6 use SL::HTML::Restrict;
7 use SL::HTML::Util;
8
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);
12
13 use Carp;
14
15 my $html_cleaner;
16
17 sub truncate {
18   my ($text, %params) = @_;
19
20   escape(Common::truncate($text, %params));
21 }
22
23 sub simple_format {
24   my ($text, %params) = @_;
25
26   $text =  $::locale->quote_special_chars('HTML', $text || '');
27
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
31
32   return '<p>' . $text;
33 }
34
35 sub format_man_days {
36   my ($value, %params) = @_;
37
38   return '---' if $params{skip_zero} && !$value;
39
40   return escape($::locale->text('#1 h', $::form->format_amount(\%::myconfig, $value, 2))) if 8.0 > $value;
41
42   $value     /= 8.0;
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;
46
47   escape($output);
48 }
49
50 sub restricted_html {
51   my ($value) = @_;
52   $html_cleaner //= SL::HTML::Restrict->create;
53   return $html_cleaner->process($value);
54 }
55
56 sub stripped_html {
57   my ($value) = @_;
58   return SL::HTML::Util::strip($value);
59 }
60
61 1;
62 __END__
63
64 =pod
65
66 =encoding utf8
67
68 =head1 NAME
69
70 SL::Presenter::Text - Presenter module for assorted text helpers
71
72 =head1 SYNOPSIS
73
74   use  SL::Presenter::Text qw(truncate);
75
76   my $long_text = "This is very, very long. Need shorter, surely.";
77   my $truncated = truncate($long_text, at => 10);
78   # Result: "This is..."
79
80 =head1 FUNCTIONS
81
82 =over 4
83
84 =item C<format_man_days $value, [%params]>
85
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.
89
90 If the parameter C<skip_zero> is trueish then C<---> is returned
91 instead of the normal formatting if C<$value> equals 0.
92
93 =item C<truncate $text, %params>
94
95 Returns the C<$text> truncated after a certain number of
96 characters. See L<Common/truncate> for the actual implementation and
97 supported parameters.
98
99 =item C<simple_format $text>
100
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
105 are removed.
106
107 =item C<restricted_html $unsafe_html>
108
109 Returns HTML code stripped from unwanted/unsupported content. This is
110 done via the module L<SL::HTML::Restrict>.
111
112 =item C<stripped_html $html>
113
114 Returns the raw text with all HTML tags and comments stripped. This is
115 done via L<SL::HTML::Util/strip>.
116
117 =back
118
119 =head1 BUGS
120
121 Nothing here yet.
122
123 =head1 AUTHOR
124
125 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
126
127 =cut