L/Presenter: Funktion zum Säubern von HTML von unerwünschten Tags
[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
8 use Exporter qw(import);
9 our @EXPORT_OK = qw(format_man_days simple_format truncate restricted_html);
10 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
11
12 use Carp;
13
14 my $html_cleaner;
15
16 sub truncate {
17   my ($text, %params) = @_;
18
19   escape(Common::truncate($text, %params));
20 }
21
22 sub simple_format {
23   my ($text, %params) = @_;
24
25   $text =  $::locale->quote_special_chars('HTML', $text || '');
26
27   $text =~ s{\r\n?}{\n}g;                    # \r\n and \r -> \n
28   $text =~ s{\n\n+}{</p>\n\n<p>}g;           # 2+ newline  -> paragraph
29   $text =~ s{([^\n]\n)(?=[^\n])}{$1<br />}g; # 1 newline   -> br
30
31   return '<p>' . $text;
32 }
33
34 sub format_man_days {
35   my ($value, %params) = @_;
36
37   return '---' if $params{skip_zero} && !$value;
38
39   return escape($::locale->text('#1 h', $::form->format_amount(\%::myconfig, $value, 2))) if 8.0 > $value;
40
41   $value     /= 8.0;
42   my $output  = $::locale->text('#1 MD', int($value));
43   my $rest    = ($value - int($value)) * 8.0;
44   $output    .= ' ' . $::locale->text('#1 h', $::form->format_amount(\%::myconfig, $rest)) if $rest > 0.0;
45
46   escape($output);
47 }
48
49 sub restricted_html {
50   my ($value) = @_;
51   $html_cleaner //= SL::HTML::Restrict->create;
52   return $html_cleaner->process($value);
53 }
54
55 1;
56 __END__
57
58 =pod
59
60 =encoding utf8
61
62 =head1 NAME
63
64 SL::Presenter::Text - Presenter module for assorted text helpers
65
66 =head1 SYNOPSIS
67
68   use  SL::Presenter::Text qw(truncate);
69
70   my $long_text = "This is very, very long. Need shorter, surely.";
71   my $truncated = truncate($long_text, at => 10);
72   # Result: "This is..."
73
74 =head1 FUNCTIONS
75
76 =over 4
77
78 =item C<format_man_days $value, [%params]>
79
80 C<$value> is interpreted to mean a number of hours (for C<$value> < 8)
81 / man days (if >= 8). Returns a translated, human-readable version of
82 it, e.g. C<2 PT 2 h> for the value C<18> and German.
83
84 If the parameter C<skip_zero> is trueish then C<---> is returned
85 instead of the normal formatting if C<$value> equals 0.
86
87 =item C<truncate $text, %params>
88
89 Returns the C<$text> truncated after a certain number of
90 characters. See L<Common/truncate> for the actual implementation and
91 supported parameters.
92
93 =item C<simple_format $text>
94
95 Applies simple formatting rules to C<$text>: The text is put into
96 paragraph HTML tags. Two consecutive newlines are interpreted as a
97 paragraph change: they close the current paragraph tag and start a new
98 one. Single newlines are converted to line breaks. Carriage returns
99 are removed.
100
101 =item C<restricted_html $unsafe_html>
102
103 Returns HTML code stripped from unwanted/unsupported content. This is
104 done via the module L<SL::HTML::Restrict>.
105
106 =back
107
108 =head1 BUGS
109
110 Nothing here yet.
111
112 =head1 AUTHOR
113
114 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
115
116 =cut