1 package SL::Presenter::Text;
5 use parent qw(Exporter);
7 use Exporter qw(import);
8 our @EXPORT = qw(simple_format truncate);
13 my ($self, $text, %params) = @_;
16 $params{at} = 3 if 3 > $params{at};
19 return $text if length($text) < $params{at};
20 return substr($text, 0, $params{at}) . '...';
24 my ($self, $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
44 SL::Presenter::Text - Presenter module for assorted text helpers
48 my $long_text = "This is very, very long. Need shorter, surely.";
49 my $truncated = $::request->presenter->truncate($long_text, at => 10);
50 # Result: "This is..."
56 =item C<truncate $text, [%params]>
58 Returns the C<$text> truncated after a certain number of
61 The number of characters to truncate at is determined by the parameter
62 C<at> which defaults to 50. If the text is longer than C<$params{at}>
63 then it will be truncated and postfixed with '...'. Otherwise it will
64 be returned unmodified.
66 =item C<simple_format $text>
68 Applies simple formatting rules to C<$text>: The text is put into
69 paragraph HTML tags. Two consecutive newlines are interpreted as a
70 paragraph change: they close the current paragraph tag and start a new
71 one. Single newlines are converted to line breaks. Carriage returns
82 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>