# Plugin for TWiki Collaboration Platform, http://TWiki.org/ # # Copyright (C) 2000-2003 Andrea Sterbini, a.sterbini@flashnet.it # Copyright (C) 2001-2003 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.org/copyleft/gpl.html # ========================= package TWiki::Plugins::AliasPlugin; # change the package name and $pluginName!!! # ========================= use vars qw( $web $topic $user $installWeb $VERSION $aliasPattern $debug $acceptNonTWikiWord ); $VERSION = '1.010'; # ========================= sub writeDebug { TWiki::Func::writeDebug("AliasPlugin - " . $_[0]) if $debug; } # ========================= sub initPlugin { ( $topic, $web, $user, $installWeb ) = @_; # check for Plugins.pm versions if ($TWiki::Plugins::VERSION < 1) { TWiki::Func::writeWarning( "Version mismatch between AliasPlugin and Plugins.pm" ); return 0; } # 0: off, 1: debug, 2: heavy debug $debug = 0; # get plugin flag for non-twiki aliases $acceptNonTWikiWord = TWiki::Func::getPreferencesFlag( "ALIASPLUGIN_ACCEPT_NON_TWIKI_WORD_ALIASES" ); #writeDebug($acceptNonTWikiWord?"DOES":"Does NOT" . " accept non-TWiki-word aliases"); # decide on how to match alias words # $wordMatch = '[a-zA-Z0-9]+'; my $wordMatch; my $wikiWordRegex = &TWiki::Func::getRegularExpression('wikiWordRegex'); if ($acceptNonTWikiWord) { $wordMatch = '.+'; } else { $wordMatch = $wikiWordRegex; } # parse the plugin preferences lines my $prefText = TWiki::Func::readTopicText(($web), 'AliasPlugin'); foreach my $line (split /\n/, $prefText) { if ($line =~ /^(?:\t| {3})+\* (?:\)?($wordMatch): +(.*)$/) { my $key = $1; my $value = $2; $value =~ s/\s+$//go; # convenience for alias-links if ($value =~ /([^.]+)\.$wikiWordRegex/) { $value = "\[\[$value\]\[$key\]\]"; } $aliasHash{$key} = $value; writeDebug("config match: key='$key', value='$value'") if $debug > 1; } } # create alias pattern $aliasPattern = join('|', keys(%aliasHash)); writeDebug("generated aliasPattern: $aliasPattern") if $aliasPattern; # Plugin correctly initialized writeDebug("initPlugin( $web.$topic ) is OK" ); return 1; } # ========================= sub startRenderingHandler { ### my ( $text ) = @_; # do not uncomment, use $_[0] instead writeDebug("startRenderingHandler(...,$_[1]) was called"); return if !$aliasPattern; $_[0] =~ s//NOPTOKEN/g; $_[0] =~ s/([^<]+)(<[^>*]+>)?/&_substitute($1,$2)/ge; $_[0] =~ s/NOPTOKEN//g; } # ========================= sub _substitute { my ($text, $skip) = @_; $skip = '' if !$skip; if ($debug) { writeDebug("_substitute() called"); writeDebug("text='$text'"); writeDebug("skip='$skip'"); } if ($debug > 1) { # heavy debug $text =~ s/\b($aliasPattern)\b/&_debugAliasReplacement($1)/ge } else { $text =~ s/($aliasPattern)/$aliasHash{$1}/g; } return "$text$skip"; } # ========================= sub _debugAliasReplacement { my ($key) = @_; my $value = $aliasHash{$key}; writeDebug("substs '$key' -> '$value'"); return $value; } 1;