Annotation of loncom/localize/localize/mtarguments, revision 1.1

1.1     ! riegler     1: #!/usr/bin/perl
        !             2: 
        !             3: use strict;
        !             4: use warnings;
        !             5: 
        !             6: my $man =
        !             7: "mtarguments - searches a single file or all files in a directory and its subdirectory for occurence of calls to the subroutine &mt. Arguments of &mt are written to the file newphrases.txt. In addition, arguments and locations where such calls appear are written to the file newphraseslocations.txt.
        !             8: 
        !             9: mtarguments is particularly useful for detecting new arguments to mt which consequently need to be translated for non-English users of loncapa. Detection of such arguments is most easily done by comparing newphrases.txt with an earlier version of this file via unix's diff. Detected new phrases can then be incorporated into localization files using sync.pl.
        !            10: 
        !            11: 
        !            12: SYNOPSIS:\tmtarguments -h 
        !            13: \t\tmtarguments --path DIR
        !            14: \t\tmtarguments FILE
        !            15: 
        !            16: OPTIONS:
        !            17: -h\t\tDisplay this help and exit.
        !            18: 
        !            19: --path\t\tSearches in all files contained in DIR and its subdirectories.
        !            20: ";
        !            21: 
        !            22: my $path = 0;
        !            23: my @files; 
        !            24: die "Use option -h for help.\n" unless exists $ARGV[0];
        !            25: #analyze options
        !            26: if ( $ARGV[0] =~ m/^\s*-h/ ) {
        !            27: 	print $man;
        !            28: 	exit();
        !            29: }elsif( $ARGV[0] =~ m/^\s*--path/ ) {
        !            30: 	shift(@ARGV) if exists $ARGV[1] or exit;
        !            31: 	$path = $ARGV[0];
        !            32: 	die "$path is not a directory.\n" unless -d $path;
        !            33: 	@files = recursivefilelist($path);
        !            34: }else{
        !            35: 	@files = ($ARGV[0]);
        !            36: 	die "$files[0] is not a file.\n" unless -f $ARGV[0];
        !            37: }
        !            38: 
        !            39: # expression for nested parantheses
        !            40: my $rep;
        !            41: $rep = qr{ \( ((?: (?> [^()]+ )  | (??{ $rep })  )*) \) }x;
        !            42: 
        !            43: my %mtArgInFile=(); 
        !            44: #keys are arguments of &mt, values are filenames (seperated by ,) where 
        !            45: #said arguments appear
        !            46: 
        !            47: foreach my $file(@files){ 
        !            48: 	find_mt_arguments($file);
        !            49: }
        !            50: 
        !            51: sub find_mt_arguments{
        !            52: 	my $filename = shift;
        !            53: 	open(FH,$filename);
        !            54: 	while(my $line=<FH>){
        !            55: 		next if $line=~/^\s*#/;
        !            56: 		if($line=~/[^\w]&?mt$rep/ and $1 ne ''){
        !            57: 			my $mtarg = $1;
        !            58: 			#if of form '...[_1]...',$var
        !            59: 			#get only '...[_1]...' 
        !            60: 			$mtarg=~s/('|")\s*,\s*\$.*/$1/;
        !            61: 			if($mtArgInFile{$mtarg}){
        !            62: 				$mtArgInFile{$mtarg}.=", $filename" 
        !            63: 				unless $mtArgInFile{$mtarg}=~$filename;
        !            64: 			}else{
        !            65: 				$mtArgInFile{$mtarg}.=$filename;
        !            66: 			}
        !            67: 		}
        !            68: 	}
        !            69: 	close(FH);
        !            70: }
        !            71: 
        !            72: #write outputfiles
        !            73: open(NP,'>','newphrases.txt');
        !            74: open(NPL,'>','newphraseslocations.txt');
        !            75: my $warnings=0;
        !            76: foreach my $expr (sort keys %mtArgInFile){
        !            77: 	print NP "$expr\n";
        !            78: 	print NPL "$expr\n\tFOUND IN: $mtArgInFile{$expr}\n";
        !            79: 	$warnings++ if($expr=~/\$|@|&/);
        !            80: }
        !            81: close(NPL);
        !            82: close(NP);
        !            83: print "Wrote newphrases.txt.\nWrote newphraseslocations.txt.\n";
        !            84: print "WARNING: Found $warnings case(s) where argument of &mt is a perl variable.\n" if $warnings;
        !            85: 
        !            86: sub recursivefilelist {
        !            87: 	my ($currentpath) = @_;
        !            88: 	opendir( DIR, $currentpath );
        !            89: 	my @filesindir = readdir(DIR);
        !            90: 	close(DIR);
        !            91: 	my @files;
        !            92: 	foreach my $file (@filesindir) {
        !            93: 		my $fullfilename = "$currentpath/$file";
        !            94: 		if ( $file =~ m/^\.{1,2}$/ ) {
        !            95: 			#we are not interested in these (. or ..)
        !            96: 		}elsif( -d $fullfilename ) {
        !            97: 			push @files, recursivefilelist($fullfilename);
        !            98: 		}else{
        !            99: 			push @files, $fullfilename;
        !           100: 		}
        !           101: 	}
        !           102: 	return sort(@files);
        !           103: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>