Skip to content

Commit

Permalink
Item11808: reverting perltidy
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/MimeIconPlugin@14737 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
MichaelDaum authored and MichaelDaum committed May 7, 2012
1 parent 8758a39 commit a47077a
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 166 deletions.
299 changes: 138 additions & 161 deletions lib/Foswiki/Plugins/MimeIconPlugin.pm
Expand Up @@ -26,9 +26,9 @@ use warnings;

use Foswiki::Func ();

our $VERSION = '$Rev$';
our $RELEASE = '1.1';
our $SHORTDESCRIPTION = 'Icon sets for mimetypes';
our $VERSION = '$Rev$';
our $RELEASE = '1.1';
our $SHORTDESCRIPTION = 'Icon sets for mimetypes';
our $NO_PREFS_IN_TOPIC = 1;
our $baseWeb;
our $baseTopic;
Expand All @@ -41,11 +41,11 @@ our %cache = ();
=cut

sub initPlugin {
( $baseTopic, $baseWeb ) = @_;
($baseTopic, $baseWeb) = @_;

Foswiki::Func::registerTagHandler( 'MIMEICON', \&MIMEICON );
Foswiki::Func::registerTagHandler('MIMEICON', \&MIMEICON);

return 1;
return 1;
}

=begin TML
Expand All @@ -57,42 +57,39 @@ handler for the MIMEICON macro
=cut

sub MIMEICON {
my ( $session, $params ) = @_;
my ($session, $params) = @_;

my $extension = lc( $params->{_DEFAULT} || '' );
my $size = $params->{size} || '48';
my $theme = $params->{theme};
my $format = $params->{format};
my $extension = lc($params->{_DEFAULT} || '');
my $size = $params->{size} || '48';
my $theme = $params->{theme};
my $format = $params->{format};

$format =
"<img class='foswikiIcon' src='\$url' width='\$size' height='\$size' alt='\$name' />"
unless defined $format;
$format = "<img class='foswikiIcon' src='\$url' width='\$size' height='\$size' alt='\$name' />"
unless defined $format;

$theme = $Foswiki::cfg{Plugins}{MimeIconPlugin}{Theme} || 'oxygen'
unless defined $theme;
$theme = $Foswiki::cfg{Plugins}{MimeIconPlugin}{Theme} || 'oxygen'
unless defined $theme;

$extension =~ s/^.*\.//;
$extension =~ s/^\s+//;
$extension =~ s/\s+$//;
$extension =~ s/^.*\.//;
$extension =~ s/^\s+//;
$extension =~ s/\s+$//;

$size = getBestSize( $theme, $size );
my ( $iconName, $iconPath ) = getIcon( $extension, $theme, $size );
$size = getBestSize($theme, $size);
my ($iconName, $iconPath) = getIcon($extension, $theme, $size);

return
"<span class='foswikiAlert'>Error: can't even find a fallback mime-icon</span>"
unless defined $iconName;
return "<span class='foswikiAlert'>Error: can't even find a fallback mime-icon</span>"
unless defined $iconName;

# formatting result
my $result = $format;
$result =~ s/(%NAME%|\$name\b)/$iconName/g;
$result =~ s/(%URL%|\$url\b)/$iconPath/g;
$result =~ s/(%SIZE%|\$size\b)/$size/g;
# formatting result
my $result = $format;
$result =~ s/(%NAME%|\$name\b)/$iconName/g;
$result =~ s/(%URL%|\$url\b)/$iconPath/g;
$result =~ s/(%SIZE%|\$size\b)/$size/g;

print STDERR
"MimeIconPlugin - extension=$extension, icon=$iconName, iconPath=$iconPath\n"
if $Foswiki::cfg{Plugins}{MimeIconPlugin}{Debug};
print STDERR "MimeIconPlugin - extension=$extension, icon=$iconName, iconPath=$iconPath\n"
if $Foswiki::cfg{Plugins}{MimeIconPlugin}{Debug};

return $result;
return $result;
}

=begin TML
Expand All @@ -102,88 +99,77 @@ returns the name and path of an icon given an extension.
=cut

sub getIcon {
my ( $extension, $theme, $size, $fromFallback ) = @_;

readIconMapping($theme)
unless defined $cache{ $theme . ':sizes' };
my ($extension, $theme, $size, $fromFallback) = @_;

my $iconName = $cache{ $theme . ':' . $extension };
my $iconPath = $cache{ $theme . ':' . $extension . ':' . $size };
readIconMapping($theme)
unless defined $cache{$theme.':sizes'};

return ( $iconName, $iconPath ) if defined $iconName && defined $iconPath;
my $iconName = $cache{ $theme . ':' . $extension };
my $iconPath = $cache{ $theme . ':' . $extension . ':' . $size };

unless ($iconName) {
return ($iconName, $iconPath) if defined $iconName && defined $iconPath;

# even 'unknown' is unknown
if ( $extension eq 'unknown' ) {
print STDERR
"ERROR: no default icon when asking for $fromFallback\n";
return;
}
unless ($iconName) {

# try 'unknown'
return getIcon( "unknown", $theme, $size, $extension );
# even 'unknown' is unknown
if ($extension eq 'unknown') {
print STDERR "ERROR: no default icon when asking for $fromFallback\n";
return;
}

# found mapping, now get iconPath

# checking physical presence
my $pubDir =
$Foswiki::cfg{PubDir} . '/'
. $Foswiki::cfg{SystemWebName}
. '/MimeIconPlugin/'
. $theme;
my $pubPath =
$Foswiki::cfg{PubUrlPath} . '/'
. $Foswiki::cfg{SystemWebName}
. '/MimeIconPlugin/'
. $theme;

my $iconDir = $pubDir . '/' . $size . 'x' . $size . '/' . $iconName;
$iconPath = $pubPath . '/' . $size . 'x' . $size . '/' . $iconName;

unless ( -f $iconDir ) {
print STDERR
"MimeIconPlugin - $iconName not found at $iconDir ... checking lower resolutions\n"
if $Foswiki::cfg{Plugins}{MimeIconPlugin}{Debug};

# fallback to lower size
my $state = 0;
foreach my $s ( @{ $cache{ $theme . ':sizes' } } ) {
print STDERR "MimeIconPlugin - ... checking $s\n"
if $Foswiki::cfg{Plugins}{MimeIconPlugin}{Debug};
if ( $state == 0 ) {
next unless $s eq $size;
$state = 1;
next;
}
if ( $state == 1 ) {
$iconDir = $pubDir . '/' . $s . 'x' . $s . '/' . $iconName;
$iconPath = $pubPath . '/' . $s . 'x' . $s . '/' . $iconName;
if ( -f $iconDir ) {
$state = 2;
last;
}
}
}
if ( $state < 2 ) {
# try 'unknown'
return getIcon("unknown", $theme, $size, $extension);
}

# found mapping, now get iconPath

# checking physical presence
my $pubDir = $Foswiki::cfg{PubDir} . '/' . $Foswiki::cfg{SystemWebName} . '/MimeIconPlugin/' . $theme;
my $pubPath = $Foswiki::cfg{PubUrlPath} . '/' . $Foswiki::cfg{SystemWebName} . '/MimeIconPlugin/' . $theme;

# no icon found
if ( $extension eq 'unknown' ) {
print STDERR
"ERROR: no default icon when asking for '$fromFallback'\n";
return;
}
my $iconDir = $pubDir . '/' . $size . 'x' . $size . '/' . $iconName;
$iconPath = $pubPath . '/' . $size . 'x' . $size . '/' . $iconName;

return getIcon( "unknown", $theme, $size, $extension );
unless (-f $iconDir) {
print STDERR "MimeIconPlugin - $iconName not found at $iconDir ... checking lower resolutions\n"
if $Foswiki::cfg{Plugins}{MimeIconPlugin}{Debug};

# fallback to lower size
my $state = 0;
foreach my $s (@{ $cache{ $theme . ':sizes' } }) {
print STDERR "MimeIconPlugin - ... checking $s\n"
if $Foswiki::cfg{Plugins}{MimeIconPlugin}{Debug};
if ($state == 0) {
next unless $s eq $size;
$state = 1;
next;
}
if ($state == 1) {
$iconDir = $pubDir . '/' . $s . 'x' . $s . '/' . $iconName;
$iconPath = $pubPath . '/' . $s . 'x' . $s . '/' . $iconName;
if (-f $iconDir) {
$state = 2;
last;
}
}
}
if ($state < 2) {

# no icon found
if ($extension eq 'unknown') {
print STDERR "ERROR: no default icon when asking for '$fromFallback'\n";
return;
}

return getIcon("unknown", $theme, $size, $extension);
}
}

# caching
$extension = $fromFallback if defined $fromFallback;
$cache{ $theme . ':' . $extension . ':' . $size } = $iconPath;
# caching
$extension = $fromFallback if defined $fromFallback;
$cache{ $theme . ':' . $extension . ':' . $size } = $iconPath;

return ( $iconName, $iconPath );
return ($iconName, $iconPath);
}

=begin TML
Expand All @@ -195,25 +181,24 @@ returns the closest icon size available for a theme
=cut

sub getBestSize {
my ( $theme, $size ) = @_;

readIconMapping($theme)
unless defined $cache{ $theme . ':sizes' };

if ( defined $cache{ $theme . ':knownsizes' }{$size} ) {
return $size;
}
else {
my $bestSize = 16;
foreach my $s ( @{ $cache{ $theme . ':sizes' } } ) {
if ( $size >= $s ) {
$bestSize = $s;
last;
}
}
print STDERR "bestSize=$bestSize size=$size\n";
return $bestSize;
my ($theme, $size) = @_;

readIconMapping($theme)
unless defined $cache{$theme.':sizes'};

if (defined $cache{$theme.':knownsizes'}{$size}) {
return $size;
} else {
my $bestSize = 16;
foreach my $s (@{$cache{$theme.':sizes'}}) {
if($size >= $s) {
$bestSize = $s;
last;
}
}
print STDERR "bestSize=$bestSize size=$size\n";
return $bestSize;
}
}

=begin TML
Expand All @@ -223,59 +208,51 @@ sub getBestSize {
=cut

sub finishPlugin {

my $keep = $Foswiki::cfg{Plugins}{MimeIconPlugin}{MemoryCache};
$keep = 1 unless defined $keep;

my $keep = $Foswiki::cfg{Plugins}{MimeIconPlugin}{MemoryCache};
$keep = 1 unless defined $keep;

undef %cache unless $keep;
undef %cache unless $keep;
}


=begin TML
---++ readIconMapping()
=cut

sub readIconMapping {
my ($theme) = shift;

print STDERR "MimeIconPlugin - readIconMapping($theme)\n"
if $Foswiki::cfg{Plugins}{MimeIconPlugin}{Debug};

my $mappingFile =
$Foswiki::cfg{PubDir} . '/'
. $Foswiki::cfg{SystemWebName}
. '/MimeIconPlugin/'
. $theme
. '/mapping.txt';

my $IN_FILE;
open( $IN_FILE, '<', $mappingFile ) || return '';

foreach my $line (<$IN_FILE>) {
$line =~ s/#.*$//;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next if $line =~ /^$/;

if ( $line =~ /^(.*?)\s*=\s*(.*?)$/ ) {
my $key = $1;
my $val = $2;
if ( $key eq 'sizes' ) {
$cache{ $theme . ':sizes' } =
[ reverse split( /\s*,\s*/, $val ) ];
}
else {
$cache{ $theme . ':' . $key } = $val;
}
}
my ($theme) = shift;

print STDERR "MimeIconPlugin - readIconMapping($theme)\n"
if $Foswiki::cfg{Plugins}{MimeIconPlugin}{Debug};

my $mappingFile = $Foswiki::cfg{PubDir}.'/'.$Foswiki::cfg{SystemWebName}.'/MimeIconPlugin/'.$theme.'/mapping.txt';

my $IN_FILE;
open( $IN_FILE, '<', $mappingFile ) || return '';

foreach my $line (<$IN_FILE>) {
$line =~ s/#.*$//;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
next if $line =~ /^$/;

if ($line =~ /^(.*?)\s*=\s*(.*?)$/) {
my $key = $1;
my $val = $2;
if ($key eq 'sizes') {
$cache{$theme.':sizes'} = [ reverse split(/\s*,\s*/, $val)];
} else {
$cache{$theme.':'.$key} = $val;
}
}
close($IN_FILE);
}
close($IN_FILE);

$cache{ $theme . ':sizes' } = ['16']
unless defined $cache{ $theme . ':sizes' };
%{ $cache{ $theme . ':knownsizes' } } =
map { $_ => 1 } @{ $cache{ $theme . ':sizes' } };
$cache{$theme.':sizes'} = ['16'] unless defined $cache{$theme.':sizes'};
%{$cache{$theme.':knownsizes'}} = map {$_ => 1} @{$cache{$theme.':sizes'}};
}

1;
5 changes: 1 addition & 4 deletions lib/Foswiki/Plugins/MimeIconPlugin/build.pl
Expand Up @@ -12,16 +12,13 @@

# name of web to upload to
$build->{UPLOADTARGETWEB} = 'Extensions';

# Full URL of pub directory
$build->{UPLOADTARGETPUB} = 'http://foswiki.org/pub';

# Full URL of bin directory
$build->{UPLOADTARGETSCRIPT} = 'http://foswiki.org/bin';

# Script extension
$build->{UPLOADTARGETSUFFIX} = '';

# Build the target on the command line, or the default target
$build->build( $build->{target} );
$build->build($build->{target});

0 comments on commit a47077a

Please sign in to comment.