Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 4f15b8f0

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

  • ID 4f15b8f0ee316711afb118db91f057eec5924d43
  • Vorgänger bc9f12ce
  • Nachfolger 072c8215

L: "truncate" und "simple_format" in Presenter verschoben

Unterschiede anzeigen:

SL/Presenter.pm
14 14
use SL::Presenter::Order;
15 15
use SL::Presenter::Project;
16 16
use SL::Presenter::Record;
17
use SL::Presenter::Text;
17 18

  
18 19
sub get {
19 20
  return $::request->presenter;
SL/Presenter/Text.pm
1
package SL::Presenter::Text;
2

  
3
use strict;
4

  
5
use parent qw(Exporter);
6

  
7
use Exporter qw(import);
8
our @EXPORT = qw(simple_format truncate);
9

  
10
use Carp;
11

  
12
sub truncate {
13
  my ($self, $text, %params) = @_;
14

  
15
  $params{at}             ||= 50;
16
  $params{at}               =  3 if 3 > $params{at};
17
  $params{at}              -= 3;
18

  
19
  return $text if length($text) < $params{at};
20
  return substr($text, 0, $params{at}) . '...';
21
}
22

  
23
sub simple_format {
24
  my ($self, $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
1;
36
__END__
37

  
38
=pod
39

  
40
=encoding utf8
41

  
42
=head1 NAME
43

  
44
SL::Presenter::Text - Presenter module for assorted text helpers
45

  
46
=head1 SYNOPSIS
47

  
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..."
51

  
52
=head1 FUNCTIONS
53

  
54
=over 4
55

  
56
=item C<truncate $text, [%params]>
57

  
58
Returns the C<$text> truncated after a certain number of
59
characters.
60

  
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.
65

  
66
=item C<simple_format $text>
67

  
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
72
are removed.
73

  
74
=back
75

  
76
=head1 BUGS
77

  
78
Nothing here yet.
79

  
80
=head1 AUTHOR
81

  
82
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
83

  
84
=cut
SL/Template/Plugin/L.pm
52 52
  return $_[0]->{CONTEXT};
53 53
}
54 54

  
55
sub _call_presenter {
56
  my ($method, @args) = @_;
57

  
58
  my $presenter       = $::request->presenter;
59

  
60
  return '' unless $presenter->can($method);
61

  
62
  splice @args, -1, 1, %{ $args[-1] } if @args && (ref($args[-1]) eq 'HASH');
63

  
64
  $presenter->$method(@args);
65
}
66

  
55 67
sub name_to_id {
56 68
  my $self =  shift;
57 69
  my $name =  shift;
......
596 608
}
597 609

  
598 610
sub truncate {
599
  my ($self, $text, @slurp) = @_;
600
  my %params                = _hashify(@slurp);
601

  
602
  $params{at}             ||= 50;
603
  $params{at}               =  3 if 3 > $params{at};
604
  $params{at}              -= 3;
605

  
606
  return $text if length($text) < $params{at};
607
  return substr($text, 0, $params{at}) . '...';
611
  my $self = shift;
612
  return _call_presenter('truncate', @_);
608 613
}
609 614

  
610 615
sub sortable_table_header {
......
651 656
  return SL::Presenter->get->render('common/paginate', %template_params);
652 657
}
653 658

  
659
sub simple_format {
660
  my $self = shift;
661
  return _call_presenter('simple_format', @_);
662
}
663

  
654 664
1;
655 665

  
656 666
__END__
......
1005 1015

  
1006 1016
  L.tab('Awesome tab wih much info', '_much_info.html', if => SELF.wants_all)
1007 1017

  
1008
=item C<truncate $text, %params>
1018
=item C<truncate $text, [%params]>
1019

  
1020
See L<SL::Presenter::Text/truncate>.
1009 1021

  
1010
Returns the C<$text> truncated after a certain number of
1011
characters.
1022
=item C<simple_format $text>
1012 1023

  
1013
The number of characters to truncate at is determined by the parameter
1014
C<at> which defaults to 50. If the text is longer than C<$params{at}>
1015
then it will be truncated and postfixed with '...'. Otherwise it will
1016
be returned unmodified.
1024
See L<SL::Presenter::Text/simple_format>.
1017 1025

  
1018 1026
=back
1019 1027

  

Auch abrufbar als: Unified diff