The R Project SVN R

Rev

Rev 21786 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21786 Rev 25447
1
### $Id: pkg2tex.pl,v 1.3 2002/11/03 17:32:29 hornik Exp $
1
### $Id: pkg2tex.pl,v 1.4 2003/07/29 10:35:13 ripley Exp $
2
 
2
 
3
## Create a single pkgname-pkg.tex file from the Latex subdirectories
3
## Create a single pkgname-pkg.tex file from the Latex subdirectories
4
## Copyright (C) 1998 Douglas M. Bates <bates@stat.wisc.edu>
4
## Copyright (C) 1998 Douglas M. Bates <bates@stat.wisc.edu>
5
 
5
 
6
## This file is free software; you can redistribute it and/or modify
6
## This file is free software; you can redistribute it and/or modify
7
## it under the terms of the GNU General Public License as published by
7
## it under the terms of the GNU General Public License as published by
8
## the Free Software Foundation; either version 2, or (at your option)
8
## the Free Software Foundation; either version 2, or (at your option)
9
## any later version.
9
## any later version.
10
 
10
 
11
## This file is distributed in the hope that it will be useful,
11
## This file is distributed in the hope that it will be useful,
12
## but WITHOUT ANY WARRANTY; without even the implied warranty of
12
## but WITHOUT ANY WARRANTY; without even the implied warranty of
13
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
## GNU General Public License for more details.
14
## GNU General Public License for more details.
15
 
15
 
16
## A copy of the GNU General Public License is available via WWW at
16
## A copy of the GNU General Public License is available via WWW at
17
## http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
17
## http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
18
## writing to the Free Software Foundation, Inc., 59 Temple Place, 
18
## writing to the Free Software Foundation, Inc., 59 Temple Place, 
19
## Suite 330, Boston, MA  02111-1307  USA
19
## Suite 330, Boston, MA  02111-1307  USA
20
 
20
 
21
## Send any bug reports to bates@stat.wisc.edu
21
## Send any bug reports to bates@stat.wisc.edu
22
 
22
 
23
## <NOTE>
23
## <NOTE>
24
## Could use &file_path() to make this portable.
24
## Could use &file_path() to make this portable.
25
## </NOTE>
25
## </NOTE>
26
 
26
 
27
use strict;
27
use strict;
28
use FileHandle;
28
use FileHandle;
29
use Carp;
29
use Carp;
30
use Getopt::Long;
30
use Getopt::Long;
-
 
31
use R::Utils;
31
 
32
 
32
my $help;
33
my $help;
33
 
34
 
34
my $revision = ' $Revision: 1.3 $ ';
35
my $revision = ' $Revision: 1.4 $ ';
35
my $version;
36
my $version;
36
my $name;
37
my $name;
37
 
38
 
38
($name = $0) =~ s|.*/||;
39
($name = $0) =~ s|.*/||;
39
$revision =~ / ([\d\.]*) /;
40
$revision =~ / ([\d\.]*) /;
40
$version = $1;
41
$version = $1;
41
 
42
 
42
GetOptions("help|h" => \$help);
43
GetOptions("help|h" => \$help);
43
&usage() if $help;
44
&usage() if $help;
44
&usage() if $#ARGV < 0;
45
&usage() if $#ARGV < 0;
45
 
46
 
46
my $RLIB;
47
my $RLIB;
47
if ($ENV{'RLIB'}) {
48
if ($ENV{'RLIB'}) {
48
    ## Set under Windows, but also useful to override the default.
49
    ## Set under Windows, but also useful to override the default.
49
    $RLIB = $ENV{'RLIB'};
50
    $RLIB = $ENV{'RLIB'};
50
} else {
51
} else {
51
    $RLIB = "../../library";
52
    $RLIB = "../../library";
52
}
53
}
53
 
54
 
54
for (@ARGV) {
55
for (@ARGV) {
55
    my $latexDir = $RLIB . "/" . $_ . "/latex/";
56
    my $latexDir = $RLIB . "/" . $_ . "/latex/";
56
    carp "latex subdirectory for library $_ does not exist!\n", next
57
    carp "latex subdirectory for library $_ does not exist!\n", next
57
	unless -d $latexDir;
58
	unless -d $latexDir;
-
 
59
    my $was_zipped = 0;
-
 
60
    if(-f $latexDir . "Rhelp.zip") {
-
 
61
	$was_zipped = 1;
-
 
62
	my $cmd = "cd $latexDir; unzip -qo Rhelp.zip";
-
 
63
	croak "Cannot unzip latex files\n" if R_system($cmd);
-
 
64
    }
58
    my $out = new FileHandle "> " . $_ . "-pkg.tex" or
65
    my $out = new FileHandle "> " . $_ . "-pkg.tex" or
59
	croak "unable to open file $_-pkg.tex: $!\n";
66
	croak "unable to open file $_-pkg.tex: $!\n";
60
    &do_header($_, $out);
67
    &do_header($_, $out);
61
    &do_tex_files($latexDir, $out);
68
    &do_tex_files($latexDir, $out);
62
    &do_trailer($out);
69
    &do_trailer($out);
63
    $out->close;
70
    $out->close;
-
 
71
    if($was_zipped) {
-
 
72
	croak "Removing unzipped latex files failed.\n" if
-
 
73
	    R_system("rm -f $latexDir*.tex");
-
 
74
    }
64
}
75
}
65
 
76
 
66
sub do_header {
77
sub do_header {
67
    my( $pkgname, $outfile ) = @_;
78
    my( $pkgname, $outfile ) = @_;
68
    $outfile->print("\n\\chapter\{The \\texttt\{$pkgname\} package\}\n");
79
    $outfile->print("\n\\chapter\{The \\texttt\{$pkgname\} package\}\n");
69
}
80
}
70
 
81
 
71
sub foldorder {uc($a) cmp uc($b) or $a cmp $b;}
82
sub foldorder {uc($a) cmp uc($b) or $a cmp $b;}
72
sub do_tex_files {
83
sub do_tex_files {
73
    my( $latexDir, $outfile ) = @_;
84
    my( $latexDir, $outfile ) = @_;
74
    my $fh = new FileHandle;
85
    my $fh = new FileHandle;
75
    my $fname;
86
    my $fname;
76
    my $fline;
87
    my $fline;
77
    my %filenames;
88
    my %filenames;
78
 
89
 
79
    opendir DIR, $latexDir or
90
    opendir DIR, $latexDir or
80
	croak "can't open directory $latexDir: $!\n";
91
	croak "can't open directory $latexDir: $!\n";
81
    foreach $fname ( grep /^[A-za-z].*\.tex$/, readdir DIR ) 
92
    foreach $fname ( grep /^[A-za-z].*\.tex$/, readdir DIR ) 
82
    {
93
    {
83
	$fh->open( $latexDir . $fname ) 
94
	$fh->open( $latexDir . $fname ) 
84
	    or croak "unable to open file $_:$!\n";
95
	    or croak "unable to open file $_:$!\n";
85
	$fline = <$fh>;
96
	$fline = <$fh>;
86
	## first line is \Header{object}{...}
97
	## first line is \Header{object}{...}
87
	$fline =~ s/\\Header\{\s*([^}]*)\}//;
98
	$fline =~ s/\\Header\{\s*([^}]*)\}//;
88
	$filenames{$1} = $fname;
99
	$filenames{$1} = $fname;
89
    }
100
    }
90
    close $fh;
101
    close $fh;
91
 
102
 
92
    foreach $fname (sort foldorder keys %filenames)
103
    foreach $fname (sort foldorder keys %filenames)
93
    {
104
    {
94
	$fh->open( $latexDir . $filenames{$fname} ) 
105
	$fh->open( $latexDir . $filenames{$fname} ) 
95
	    or croak "unable to open file $_:$!\n";
106
	    or croak "unable to open file $_:$!\n";
96
	$outfile->print( <$fh> );
107
	$outfile->print( <$fh> );
97
    }
108
    }
98
    close $fh;
109
    close $fh;
99
}
110
}
100
 
111
 
101
sub do_trailer {
112
sub do_trailer {
102
    my $outfile = shift;
113
    my $outfile = shift;
103
    $outfile->print("\\clearpage");
114
    $outfile->print("\\clearpage");
104
}
115
}
105
 
116
 
106
sub usage {
117
sub usage {
107
 
118
 
108
    print "$name version $version\n";
119
    print "$name version $version\n";
109
    print "Usage: $name [--help/-h] file ..." ;
120
    print "Usage: $name [--help/-h] file ..." ;
110
 
121
 
111
    exit 0;
122
    exit 0;
112
}
123
}
113
 
124
 
114
### Local variables: ***
125
### Local variables: ***
115
### mode: perl ***
126
### mode: perl ***
116
### perl-indent-level: 4 ***
127
### perl-indent-level: 4 ***
117
### End: ***
128
### End: ***