Skip to content

Commit

Permalink
Item2199: moved style to a template
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/ExternalLinkPlugin@5201 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
ArthurClemens authored and ArthurClemens committed Oct 2, 2009
1 parent 5160963 commit 448f49c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 36 deletions.
81 changes: 45 additions & 36 deletions lib/Foswiki/Plugins/ExternalLinkPlugin.pm
Expand Up @@ -13,17 +13,16 @@
# 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
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html

package Foswiki::Plugins::ExternalLinkPlugin;

our $VERSION = '$Rev$';
our $RELEASE = '1.20';
our $NO_PREFS_IN_TOPIC = 1;
our $SHORTDESCRIPTION =
'Adds a visual indicator to outgoing links';
our $pluginName = 'ExternalLinkPlugin'; # Name of this Plugin
our $SHORTDESCRIPTION = 'Adds a visual indicator to outgoing links';
our $pluginName = 'ExternalLinkPlugin'; # Name of this Plugin

my $protocolsPattern;
my $addedToHead;
Expand All @@ -32,54 +31,59 @@ sub initPlugin {
my ( $topic, $web, $user, $installWeb ) = @_;

# check for Plugins.pm versions
if( $Foswiki::Plugins::VERSION < 1.021 ) {
Foswiki::Func::writeWarning( "Version mismatch between $pluginName and Plugins.pm" );
if ( $Foswiki::Plugins::VERSION < 1.021 ) {
Foswiki::Func::writeWarning(
"Version mismatch between $pluginName and Plugins.pm");
return 0;
}

$protocolsPattern = Foswiki::Func::getRegularExpression('linkProtocolPattern');
$addedToHead = 0;
addStyleToHead();

$protocolsPattern =
Foswiki::Func::getRegularExpression('linkProtocolPattern');
$addedToHead = 0;
addStyleToHead();

# Plugin correctly initialized
Foswiki::Func::writeDebug( "- Foswiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK" ) if $Foswiki::cfg{Plugins}{ExternalLinkPlugin}{Debug};
Foswiki::Func::writeDebug(
"- Foswiki::Plugins::${pluginName}::initPlugin( $web.$topic ) is OK")
if $Foswiki::cfg{Plugins}{ExternalLinkPlugin}{Debug};
return 1;
}

sub addStyleToHead {

return if $addedToHead;
return if $addedToHead;

Foswiki::Func::writeDebug( "- ${pluginName}::addStyleToHead" ) if $Foswiki::cfg{Plugins}{ExternalLinkPlugin}{Debug};
Foswiki::Func::writeDebug("- ${pluginName}::addStyleToHead")
if $Foswiki::cfg{Plugins}{ExternalLinkPlugin}{Debug};

# Untaint is required if use locale is on
Foswiki::Func::loadTemplate(
Foswiki::Sandbox::untaintUnchecked( lc($pluginName) ) );
my $header = Foswiki::Func::expandTemplate('externallink:header');

$header =~
s/%markerimage%/$Foswiki::cfg{Plugins}{ExternalLinkPlugin}{MarkerImage}/g;
Foswiki::Func::addToHEAD( $pluginName, $header );

my $style = <<END;
<style type="text/css" media="all">
.externalLink a:link,
.externalLink a:visited,
.externalLink a:hover {
background-image:url($Foswiki::cfg{Plugins}{ExternalLinkPlugin}{MarkerImage});
background-repeat:no-repeat;
background-position:right;
padding-right:18px;
}
END
Foswiki::Func::addToHEAD($pluginName, $style);
$addedToHead = 1;
}

sub commonTagsHandler {
### my ( $text, $topic, $web ) = @_; # do not uncomment, use $_[0], $_[1]... instead

Foswiki::Func::writeDebug( "- ${pluginName}::commonTagsHandler( $_[2].$_[1] )" ) if $Foswiki::cfg{Plugins}{ExternalLinkPlugin}{Debug};
Foswiki::Func::writeDebug(
"- ${pluginName}::commonTagsHandler( $_[2].$_[1] )")
if $Foswiki::cfg{Plugins}{ExternalLinkPlugin}{Debug};

# This is the place to define customized tags and variables
# Called by Foswiki::handleCommonTags, after %INCLUDE:"..."%

# do custom extension rule, like for example:
$_[0] =~ s!(\[\[($protocolsPattern://[^]]+?)\]\[[^]]+?\]\]([&]nbsp;)?)! handleExternalLink($1, $2) !ge;
$_[0] =~
s!(\[\[($protocolsPattern://[^]]+?)\]\[[^]]+?\]\]([&]nbsp;)?)! handleExternalLink($1, $2) !ge;
}

sub internalLink { _internalLink(@_) }
sub internalLink { _internalLink(@_) }

sub _internalLink {
my $pre = shift;
Expand All @@ -89,16 +93,21 @@ sub _internalLink {
}

sub handleExternalLink {
my ( $wholeLink, $url ) = @_;
my ( $wholeLink, $url ) = @_;

my $scriptUrl = Foswiki::Func::getUrlHost() . Foswiki::Func::getScriptUrlPath();
my $pubUrl = Foswiki::Func::getUrlHost() . Foswiki::Func::getPubUrlPath();
my $scriptUrl =
Foswiki::Func::getUrlHost() . Foswiki::Func::getScriptUrlPath();
my $pubUrl = Foswiki::Func::getUrlHost() . Foswiki::Func::getPubUrlPath();

if (($url =~ /^$scriptUrl/) || ($url =~ /^$pubUrl/ ) || ($wholeLink =~ /[&]nbsp;$/)) {
return $wholeLink;
} else {
return "<span class='externalLink'>$wholeLink</span>";
}
if ( ( $url =~ /^$scriptUrl/ )
|| ( $url =~ /^$pubUrl/ )
|| ( $wholeLink =~ /[&]nbsp;$/ ) )
{
return $wholeLink;
}
else {
return "<span class='externalLink'>$wholeLink</span>";
}

}

Expand Down
1 change: 1 addition & 0 deletions lib/Foswiki/Plugins/ExternalLinkPlugin/MANIFEST
Expand Up @@ -3,3 +3,4 @@ lib/Foswiki/Plugins/ExternalLinkPlugin/Config.spec 0444 Settings
data/System/ExternalLinkPlugin.txt 0644 0644 Documentation
pub/System/ExternalLinkPlugin/diagonal.gif 0777
pub/System/ExternalLinkPlugin/external.gif 0777
templates/externallinkplugin.tmpl 0444 Template for CSS style written to head
13 changes: 13 additions & 0 deletions templates/externallinkplugin.tmpl
@@ -0,0 +1,13 @@
%{ this is the default header for ExternalLinkPlugin. Variable %markerimage% will be substituted
with the value of $Foswiki::cfg{Plugins}{ExternalLinkPlugin}{MarkerImage} }%

%TMPL:DEF{externallink:header}%<style type="text/css" media="all">
.externalLink a:link,
.externalLink a:visited,
.externalLink a:hover {
background-image:url(%markerimage%);
background-repeat:no-repeat;
background-position:right;
padding-right:18px;
</style>
%TMPL:END%

0 comments on commit 448f49c

Please sign in to comment.