aboutsummaryrefslogtreecommitdiff
path: root/install-hyphenation
blob: 7ee906025b6edcd4cfb90bd2470273b3b100ea61 (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
#!/usr/bin/env perl

use POSIX;
use File::Basename;

sub getpattendir () {
   # Extract pattern directory from Makefile, in the same directory as
   # this script. A complete interpretation of the Makefiles would be
   # too complicated, so only ${prefix} is read, which is (hopefully)
   # defined as constant.
   
   my $mf = (dirname $0) . "/Makefile", $prefix = "";
   open MF, $mf or die "Cannot open $mf for reading: $!";
   while (<MF>) {
      if (/^\s*prefix\s*=\s*(.*)\s*$/) {
         $prefix = $1;
     }        
   }

   close MF;
   
   if ($prefix eq "") {
      die "prefix not defined in $mf";
   }
   
   return "$prefix/lib/dillo/hyphenation";
}


sub printhelp {
   print "   Syntax: install-hyphenation <lang1> [<lang2> ...]\n";
   print "\n";
   print "   If there are multiple pattern files for a language, and so the file name\n";
   print "   is not \"hyph-<lang>.tex\", you can specify the origin by using a colon:\n";
   print "   \"<src-lang>:<dest-lang>\", eg. \"de-1996:de\". If both are identical, the\n";
   print "   colon can be committed; \"ru\" is equivalent to \"ru:ru\".\n";

}

sub trpatfile {
   # Extract the data needed by dillo from the TeX file, based on two
   # rules:
   #
   # 1. Extract the argument of the "\pattern" sequence, i. e.
   #    anything between "\pattern{" and the next "}".
   # 2. As an exception, comments are always preserved.

   my $url = $_[0], $in = $_[1], $out = $_[2];
   
   open IN, $in or die die "Cannot open $in for reading: $!";
   open OUT, "> $out" or die "Cannot open $out for writing: $!";
   
   printf OUT "%% The original file was downloaded from\n";
   printf OUT "%%\n";
   printf OUT "%%    $url\n";
   printf OUT "%%\n";
   printf OUT "%% and automatically translated into this format. The original comments,\n";
   printf OUT "%% including the original copyright notice, are preserved.\n";
   printf OUT "%%\n";
   printf OUT "%% -----------------------------------------------------------------------------\n";
   printf OUT "%%\n";
   
   $inpatterns = 0;
   while (<IN>) {
      if (/^%/) {
         # Adopt all comments
         print OUT;
      } elsif (!$inpatterns) {
         if (/\\patterns\s*{/) {
            $inpatterns = 1;
        }
     } else {
        if (/}/) {
           $inpatterns = 0;
        } else {
           print OUT;
        }
      }
   }
   
   close IN;
   close OUT;

   printf "Wrote pattern file: $out\n";
}

sub dlpatfile {
   my $lang = $_[0], $destdir = $_[1], $lang1, $lang2;
   if ($lang =~ /(.*):(.*)/) {
      $lang1 = $1;
      $lang2 = $2;
   } else {
      $lang1 = $lang2 = $lang;
   }
   
   my $url = "ftp://ftp.mpi-sb.mpg.de/pub/tex/mirror/ftp.dante.de/pub/tex/language/hyph-utf8/tex/generic/hyph-utf8/patterns/tex/hyph-$lang1.tex";
   my $tmp = tmpnam();
   if (system("wget -O $tmp $url") == 0) {
      my $out = "$destdir/$lang2.pat";
      trpatfile $url, $tmp, $out;
      unlink $tmp;
   } else {
      print "Error downloading patterns for language $lang1. See messages above for details.\n"
   }
}

$destdir = getpattendir;

foreach $lang (@ARGV) {
   if ($lang eq '-?' || $lang eq '--?' || $lang eq '-h' ||
       $lang eq '--h' || $lang eq '-help' || $lang eq '--help') {
      printhelp;
   } else {
      dlpatfile $lang, $destdir;
   }
}