# 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::MacroPlugin; # change the package name and $pluginName!!! # ========================= use vars qw( $web $topic $user $installWeb $VERSION $macroPattern $prefixRegExp $debug $acceptNonTWikiWord ); $VERSION = '1.011'; # ========================= sub initPlugin { ( $topic, $web, $user, $installWeb ) = @_; # check for Plugins.pm versions if( $TWiki::Plugins::VERSION < 1 ) { TWiki::Func::writeWarning( "Version mismatch between MacroPlugin and Plugins.pm" ); return 0; } # Get plugin debug flag $debug = TWiki::Func::getPreferencesValue("MACROPLUGIN_DEBUG"); TWiki::Func::writeDebug( "Trying to start MacroPlugin, debug level = $debug" ) if $debug; # Get plugin flag for non-twiki macroes $acceptNonTWikiWord = TWiki::Func::getPreferencesFlag( "MACROPLUGIN_ACCEPT_NON_TWIKI_WORD_MACROES" ); TWiki::Func::writeDebug("MacroPlugin: ".($acceptNonTWikiWord?"DOES":"Does NOT")." accept non-TWiki-word macroes") if $debug; #$prefixRegExp = '\\s|\\)'; $prefixRegExp = '[-=*#&,;.:\\/()\\]{}+_>\\s]'; # should this be set by user on MacroPlugin page ? TWiki::Func::writeDebug("MacroPlugin: Reg exp prefixing macroes: $prefixRegExp") if $debug; # get the plugin preferences text my $prefText = TWiki::Func::readTopicText( TWiki, MacroPlugin ); my @prefLines = split /\n/, $prefText; # create macro hash foreach my $line ( @prefLines ) { if(($acceptNonTWikiWord && $line =~ /\|\s*(?:\)?(\w+)\s*\|\s*(?:\)?(\S*[^|]*)\s*\|/) || ($line =~ /\|\s*(?:\)?($TWiki::wikiWordRegex)\s*\|\s*(?:\)?(\S*[^|]*)\s*\|/)) { TWiki::Func::writeDebug("MacroPlugin (detail): Config match: key=$1, value=$2") if($debug>1); #heavy debug $macroHash{"$1"} = "$2"; } } # create macro pattern $macroPattern = join('|', keys(%macroHash)); TWiki::Func::writeDebug( "MacroPlugin: generated macroPattern: $macroPattern") if $debug; # Plugin correctly initialized TWiki::Func::writeDebug( "- TWiki::Plugins::MacroPlugin::initPlugin( $web.$topic ) is OK" ) if $debug; return 1; } # ========================= sub startRenderingHandler { ### my ( $text ) = @_; # do not uncomment, use $_[0] instead TWiki::Func::writeDebug("MacroPlugin: startRenderingHandler(...,$_[1]) was called") if $debug; # if a WikiWord is found, check the line for defined macroes # and replace them TWiki::Func::writeDebug("MacroPlugin: startRenderingHandler got macroPattern = $macroPattern") if $debug; $_[0] =~ s//NOPTOKEN/g; if($debug>1) { # debug each alisa substitution $_[0] =~ s/($prefixRegExp)($macroPattern)\b/&_debugMacroReplacement($1,$2)/eg; } else { $_[0] =~ s/($prefixRegExp)($macroPattern)\b/$1$macroHash{$2}/g; } $_[0] =~ s/NOPTOKEN//g; } sub _debugMacroReplacement { my($prefix,$macro) = @_; my $link = $macroHash{$macro}; TWiki::Func::writeDebug("MacroPlugin (detail): Render '$macro' as '$link'"); return "$prefix\[\[$link\]\[$macro\]\]"; } 1;