| 1 | package JAF::Util; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use File::Path (); | ||
| 5 | use File::Basename (); | ||
| 6 | use DirHandle (); | ||
| 7 | |||
| 8 | ### Content | ||
| 9 | |||
| 10 | sub trim { | ||
| 11 | my $s = shift; | ||
| 12 | $s =~ s/^\s+//s; | ||
| 13 | $s =~ s/\s+$//s; | ||
| 14 | return $s; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub urlify { | ||
| 18 | my $urls = '(http|telnet|gopher|file|wais|ftp)'; | ||
| 19 | my $ltrs = '\w'; | ||
| 20 | my $gunk = '/#~:.?+=&%@!\-'; | ||
| 21 | my $punc = '.:?\-'; | ||
| 22 | my $any = "${ltrs}${gunk}${punc}"; | ||
| 23 | |||
| 24 | my @result = (); | ||
| 25 | my @data = @_; | ||
| 26 | |||
| 27 | while ($_ = shift @data) { | ||
| 28 | s{ | ||
| 29 | \b # start at word boundary | ||
| 30 | ( # begin $1 { | ||
| 31 | $urls : # need resource and a colon | ||
| 32 | [$any] +? # followed by on or more | ||
| 33 | # of any valid character, but | ||
| 34 | # be conservative and take only | ||
| 35 | # what you need to.... | ||
| 36 | ) # end $1 } | ||
| 37 | (?= # look-ahead non-consumptive assertion | ||
| 38 | [$punc]* # either 0 or more punctuation | ||
| 39 | [^$any] # followed by a non-url char | ||
| 40 | | # or else | ||
| 41 | $ # then end of the string | ||
| 42 | ) | ||
| 43 | }{<a target="_blank" href="$1">$1</a>}igox; | ||
| 44 | push @result, $_; | ||
| 45 | } | ||
| 46 | return wantarray ? @result : $result[0]; | ||
| 47 | } | ||
| 48 | |||
| 49 | ### System | ||
| 50 | |||
| 51 | sub mkdir { | ||
| 52 | my ($path, $root, $subst) = @_; | ||
| 53 | return unless $path; | ||
| 54 | if ($root) { | ||
| 55 | my @dirs = split '/', $root; | ||
| 56 | $dirs[-1] = $subst if $subst; | ||
| 57 | $root = join '/', @dirs; | ||
| 58 | } | ||
| 59 | $path = $root . $path if $root; | ||
| 60 | File::Path::mkpath($path) unless -d $path; | ||
| 61 | return $path; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub unlink_with_path { | ||
| 65 | my $filename = shift; | ||
| 66 | my (@files, $rm); | ||
| 67 | |||
| 68 | if(-f $filename) { | ||
| 69 | $rm = File::Basename::dirname($filename); | ||
| 70 | unlink($filename); | ||
| 71 | } elsif(-d $filename) { | ||
| 72 | $rm = $filename; | ||
| 73 | } else { | ||
| 74 | return "Neither a file nor a directory!"; | ||
| 75 | } | ||
| 76 | |||
| 77 | while (!@files && $rm) { | ||
| 78 | opendir DIR, $rm; | ||
| 79 | @files = grep !/^\.\.?$/, readdir(DIR); | ||
| 80 | closedir DIR; | ||
| 81 | unless (@files) { | ||
| 82 | rmdir $rm; | ||
| 83 | $rm =~ s/\/([^\/]+)$//g; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | return $!; | ||
| 87 | } | ||
| 88 | |||
| 89 | ### Date | ||
| 90 | |||
| 91 | sub current_date { | ||
| 92 | my ($day, $month, $year) = (localtime)[3..5]; | ||
| 93 | $month++; | ||
| 94 | $year += 1900; | ||
| 95 | return wantarray ? ($day, $month, $year) : sprintf "%d.%02d.%04d", ($day, $month, $year); | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; |