Changeset 11321


Ignore:
Timestamp:
2006-03-09T15:07:34+13:00 (18 years ago)
Author:
mdewsnip
Message:

Added support for the XML files used for the tutorial exercises.

File:
1 edited

Legend:

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

    r11224 r11321  
    948948{
    949949    my $source_file = shift(@_);  # Not used
    950     my $source_file_lines = shift(@_);
     950    my @source_file_lines = @{shift(@_)};
    951951    my $source_file_key_to_text_mapping = shift(@_);
    952952    my $target_file = shift(@_);
    953     my $target_file_lines = shift(@_);  # Not used
     953    my @target_file_lines = @{shift(@_)};
    954954    my $target_file_key_to_text_mapping = shift(@_);
    955955    my $target_file_key_to_comment_date_mapping = shift(@_);
     
    957957
    958958    # Build a mapping from source file line to chunk key
    959     my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@$source_file_lines);
     959    my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@source_file_lines);
    960960    my %source_file_line_to_key_mapping = ();
    961961    foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
     
    966966
    967967    # Build a mapping from target file line to chunk key
    968     my %target_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@$target_file_lines);
     968    my %target_file_key_to_line_mapping = &build_key_to_line_mapping_for_macrofile(@target_file_lines);
    969969    my %target_file_line_to_key_mapping = ();
    970970    foreach my $chunk_key (keys(%target_file_key_to_line_mapping)) {
     
    984984    my $target_file_chunk_starting_line_number = (split(/-/, $target_file_line_keys[0]))[0];
    985985    while ($target_file_line_number < $target_file_chunk_starting_line_number) {
    986         my $target_file_line = @$target_file_lines[$target_file_line_number];
     986        my $target_file_line = $target_file_lines[$target_file_line_number];
    987987        last if ($target_file_line =~ /^\# -- Missing translation: /);  # We don't want to get into the macros
    988988        print TARGET_FILE $target_file_line;
     
    999999    my $source_file_chunk_finishing_line_number = (split(/-/, $line_key))[1];
    10001000    while ($source_file_line_number < $source_file_chunk_starting_line_number) {
    1001         print TARGET_FILE @$source_file_lines[$source_file_line_number];
     1001        print TARGET_FILE $source_file_lines[$source_file_line_number];
    10021002        $source_file_line_number++;
    10031003    }
     
    10181018
    10191019    # Grab the source chunk text
    1020     my $source_file_chunk = @$source_file_lines[$source_file_chunk_starting_line_number];
     1020    my $source_file_chunk = $source_file_lines[$source_file_chunk_starting_line_number];
    10211021    for (my $l = ($source_file_chunk_starting_line_number + 1); $l <= $source_file_chunk_finishing_line_number; $l++) {
    1022         $source_file_chunk .= @$source_file_lines[$l];
     1022        $source_file_chunk .= $source_file_lines[$l];
    10231023    }
    10241024
     
    10651065    my (@file_lines) = @_;
    10661066
    1067     my %key_to_line_mapping = ();
     1067    my %chunk_key_to_line_mapping = ();
    10681068    for (my $i = 0; $i < scalar(@file_lines); $i++) {
    10691069    my $line = $file_lines[$i];
     
    10751075
    10761076        # Map from chunk key to line
    1077         $key_to_line_mapping{$chunk_key} = $i . "-" . $i;
    1078     }
    1079     }
    1080 
    1081     return %key_to_line_mapping;
     1077        $chunk_key_to_line_mapping{$chunk_key} = $i . "-" . $i;
     1078    }
     1079    }
     1080
     1081    return %chunk_key_to_line_mapping;
    10821082}
    10831083
     
    11191119{
    11201120    my $source_file = shift(@_);  # Not used
    1121     my $source_file_lines = shift(@_);
     1121    my @source_file_lines = @{shift(@_)};
    11221122    my $source_file_key_to_text_mapping = shift(@_);
    11231123    my $target_file = shift(@_);
    1124     my $target_file_lines = shift(@_);  # Not used
     1124    my @target_file_lines = @{shift(@_)};  # Not used
    11251125    my $target_file_key_to_text_mapping = shift(@_);
    11261126    my $target_file_key_to_comment_date_mapping = shift(@_);
     
    11281128
    11291129    # Build a mapping from chunk key to source file line, and from source file line to chunk key
    1130     my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_resource_bundle(@$source_file_lines);
     1130    my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_resource_bundle(@source_file_lines);
    11311131    my %source_file_line_to_key_mapping = ();
    11321132    foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
     
    11471147    my $source_file_chunk_finishing_line_number = (split(/-/, $line_key))[1];
    11481148    while ($source_file_line_number < $source_file_chunk_starting_line_number) {
    1149         print TARGET_FILE @$source_file_lines[$source_file_line_number];
     1149        print TARGET_FILE $source_file_lines[$source_file_line_number];
    11501150        $source_file_line_number++;
    11511151    }
     
    11731173
    11741174
     1175# ==========================================================================================
     1176#   GREENSTONE XML FUNCTIONS
     1177
     1178sub build_key_to_line_mapping_for_greenstone_xml
     1179{
     1180    my (@file_lines) = @_;
     1181
     1182    my %chunk_key_to_line_mapping = ();
     1183    for (my $i = 0; $i < scalar(@file_lines); $i++) {
     1184    my $line = $file_lines[$i];
     1185    $line =~ s/(\s*)$//;  # Remove any nasty whitespace, carriage returns etc.
     1186
     1187    # Line contains a string to translate
     1188    if ($line =~ /^\s*<Text id=\"(.*?)\">/) {
     1189        my $chunk_key = $1;
     1190        $line =~ s/\s*([^\\]\#[^\}]+)?$//;  # Remove any comments and nasty whitespace
     1191
     1192        # While there is still text of the string to go...
     1193        my $startline = $i;
     1194        while ($line !~ /<\/Text>$/) {
     1195        $i++;
     1196        if ($i == scalar(@file_lines)) {
     1197            &throw_fatal_error("Could not find end of string $chunk_key.");
     1198        }
     1199        $line = $file_lines[$i];
     1200        $line =~ s/\s*([^\\]\#[^\}]+)?$//;  # Remove any comments and nasty whitespace
     1201        }
     1202
     1203        # Map from chunk key to line
     1204        $chunk_key_to_line_mapping{$chunk_key} = $startline . "-" . $i;
     1205    }
     1206    }
     1207
     1208    return %chunk_key_to_line_mapping;
     1209}
     1210
     1211
     1212sub import_chunk_from_greenstone_xml
     1213{
     1214    my ($chunk_text) = @_;
     1215
     1216    # Simple: just remove the Text tags
     1217    $chunk_text =~ s/^\s*<Text .*id=\"(.*?)\".*?>(\s*)//;
     1218    $chunk_text =~ s/<\/Text>$//;
     1219
     1220    return $chunk_text;
     1221}
     1222
     1223
     1224sub get_greenstone_xml_chunk_comment_date
     1225{
     1226    my ($chunk_text) = @_;
     1227
     1228    # Check for an "Updated DD-MMM-YYYY" comment at the end of the chunk
     1229    # !! TO DO
     1230
     1231    return undef;
     1232}
     1233
     1234
     1235sub is_greenstone_xml_chunk_automatically_translated
     1236{
     1237    # No greenstone XML chunks are automatically translated
     1238    return 0;
     1239}
     1240
     1241
     1242sub write_translated_greenstone_xml
     1243{
     1244    my $source_file = shift(@_);  # Not used
     1245    my @source_file_lines = @{shift(@_)};
     1246    my $source_file_key_to_text_mapping = shift(@_);
     1247    my $target_file = shift(@_);
     1248    my @target_file_lines = @{shift(@_)};  # Not used
     1249    my $target_file_key_to_text_mapping = shift(@_);
     1250    my $target_file_key_to_comment_date_mapping = shift(@_);
     1251    my $target_language_code = shift(@_);  # Not used
     1252
     1253    # Build a mapping from chunk key to source file line, and from source file line to chunk key
     1254    my %source_file_key_to_line_mapping = &build_key_to_line_mapping_for_greenstone_xml(@source_file_lines);
     1255    my %source_file_line_to_key_mapping = ();
     1256    foreach my $chunk_key (keys(%source_file_key_to_line_mapping)) {
     1257    $source_file_line_to_key_mapping{$source_file_key_to_line_mapping{$chunk_key}} = $chunk_key;
     1258    }
     1259
     1260    # Write the new target file
     1261    my $target_file_path = &util::filename_cat($gsdl_root_directory, $target_file);
     1262    if (!open(TARGET_FILE, ">$target_file_path")) {
     1263    &throw_fatal_error("Could not write target file $target_file_path.");
     1264    }
     1265
     1266    # Model the new target file on the source file, with the target file translations
     1267    my $source_file_line_number = 0;
     1268    foreach my $line_key (sort sort_by_line (keys(%source_file_line_to_key_mapping))) {
     1269    # Fill in the gaps before this chunk starts
     1270    my $source_file_chunk_starting_line_number = (split(/-/, $line_key))[0];
     1271    my $source_file_chunk_finishing_line_number = (split(/-/, $line_key))[1];
     1272    while ($source_file_line_number < $source_file_chunk_starting_line_number) {
     1273        print TARGET_FILE $source_file_lines[$source_file_line_number];
     1274        $source_file_line_number++;
     1275    }
     1276    $source_file_line_number = $source_file_chunk_finishing_line_number + 1;
     1277
     1278    my $chunk_key = $source_file_line_to_key_mapping{$line_key};
     1279    my $source_file_chunk_text = $source_file_key_to_text_mapping->{$chunk_key};
     1280    my $target_file_chunk_text = $target_file_key_to_text_mapping->{$chunk_key} || "";
     1281
     1282    # If no translation exists for this chunk, show this, and move on
     1283    if ($source_file_chunk_text ne "" && $target_file_chunk_text eq "") {
     1284        print TARGET_FILE "<!-- Missing translation: $chunk_key -->\n";
     1285        next;
     1286    }
     1287
     1288    print TARGET_FILE "<Text id=\"$chunk_key\">$target_file_chunk_text</Text>";
     1289    if ($target_file_key_to_comment_date_mapping->{$chunk_key}) {
     1290        # print TARGET_FILE "  # Updated " . $target_file_key_to_comment_date_mapping->{$chunk_key};
     1291    }
     1292    print TARGET_FILE "\n";
     1293    }
     1294
     1295    # Fill in the end of the file
     1296    while ($source_file_line_number < scalar(@source_file_lines)) {
     1297    print TARGET_FILE $source_file_lines[$source_file_line_number];
     1298    $source_file_line_number++;
     1299    }
     1300
     1301    close(TARGET_FILE);
     1302}
     1303
     1304
    11751305&main(@ARGV);
Note: See TracChangeset for help on using the changeset viewer.