package TWiki::Plugins::SmiliesPlugin;
# Plugin for TWiki Collaboration Platform, http://TWiki.org/
#
# Copyright (C) 2000-2001 Andrea Sterbini, a.sterbini@flashnet.it
# Copyright (C) 2002-2004 Peter Thoeny, peter@thoeny.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details, published at
# http://www.gnu.ai.mit.edu/copyleft/gpl.html
#
# =========================
#
# This plugin replaces smilies with small smilies bitmaps
# see TWiki.SmiliesPlugin
#
# =========================
# =========================



#                          smilie       url            emotion

sub new { 
	my $dummy = shift;
	my $config = shift;
	unshift @_,$dummy ;

   my ( $proto ) =@_ ;
   my $class = ref($proto) || $proto ;
   my $self;

	$self->{config}=$config;
   bless ( $self );

	$self->init_config();

   return $self ;
}

sub init_config { 

   my $self = shift;
	my $config = $self->{config} ;


$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{VERSION} = '1.003';
$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesPattern} = '^\s*\|\s*<nop>(?:\&nbsp\;)?([^\s|]+)\s*\|\s*%ATTACHURL%\/([^\s]+)\s*\|\s*"([^"|]+)"\s*\|\s*$';
$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{allPattern} = "";
$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesPubUrl} = "";

}# END of init_config

# =========================
sub initPlugin
{ 
   my $self = shift;
	my $config = $self->{config} ;


 
	 $config->{TWiki}->{Plugins}->{SmiliesPlugin}->init_config () ;

    ( $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{topic}, $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{web}, $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{user}, $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{installWeb} ) = @_;

    # Get plugin debug flag
    $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{debug} = $config->{TWiki}->{Func}->getPreferencesFlag ( "SMILIESPLUGIN_DEBUG" );

    # Get plugin preferences
    $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesFormat} = $config->{TWiki}->{Func}->getPreferencesValue ( "SMILIESPLUGIN_FORMAT" ) 
					|| '<img src="$url" alt="$tooltip" title="$tooltip" border="0" />';

    my $topicLoc = $config->{TWiki}->{Func}->getPreferencesValue ( "SMILIESPLUGIN_TOPIC" ) 
					|| "$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{installWeb}.SmiliesPlugin"; 

    my $webLoc = $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{installWeb};
    if( $topicLoc =~ /(.+)\.(.+)/ ) {
        $webLoc = $1;
        $topicLoc = $2;
    }

    $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{allPattern} = "(";
    foreach( split( /\n/, $config->{TWiki}->{Func}->readTopic ( $webLoc, $topicLoc ) ) ) {
        if( m/$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesPattern}/ ) {
            $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{allPattern} .= "\Q$1\E|";
            $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesUrls}{$1}     = $2;
            $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesEmotions}{$1} = $3;
        }
    }
    $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{allPattern} =~ s/\|$//o;
    $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{allPattern} .= ")";
    $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesPubUrl} = $config->{TWiki}->{Func}->getUrlHost () . $config->{TWiki}->{Func}->getPubUrlPath () .
                     "/$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{installWeb}/SmiliesPlugin";

    # Initialization OK
    return 1;
}

# =========================
sub commonTagsHandler
{ 
   my $self = shift;
	my $config = $self->{config} ;


#    my ( $text, $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{topic}, $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{web} ) = @_;
    $_[0] =~ s/%SMILIES%/$config->{TWiki}->{Plugins}->{SmiliesPlugin}->allSmiliesTable () /geo;
}

# =========================
sub outsidePREHandler
{ 
   my $self = shift;
	my $config = $self->{config} ;


#    my ( $text, $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{web} ) = @_;

    $_[0] =~ s/(\s|^)$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{allPattern}(?=\s|$)/$config->{TWiki}->{Plugins}->{SmiliesPlugin}->renderSmilie ( $1, $2 )/geo;
}

# =========================
sub renderSmilie
{ 
   my $self = shift;
	my $config = $self->{config} ;


    my ( $thePre, $theSmilie ) = @_;

    return $thePre unless $theSmilie;  # return unless initialized

    my $text = "$thePre$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesFormat}";
    $text =~ s/\$emoticon/$theSmilie/go;
    $text =~ s/\$tooltip/$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesEmotions}{$theSmilie}/go;
    $text =~ s/\$url/$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesPubUrl}\/$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesUrls}{$theSmilie}/go;

    return $text;
}

# =========================
sub allSmiliesTable
{ 
   my $self = shift;
	my $config = $self->{config} ;


    my $text = "| *What to Type* | *Graphic That Will Appear* | *Emotion* |\n";

#    my ($k, $a, $b);
    foreach $k ( sort { $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesEmotions}{$b} cmp $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesEmotions}{$a} }
                keys %{$config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesEmotions}} )
    {
        $text .= "| <nop>$k | $k | ". $config->{TWiki}->{Plugins}->{SmiliesPlugin}->{smiliesEmotions}{$k} ." |\n";
    }
    return $text;
}

# =========================

1;

1;
