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