The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

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