File:  [LON-CAPA] / loncom / localize / localize / mtarguments
Revision 1.2: download - view: text, annotated - select for diffs
Tue Jan 13 12:42:55 2009 UTC (15 years, 3 months ago) by bisitz
Branches: MAIN
CVS tags: version_2_9_X, version_2_9_99_0, version_2_9_1, version_2_9_0, version_2_8_99_1, version_2_8_99_0, version_2_12_X, version_2_11_X, version_2_11_4_uiuc, version_2_11_4_msu, version_2_11_4, version_2_11_3_uiuc, version_2_11_3_msu, version_2_11_3, version_2_11_2_uiuc, version_2_11_2_msu, version_2_11_2_educog, version_2_11_2, version_2_11_1, version_2_11_0_RC3, version_2_11_0_RC2, version_2_11_0_RC1, version_2_11_0, version_2_10_X, version_2_10_1, version_2_10_0_RC2, version_2_10_0_RC1, version_2_10_0, loncapaMITrelate_1, language_hyphenation_merge, language_hyphenation, bz6209-base, bz6209, bz5969, bz2851, PRINT_INCOMPLETE_base, PRINT_INCOMPLETE, HEAD, GCI_3, GCI_2, BZ5971-printing-apage, BZ5434-fox, BZ4492-merge, BZ4492-feature_horizontal_radioresponse
- Don't rewrite file but append new entries to newtranslations.txt. Keep exisiting entries, e.g. additions from other scripts. Changed documentation accordingly.
- Display start message
- Display of file written info after actual closure now.
- Bugfix: Don't write " or ' at beginning and end of each entry to newphrases.txt anymore. Needed for synch.pl.
- Display info about total amount of found entries.

#!/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=<FH>){
		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);
}

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