Chameleon

Chameleon Svn Source Tree

Root/branches/blackosx/package/Resources.source/ConvertResourcesTextFile.pl

  • Property svn:executable set to *
1#!/usr/bin/perl
2
3# This script converts a human readable file for all the
4# language 'Resources' for the Chameleon package installer
5# in to a simple format ready for merging with pre-defined
6# templates during compilation.
7#
8# The human readable file is hosted on Google Docs at:
9# www.......
10# The spreadsheet is a published file and will be
11# automatically downloaded as a UTF-8, tabbed delimited
12# text file which will be savedd in the same folder as
13# this script.
14#
15# Call this script with:
16# ./ConvertResourcesTextFile.pl ResourcesSourceFile.txt
17#
18#
19
20use LWP::Simple;
21
22if ($#ARGV < 1) {
23 print stderr "A destination path and filename is needed\n";
24} else {
25 $destination_dir=$ARGV[0];
26 $destination_file=$ARGV[1];
27}
28
29sub set_indent {
30# Set indent to 4 spaces X the number of fields deep currently at.
31# $_[0] = Headed field number (not including blanks) currently being processed.
32 $new_indent=" ";
33 for ($l = 0; $l < $_[0]; $l++) {
34 $new_indent=$new_indent." ";
35 }
36 return $new_indent;
37}
38
39sub adjust_indent {
40# Pull back indent if Headed fields previous to current one haven't yet been populated.
41# $_[0] = Current indent being used which would have come from function set_indent().
42# $_[1] = Headed field number (not including blanks) currently being processed.
43 $new_indent= $_[0];
44 for ($p = 0; $p < $_[1]; $p++) {
45 if ( $fieldsWithHeadersSet[$p] eq 0 ) {
46 $new_indent = substr($new_indent, 0, -4);
47 }
48 }
49 return $new_indent;
50}
51
52sub calculateIndent {
53# Find number of indents required by matching name of fieldsWithoutLanguage to position in fieldsWithHeaders.
54 for($i = 0; $i < scalar(@fieldsWithHeaders); $i++) {
55 if ( $fieldsWithHeaders[$i] eq $fieldsWithoutLanguage[$loop] ) {
56 $indent=set_indent($i);
57 last;
58 }
59 }
60 $indent=adjust_indent($indent,$i); # Are there any empty 'headed' fields preceding this one? if so, adjust.
61}
62
63#--------------------------------------------------------------------------
64$googlePublishedDoc = 'https://docs.google.com/spreadsheet/pub?key=0Aj0jJ2rdmK_sdFdNbm45NlpNYU1PcjRmOHVXX0FNa3c&single=true&gid=0&output=txt';
65$sourceFileToRead="PackageInstallerResourceText.tsv";
66getstore ($googlePublishedDoc, $destination_dir."/".$sourceFileToRead) or die "Couldn't get master file";
67#--------------------------------------------------------------------------
68
69open (FILE, "$destination_dir"."/"."$sourceFileToRead");
70open (OUTFILE, ">$destination_dir"."/"."$destination_file");
71
72print OUTFILE "\n"; # Output blank line.
73 # This is needed so after converting to rtf using textutil in later bash script
74 # the rtf description header appears on it's own line. For example: \f0\fs24 \cf0..
75while (<FILE>) {
76 $indent = " ";
77 chomp;
78 s/&amp;/and/g; # Handle '&amp;' for now by converting to 'and'.
79 s/&/and/g; # Handle '&' for now by converting to 'and'.
80 @fieldsReadIn = split("\t");
81 $startFirstField=substr($fieldsReadIn[0],0,1); # First character of first field.
82$startSecondField=substr($fieldsReadIn[1],0,1); # First character of second field.
83
84 if ($startFirstField ne "#" ) { # Ignore lines beginning with a hash.
85 if ($startFirstField ne "") { # Check first field for language identifier.
86 print OUTFILE "language: $fieldsReadIn[0]\n"; # Output first field - LANGUAGE.
87 }
88 $headedFieldCount=0;
89 if ($startSecondField ne "") { # Check second field for language identifier.
90 print OUTFILE " file: $fieldsReadIn[1]\n"; # Output second field - FILE.
91 @fieldsWithHeaders = (); # Clear array which stores only field names with titles and NOT blanks.
92 for($i = 1; $i <= scalar(@fieldsReadIn); $i++) { # Loop through each field - ignoring first field [0].
93 @fieldsWithoutLanguage[$i]=$fieldsReadIn[$i]; # copy each field in to 'fieldsWithoutLanguage' array.
94 if ( $fieldsWithoutLanguage[$i] ne "" ) { # Check for NON-Blank fields headers
95 push(@fieldsWithHeaders, $fieldsWithoutLanguage[$i]); # Store in to 'fieldsWithHeaders' array.
96 }
97 }
98 }
99
100 for ($loop = 1; $loop < scalar(@fieldsReadIn); $loop++) {
101 if ( $fieldsReadIn[$loop] ne "" && $fieldsWithoutLanguage[$loop] ne "" ) {
102 if ( $fieldsReadIn[$loop] ne $fieldsWithoutLanguage[$loop]) {
103 $headedFieldCount++;
104 $indent=calculateIndent;
105 print OUTFILE $indent."$fieldsWithoutLanguage[$loop]: $fieldsReadIn[$loop]\n";
106 $fieldsWithHeadersSet[$headedFieldCount]=1; # Set value in this array to mark that we've written to it.
107 }
108 }
109 }
110 }
111}
112close (FILE);
113close (OUTFILE);
114unlink("$destination_dir"."/"."$sourceFileToRead"); # Remove downloaded Google Doc file.
115exit;

Archive Download this file

Revision: 1834