BBCode and CCode to Render model.
[blog.pm.git] / lib / Blog / Helper / Util.pm
blob79e9729652de1a928b182ab3744fa1b7c01dd4ff
1 package Blog::Helper::Util;
3 use strict;
5 use base 'Blog::Helper';
7 sub mon2name {
8 my ( $self, $mon, $short ) = @_;
10 return unless defined $mon && ( $mon >= 0 && $mon < 12 );
12 $short ||= '';
13 my $abbr = $self->{ "_helper_util_mon2name_abbr$short" };
15 unless ( $abbr ) {
16 my @abbr =
17 split(
18 ' ',
19 $self->{ c }->loc(
20 $short
21 ? 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'
22 : 'January February March April May June July August '
23 . 'September October November December'
27 $abbr = $self->{ "_helper_util_mon2name_abbr$short" } = \@abbr;
30 return $abbr->[ $mon ];
33 sub datefmt {
34 my ( $self, $dt ) = @_;
36 return unless ref $dt;
38 #my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
39 #localtime( $date );
41 return sprintf(
42 qq#<span class="dateformat"><span class="date">%02d %s, %d</span>#,
43 $dt->day, $self->mon2name( $dt->month - 1, 1 ), $dt->year
47 sub datediff {
48 my ( $self, $addtime, $modtime ) = @_;
50 return $modtime->delta_days( $addtime )->days;
53 sub uuid {
54 my ( $self ) = @_;
56 my $uuid = int( rand( 9 ) ) + 1;
57 foreach ( 1 .. 5 ) {
58 $uuid .= int( rand( 10 ) );
61 return $uuid;
64 sub cut {
65 my ( $self, $text, $maxlength ) = @_;
67 $maxlength ||= 50;
69 if ( $text =~ m/^<p>(.*?)<\/p>/os ) {
70 $text = $1;
73 $text =~ s/<.*?>//g;
75 my $toolong = 1 if length $text > $maxlength;
77 if ( $toolong ) {
78 $text = substr( $text, 0, $maxlength );
79 $text =~ s/[^[:blank:]]+$//o;
80 $text .= '[...]';
83 return $text ? $text : '[...]';
86 =head1 AUTHOR
88 vti
90 =head1 LICENSE
92 This library is free software, you can redistribute it and/or modify
93 it under the same terms as Perl itself.
95 =cut