Changeset 4178


Ignore:
Timestamp:
2003-04-17T14:04:33+12:00 (21 years ago)
Author:
mdewsnip
Message:

Fairly extensive changes to incorporate the changes into the macrofiles, rather than generating the macrofiles from the translation database. This is much better because a diff between the version in CVS and the output from the translator with now be sensible (because the macros have not been rearranged and the comments lost).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gsdl/bin/script/submit_translation.pl

    r4169 r4178  
    8585    my $submissions = &parse_user_submission($sourcelang, $targetlang, $targetcode,
    8686                         util::filename_cat($translationdir, $argsfile));
    87     # print "Submissions:\n";
    88     # &display_hash($submissions);
    8987
    9088    # Add the submissions to the translation database
     
    9391    # Writes the target macro files
    9492    my $macrosdir = util::filename_cat($gsdldir, "macros");
    95     &write_target_macro_files($sourcelang, $targetlang, $macrosdir);
    96 
    97     # Generate all the images for both macro files
    98     # my $imagesdir = util::filename_cat($gsdldir, "images", $targetcode);
    99     # my $scriptdir = util::filename_cat($gsdldir, "bin", "script");
    100 
    101     # my $targetdmfile1 = util::filename_cat($macrosdir, $targetlang . ".dm");
    102     # my $genimagescmd1 = "perl " . util::filename_cat($scriptdir, "translate.pl") .
    103     # " -language_symbol $targetcode -image_dir $imagesdir $targetdmfile1";
    104     # $genimagescmd1 = "cd $gsdldir ; source setup.bash ; $genimagescmd1";
    105     # open SUBMIT_SCRIPT_1, ">" . util::filename_cat($translationdir, "submit1.tmp");
    106     # print SUBMIT_SCRIPT_1 "$genimagescmd1\n";
    107     # close SUBMIT_SCRIPT_1;
    108 
    109     # my $targetdmfile2 = util::filename_cat($macrosdir, $targetlang . "2.dm");
    110     # my $genimagescmd2 = "perl -S " . util::filename_cat($scriptdir, "translate.pl") .
    111     # " -language_symbol $targetcode -image_dir $imagesdir $targetdmfile2";
    112     # print STDERR "Command 2: $genimagescmd2\n";
    113     # my $result2 = system($genimagescmd2);
    114     # `$genimagescmd2`;
    115     # print STDERR "Result 2: $result2\n";
     93    &update_target_macrofiles($targetlang, $macrosdir, $submissions);
    11694
    11795    # Clean up
     
    278256
    279257
    280 sub display_hash
    281 {
    282     my ($hash) = @_;
    283 
    284     foreach $key (sort (keys %$hash)) {
    285     print $key . "\n";
    286     print $hash->{$key} . "\n";
    287     }
    288 }
    289 
    290 
    291258sub add_submissions_to_database
    292259{
     
    305272
    306273
    307 sub write_target_macro_files
    308 {
    309     my ($sourcelang, $targetlang, $macrosdir) = @_;
    310 
    311     my $targetdmfile1 = util::filename_cat($macrosdir, &get_macrofile_name($targetlang, ""));
    312     my $targetdmfile2 = util::filename_cat($macrosdir, &get_macrofile_name($targetlang, "2"));
    313 
    314     my $currentfile = "1";
    315     my $currentpackage = "";
    316     my %textmacros = ();
    317     my %iconmacros = ();
    318 
    319     open(TARGET_DM_FILE, ">$targetdmfile1") or die "Error: Could not write file.\n";
    320 
    321     # Find all the target keys in the database
    322     foreach $key (sort (keys %translationdata)) {
    323     my $macrofile = $key;
    324     $macrofile =~ s/^([^:]+)::(.*)/$1/;
    325 
    326     # Check if this is a target macro
    327     if ($macrofile eq &get_macrofile_name($targetlang, "") ||
    328         $macrofile eq &get_macrofile_name($targetlang, "2")) {
    329 
    330         # Detect the point where we start on the auxiliary macros
    331         if ($macrofile eq &get_macrofile_name($targetlang, "2") && $currentfile eq "1") {
    332         # Write the macros for the package just finished
    333         &write_macros(TARGET_DM_FILE, $currentpackage, $textmacros, $iconmacros);
    334         close TARGET_DM_FILE;
    335 
    336         # Start a new package
    337         $currentpackage = "";
    338         $textmacros = ();
    339         $iconmacros = ();
    340 
    341         # Start the next file
    342         $currentfile = "2";
    343         open(TARGET_DM_FILE, ">$targetdmfile2") or die "Error: Could not write file.\n";
    344         }
    345 
    346         my $targetpackage = $key;
    347         $targetpackage =~ s/^([^:])+::(.*)::(.*)/$2/;
    348 
    349         my $macroname = $key;
    350         $macroname =~ s/^([^:])+::(.*)::(.*)/$3/;
    351 
    352         if ($targetpackage ne $currentpackage) {
    353         if ($currentpackage ne "") {
    354             # Write the macros for the package just finished
    355             &write_macros(TARGET_DM_FILE, $currentpackage, $textmacros, $iconmacros);
     274sub update_target_macrofiles
     275{
     276    my ($targetlang, $macrosdir, $submissions) = @_;
     277
     278    # Split the submissions by macrofile
     279    my %submissions1 = ();
     280    my %submissions2 = ();
     281    foreach $key (keys %$submissions) {
     282    if (&core_or_auxiliary_macro($targetlang, $key) eq "core") {
     283        $submissions1->{$key} = $submissions->{$key};
     284    }
     285    else {
     286        $submissions2->{$key} = $submissions->{$key};
     287    }
     288    }
     289
     290    # Add the submissions into the appropriate files
     291    my $macrofile1 = &get_macrofile_name($targetlang, "");
     292    &add_submissions_into_macrofile($targetlang, $macrosdir, $macrofile1, $submissions1);
     293    my $macrofile2 = &get_macrofile_name($targetlang, "2");
     294    &add_submissions_into_macrofile($targetlang, $macrosdir, $macrofile2, $submissions2);
     295}
     296
     297
     298sub core_or_auxiliary_macro
     299{
     300    my ($language, $sourcekey) = @_;
     301
     302    my $macrofile = $sourcekey;
     303    $macrofile =~ s/^([^:]+)::(.*)/$1/;
     304
     305    if ($macrofile eq ($language . ".dm")) {
     306    return "core";
     307    }
     308    if ($macrofile eq ($language . "2.dm")) {
     309    return "auxiliary";
     310    }
     311}
     312
     313
     314sub add_submissions_into_macrofile
     315{
     316    my ($targetlang, $macrosdir, $macrofile, $submissions) = @_;
     317
     318    # Opens the macro file or dies a miserable death
     319    my $macropath = util::filename_cat($macrosdir, $macrofile);
     320    open(MACROFILE_IN, "<$macropath") or die "Error: Could not open $macropath.\n";
     321    open(MACROFILE_TMP, ">$macrofile.tmp") or die "Error: Could not write $macrofile.tmp\n";
     322
     323    my $curroutline = 0;
     324    my $currpackage;
     325    my $currpackageinsertionpoint = $curroutline;
     326    my %insertionpoints = ();
     327    while (<MACROFILE_IN>) {
     328    #currline++;
     329
     330    # Check if a new package is being defined
     331    if (/^package (.*)/) {
     332        # Is this the end of a previous package?
     333        if ($currpackage) {
     334        # Yes, so remember the insertion point for the package
     335        $insertionpoints{$currpackageinsertionpoint} = $currpackage;
     336        }
     337        $currpackage = $1;
     338        print MACROFILE_TMP $_;
     339        $curroutline++;
     340    }
     341
     342    # Check if an icon macro is being defined
     343    elsif (/^(\#\# .*)/) {
     344        my $iconmacro = $1;
     345
     346        # In the case where the macroname contains a new line, read the next line too
     347        unless ($iconmacro =~ m/^\#\# .*\#\#/) {
     348        $iconmacro .= "\n" . <MACROFILE_IN>;
     349        chomp($iconmacro);
     350        # $currline++;
     351        $curroutline++;
     352        }
     353
     354        # Split the icon macro header on "##"
     355        my @iconparts = split(/\s*\#\#\s*/, $iconmacro);
     356        # The macroname is the last element of the header (ie. ## ... ## ... ## this ##)
     357        my $macroname = $iconparts[(scalar @iconparts) - 1];
     358
     359        # The key consists of macro file, package name, and macro name
     360        my $macrokey = $macrofile . "::" . $currpackage . "::" . $macroname;
     361
     362        # Check if this macro has just been submitted
     363        my $newsubmission = "false";
     364        if ($submissions->{$macrokey}) {
     365        # It has, so write the submitted text
     366        $newsubmission = "true";
     367        print MACROFILE_TMP $submissions->{$macrokey} . "\n";
     368        $count = ($submissions->{$macrokey} =~ tr/\n/\n/);
     369        $curroutline = $curroutline + $count;
     370        delete $submissions->{$macrokey};
     371        }
     372        else {
     373        # It hasn't, so write the old macro
     374        print MACROFILE_TMP $iconmacro . "\n";
     375        $curroutline++;
     376        }
     377
     378        # Chew up the old macro text, spitting it out if necessary
     379        do {
     380        $_ = <MACROFILE_IN>;
     381        # $currline++;
     382        if ($newsubmission ne "true") {
     383            print MACROFILE_TMP $_;
     384            $curroutline++;
    356385        }
    357 
    358         # Start a new package
    359         $currentpackage = $targetpackage;
    360         $textmacros = ();
    361         $iconmacros = ();
    362         }
    363 
    364         # Determine whether this is a text or icon macro
    365         my $macrotext = $translationdata{$key};
    366         if ($macrotext =~ m/\#\# (^|.)*/) {
    367         $iconmacros->{$macroname} = $macrotext;
     386        } until ($_ =~ /^\s+/);
     387
     388        # Remember where this macro finished
     389        $currpackageinsertionpoint = $curroutline;
     390    }
     391
     392    # Check if a text macro is being defined
     393    elsif (/^(_\w+_)(.*)/) {
     394        # Get the name of the macro
     395        my $macroname = $1;
     396        print MACROFILE_TMP $macroname;
     397
     398        # The key consists of macro file, package name, and macro name
     399        my $macrokey = $macrofile . "::" . $currpackage . "::" . $macroname;
     400
     401        # Check if this macro has just been submitted
     402        my $newsubmission = "false";
     403        if ($submissions->{$macrokey}) {
     404        # It has, so write the submitted text
     405        $newsubmission = "true";
     406        print MACROFILE_TMP $submissions->{$macrokey};
     407        $count = ($submissions->{$macrokey} =~ tr/\n/\n/);
     408        $curroutline = $curroutline + $count;
     409        delete $submissions->{$macrokey};
    368410        }
    369411        else {
    370         $textmacros->{$macroname} = $macrotext;
    371         }
    372     }
    373     }
    374 
    375     # Write the macros for the last package of the last file
    376     &write_macros(TARGET_DM_FILE, $currentpackage, $textmacros, $iconmacros);
    377     close TARGET_DM_FILE;
    378 }
    379 
    380 
    381 sub write_macros
    382 {
    383     my ($fh, $packagename, $textmacros, $iconmacros) = @_;
    384 
    385     # Write package header
    386     print $fh "\n";
    387     print $fh "######################################################################\n";
    388     print $fh "package $packagename\n";
    389     print $fh "######################################################################\n";
    390 
    391     # Write text macros
    392     print $fh "\n";
    393     print $fh "#------------------------------------------------------------\n";
    394     print $fh "# text macros\n";
    395     print $fh "#------------------------------------------------------------\n";
    396     print $fh "\n";
    397     foreach $textkey (sort (keys %$textmacros)) {
    398     print $fh $textkey . $textmacros->{$textkey} . "\n";
    399     }
    400 
    401     # Write icon macros
    402     print $fh "#------------------------------------------------------------\n";
    403     print $fh "# icons\n";
    404     print $fh "#------------------------------------------------------------\n";
    405     print $fh "\n";
    406     foreach $iconkey (sort (keys %$iconmacros)) {
    407     print $fh $iconmacros->{$iconkey} . "\n";
     412        # It hasn't, so write the old macro text
     413        print MACROFILE_TMP $2 . "\n";
     414        $curroutline++;
     415        }
     416
     417        # Chew up the old macro text, spitting it out if necessary
     418        while ($_ !~ /.*\}/) {
     419        $_ = <MACROFILE_IN>;
     420        # $currline++;
     421        if ($newsubmission ne "true") {
     422            print MACROFILE_TMP $_;
     423            $curroutline++;
     424        }
     425        }
     426
     427        # Remember where this macro finished
     428        $currpackageinsertionpoint = $curroutline;
     429    }
     430
     431    # The line must just be a comment or whitespace
     432    else {
     433        print MACROFILE_TMP $_;
     434        $curroutline++;
     435    }
     436    }
     437
     438    # Close opened files
     439    close MACROFILE_TMP;
     440    close MACROFILE_IN;
     441
     442    open(MACROFILE_TMP, "<$macrofile.tmp") or die "Error: Could not open $macrofile.tmp\n";
     443    # open(MACROFILE_OUT, ">$macrofile.out") or die "Error: Could not write $macrofile.out\n";
     444    open(MACROFILE_OUT, ">$macropath") or die "Error: Could not write $macropath\n";
     445
     446    # Insert the new macro submissions in the correct place
     447    $currline = 0;
     448    while (<MACROFILE_TMP>) {
     449    $currline++;
     450
     451    # Are we at a package insertion point?
     452    if ($insertionpoints{$currline}) {
     453        $currpackage = $insertionpoints{$currline};
     454
     455        # Yes, so check if there are any macros to insert here
     456        foreach $key (keys %$submissions) {
     457        $key =~ m/^[^:]+::([^:]+)::(.*)/;
     458        my $packagename = $1;
     459        my $macroname = $2;
     460
     461        if ($packagename eq $currpackage) {
     462            # Insert this macro here
     463            &write_macro_to_file($macroname, $submissions->{$key}, MACROFILE_OUT);
     464            delete $submissions->{$key};
     465        }
     466        }
     467    }
     468
     469    print MACROFILE_OUT $_;
     470    }
     471
     472    # If there are any submissions still remaining, write them to a new package at the end
     473    $currpackage = "";
     474    foreach $key (sort (keys %$submissions)) {
     475    $key =~ m/^[^:]+::([^:]+)::(.*)/;
     476    my $packagename = $1;
     477    my $macroname = $2;
     478
     479    if ($packagename ne $currpackage) {
     480        # Write package header
     481        print MACROFILE_OUT "\n";
     482        print MACROFILE_OUT "########################################################\n";
     483        print MACROFILE_OUT "package $packagename\n";
     484        print MACROFILE_OUT "########################################################\n";
     485
     486        $currpackage = $packagename;
     487    }
     488
     489    # Write macro
     490    &write_macro_to_file($macroname, $submissions->{$key}, MACROFILE_OUT);
     491    delete $submissions->{$key};
     492    }
     493
     494    # Close opened files
     495    close MACROFILE_OUT;
     496    close MACROFILE_TMP;
     497
     498    # Delete the temporary file
     499    unlink("$macrofile.tmp");
     500}
     501
     502
     503sub write_macro_to_file
     504{
     505    my ($macroname, $macrotext, $filehandle) = @_;
     506
     507    print MACROFILE_OUT "\n";
     508    print MACROFILE_OUT "# Macro added using the translation agency\n";
     509
     510    if ($macrotext =~ m/\#\# (^|.)*/) {
     511    # Icon macro
     512    print MACROFILE_OUT $macrotext;
     513    }
     514    else {
     515    # Text macro
     516    print MACROFILE_OUT $macroname . $macrotext;
    408517    }
    409518}
Note: See TracChangeset for help on using the changeset viewer.