#!/usr/bin/perl use strict; use warnings; my $man = " 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 appended to the file newphrases.txt. In addition, arguments and locations where such calls appear are written to the file newphraseslocations.txt. 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. SYNOPSIS:\tmtarguments -h \t\tmtarguments --path DIR \t\tmtarguments FILE OPTIONS: -h\t\tDisplay this help and exit. --path\t\tSearches in all files contained in DIR and its subdirectories. "; my $path = 0; my @files; die "Use option -h for help.\n" unless exists $ARGV[0]; #analyze options if ( $ARGV[0] =~ m/^\s*-h/ ) { print $man; exit(); }elsif( $ARGV[0] =~ m/^\s*--path/ ) { shift(@ARGV) if exists $ARGV[1] or exit; $path = $ARGV[0]; die "$path is not a directory.\n" unless -d $path; @files = recursivefilelist($path); }else{ @files = ($ARGV[0]); die "$files[0] is not a file.\n" unless -f $ARGV[0]; } # Start Analysis print "mtarguments is searching...\n"; # expression for nested parantheses my $rep; $rep = qr{ \( ((?: (?> [^()]+ ) | (??{ $rep }) )*) \) }x; my %mtArgInFile=(); #keys are arguments of &mt, values are filenames (seperated by ,) where #said arguments appear foreach my $file(@files){ find_mt_arguments($file); } sub find_mt_arguments{ my $filename = shift; open(FH,$filename); while(my $line=){ next if $line=~/^\s*#/; if($line=~/[^\w]&?mt$rep/ and $1 ne ''){ my $mtarg = $1; #if of form '...[_1]...',$var #get only '...[_1]...' $mtarg=~s/('|")\s*,\s*\$.*/$1/; if($mtArgInFile{$mtarg}){ $mtArgInFile{$mtarg}.=", $filename" unless $mtArgInFile{$mtarg}=~$filename; }else{ $mtArgInFile{$mtarg}.=$filename; } } } close(FH); } #write outputfiles open(NP,'>>','newphrases.txt'); # Keep existing entries and append new phrases. synch.pl will care about entries which occur more than one time. open(NPL,'>','newphraseslocations.txt'); my $counter=0; my $warnings=0; my $exprNP=""; foreach my $expr (sort keys %mtArgInFile){ $exprNP=$expr; $exprNP=~s/^["'](.*)["']$/$1/; # Remove " and ' at beginning and end for newphrases.txt print NP "$exprNP\n"; print NPL "$expr\n\tFOUND IN: $mtArgInFile{$expr}\n"; $counter++; $warnings++ if($expr=~/\$|@|&/); } close(NP); print "Wrote newphrases.txt.\n"; close(NPL); print "Wrote newphraseslocations.txt.\n"; print "WARNING: Found $warnings case(s) where argument of &mt contains a perl variable.\n" if $warnings; print "Found $counter new phrases.\n"; sub recursivefilelist { my ($currentpath) = @_; opendir( DIR, $currentpath ); my @filesindir = readdir(DIR); close(DIR); my @files; foreach my $file (@filesindir) { my $fullfilename = "$currentpath/$file"; if ( $file =~ m/^\.{1,2}$/ ) { #we are not interested in these (. or ..) }elsif( -d $fullfilename ) { push @files, recursivefilelist($fullfilename); }else{ push @files, $fullfilename; } } return sort(@files); }