summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/mkgrkindex/mkgrkindex
blob: a4d30438f1b9e403b743e1a29443ace7bdebfdf9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/env perl
#
#(c) Copyright 1998-2009   Apostolos Syropoulos
#                          asyropoulos@yahoo.com
#
# The LaTeX Project Public License (lppl) 
# This software is copyright but you are granted a license which gives you, 
# the "user" of the software, legal permission to copy, distribute, and/or 
# modify the software. However, if you modify the software and then distribute 
# it (even just locally) you must change the name of the software, or use other 
# technical means to avoid confusion. 
#
$makeindex = "makeindex"; #name of the index generation utility
sub g2e
{
   $word = $_[0];
   $tword = "";
   foreach $_ (split(//, $word))
   {
     if    (/^a|^A|^á|^Á|^Ü|^\xA2|^\xB6/) { $tword .= "a";}
     elsif (/^b|^B|^â|^Â/)                { $tword .= "b"; }
     elsif (/^d|^D|^ä|^Ä/)                { $tword .= "d"; } 
     elsif (/^e|^E|^å|^Å|^Ý|^¸/)          { $tword .= "e"; }
     elsif (/^i|^I|^é|^É|^ß|^º|^À/)       { $tword .= "i"; }
     elsif (/^o|^O|^ï|^Ï|^ü|^¼/)          { $tword .= "o"; } 
     elsif (/^p|^P|^ð|^Ð/)                { $tword .= "p"; }
     elsif (/^g|^G|^ã|^Ã/)                { $tword .= "c"; }
     elsif (/^z|^Z|^æ|^Æ/)                { $tword .= "f"; }
     elsif (/^h|^H|^ç|^Ç|^Þ|^¹/)          { $tword .= "g"; }
     elsif (/^j|^J|^è|^È/)                { $tword .= "h"; }
     elsif (/^k|^K|^ê|^Ê/)                { $tword .= "j"; }
     elsif (/^l|^L|^ë|^Ë/)                { $tword .= "k"; }
     elsif (/^m|^M|^ì|^Ì/)                { $tword .= "l"; }
     elsif (/^n|^N|^í|^Í/)                { $tword .= "m"; }
     elsif (/^x|^X|^î|^Î/)                { $tword .= "n"; }
     elsif (/^r|^R|^ñ|^Ñ/)                { $tword .= "q"; }
     elsif (/^s|^c|^S|^ó|^ò|^Ó/)          { $tword .= "r"; }
     elsif (/^t|^T|^ô|^Ô/)                { $tword .= "s"; }
     elsif (/^u|^U|^õ|^Õ|^ý|^¾|^à/)       { $tword .= "t"; }
     elsif (/^f|^F|^ö|^Ö/)                { $tword .= "u"; }
     elsif (/^q|^Q|^÷|^×/)                { $tword .= "v"; }
     elsif (/^y|^Y|^ø|^Ø/)                { $tword .= "w"; }
     elsif (/^w|^W|^ù|^Ù|^þ|^¿/)          { $tword .= "x"; }
     elsif (/^'|^`|^~|^<|^>/)             {                }
     else                                 { $tword .= $_   }
   }
   return $tword;
}

$argc = @ARGV;

if ($argc == 0 || $argc > 4 ) # no command line arguments or more than 3
{                             # arguments
   die "Usage: mkgrkindex.pl [-s A|a] [-l] index\n";
}
else
{
   print "This is mkgrkindex (previously known as mkindex) version 2.0\n";
   
      $style = 0;
      $is_latin = 0;
       
         SWITCHES: 
         while($_ = $ARGV[0])
         {  
            shift;
            if (/^-s/)
            {
               if ($ARGV[0] eq "a")
               {
                  $style = -1;
               }
               elsif ($ARGV[0] eq "A")
               {
                  $style = 1;
               }
               else
               {
                  die "$ARGV[0]: Illegal argument for switch -s\n";
               }
               shift;  
            }
            elsif (/^-l/)
            {
               $is_latin = 1;
            }
            elsif (/^-\w+/)
            {
               die "$_: Illegal command line switch!\n";
            }
            else
            {
               $file = $_;
            } 
         }    
         die "No index file name specified!\n" if $file eq "";
            
         if (! (-e $file))
         {
             die "$file: no such file!\n" if $file =~ /.+\..+/;
             die "$file.idx: no such file!\n" if (! (-e "$file.idx"));
             $index_file = "$file.idx";
         }
         else
         {
             $index_file = $file;
             $file = $1 if $index_file =~ /(.+)\..+/; 
         }
   

}


$old_file="$index_file.old";

if ($is_latin == 0)
{
   rename $index_file, $old_file;
   open(OLD, "$old_file") || die "Can't open file $old_file\n";
   open(NEW, ">$index_file") || die "Can't open file $index_file\n";
   
   while (<OLD>)
   {
       #\x7B = {, \x7D = },\x40 = @, \x7C = |, \x21 = !, \x28 = (, \x29 = )
       chomp;
       #Nikos Platis has suggested to replace the original code with a better
       #parsing method. The code that follows is essentially Nikos's code with
       #some minnor modifications, as explained in the documentation.
       $newentry = "";
       /^\\indexentry\x7B(.+)\x7D(.+)/;
       $fullentry = $1;
       $page = $2;
       ($indexentry, $format) = split(/(?<!")\x7C/, $fullentry);
       @entryparts = split(/(?<!")\x21/, $indexentry);
       $numparts = @entryparts;
       for ($i = 0; $i < $numparts; $i++)  {
         ($x, $y) = split(/(?<!")\x40/, @entryparts[$i]);
         if ($i > 0)  {
           $newentry .= "!"
         }
         $tx = g2e($x);
         if ($y) {
           $newentry .= "$tx\@$y"
         }
         else {
           $newentry .= "$tx\@$x";
         }
       }
       print NEW "\\indexentry{$newentry";
       if ($format) {
         print NEW "|$format"
       }
       print NEW "}$page\n"
   }    

   close OLD;
   close NEW;
   unlink $old_file;
} 

rename $index_file, $old_file;
open(OLD, "$old_file") || die "Can't open file $old_file\n";
open(NEW, ">$index_file") || die "Can't open file $index_file\n";

while(<OLD>)
{
    chomp($_);
    if (/\\indexentry\x7B(.+)\x7D\x7B\\textlatin\s*?\x7B(\w+)\x7D\x7D/)
    {
       print NEW "\\indexentry{$1|textlatin}{$2}\n";
    }
    else
    {
       print NEW "$_\n";
    }
}


close OLD;
close NEW;
unlink $old_file;


if ($style < 0)
{
   system("$makeindex -s alpha.ist $file");
}
elsif ($style > 0)
{
   system("$makeindex -s Alpha.ist $file");
}
else
{
   system("$makeindex $file");
}

if ($style != 0)
{
   
   
   %e2g = (
             'a' => 'á', 'A' => 'Á',
             'b' => 'â', 'B' => 'Â',
             'c' => 'ã', 'C' => 'Ã',
             'd' => 'ä', 'D' => 'Ä',
             'e' => 'å', 'E' => 'Å',
             'f' => 'æ', 'F' => 'Æ',
             'g' => 'ç', 'G' => 'Ç',
             'h' => 'è', 'H' => 'È',
             'i' => 'é', 'I' => 'É',
             'j' => 'ê', 'J' => 'Ê',
             'k' => 'ë', 'K' => 'Ë',
             'l' => 'ì', 'L' => 'Ì',
             'm' => 'í', 'M' => 'Í',
             'n' => 'î', 'N' => 'Î',
             'o' => 'ï', 'O' => 'Ï',
             'p' => 'ð', 'P' => 'Ð',
             'q' => 'ñ', 'Q' => 'Ñ',
             'r' => 'óv', 'R' => 'Ó',
             's' => 'ô', 'S' => 'Ô',
             't' => 'õ', 'T' => 'Õ',
             'u' => 'ö', 'U' => 'Ö',
             'v' => '÷', 'V' => '×',
             'w' => 'ø', 'W' => 'Ø',
             'x' => 'ù', 'X' => 'Ù'
           );

   $ind_file = "$file.ind";
   $old_file="$ind_file.old";

   if ($is_latin == 0)
   {
      rename $ind_file, $old_file;
      open(OLD, "$old_file") || die "Can't open file $old_file\n";
      open(NEW, ">$ind_file") || die "Can't open file $ind_file\n";
      
      while (<OLD>)
      {
           if (/^{\\hfil (\w?) \\hfil}/)
           {
              $lettergr = $e2g{$1};
              print NEW "{\\hfil $lettergr \\hfil}\\nopagebreak\n";
           }
           else
           {
              print NEW ;
           }
      }

      close OLD;
      close NEW;
      unlink $old_file;
   }

   rename $ind_file, $old_file;
   open(OLD, "$old_file") || die "Can't open file $old_file\n";
   open(NEW, ">$ind_file") || die "Can't open file $ind_file\n";
   
   while (<OLD>)
   {
      if (/^\x7B\\hfil (\w)ymbols \\hfil\x7D/)
      {
          if ($1 eq "s")
          {
                 print NEW "{\\hfil \\textgreek{\\textbf{s'umbola}}";
                 print NEW "\\hfil}\\nopagebreak\n";
          }
          elsif ($1 eq "S")
          {
                 print NEW "{\\hfil \\textgreek{\\textbf{S'umbola}}";
                 print NEW "\\hfil}\\nopagebreak\n";
          }
          else
          {
             die "Illegal header $1 in .ind file\n";
          }
      }
      else
      {
         print NEW;
      }
   }

   close OLD;
   close NEW;
   unlink $old_file;

}
__END__