File: pkg2tex.pl

package info (click to toggle)
r-base 2.4.0.20061125-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 53,384 kB
  • ctags: 27,223
  • sloc: ansic: 228,006; fortran: 76,848; sh: 12,670; perl: 9,489; makefile: 6,064; tcl: 2,954; yacc: 2,152; java: 486; cpp: 457; asm: 275; sed: 16
file content (136 lines) | stat: -rw-r--r-- 3,581 bytes parent folder | download | duplicates (3)
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
## Create a single pkgname-pkg.tex file from the Latex subdirectories
## Copyright (C) 1998 Douglas M. Bates <bates@stat.wisc.edu>

## This file is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.

## This file is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## A copy of the GNU General Public License is available via WWW at
## http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
## writing to the Free Software Foundation, Inc., 51 Franklin Street,
## Fifth Floor, Boston, MA 02110-1301  USA.

## Send any bug reports to bates@stat.wisc.edu

## <NOTE>
## Could use &file_path() to make this portable.
## </NOTE>

use strict;
use FileHandle;
use Carp;
use Getopt::Long;
use R::Utils;

my $help;

my $revision = ' $Revision: 1.4 $ ';
my $version;
my $name;

($name = $0) =~ s|.*/||;
$revision =~ / ([\d\.]*) /;
$version = $1;

GetOptions("help|h" => \$help);
&usage() if $help;
&usage() if $#ARGV < 0;

my $RLIB;
if ($ENV{'RLIB'}) {
    ## Set under Windows, but also useful to override the default.
    $RLIB = $ENV{'RLIB'};
} else {
    $RLIB = "../../library";
}

for (@ARGV) {
    my $latexDir = $RLIB . "/" . $_ . "/latex/";
    carp "latex subdirectory for library $_ does not exist!\n", next
	unless -d $latexDir;
    my $was_zipped = 0;
    if(-f $latexDir . "Rhelp.zip") {
	$was_zipped = 1;
	my $cmd = "cd $latexDir; unzip -qo Rhelp.zip";
	croak "Cannot unzip latex files\n" if R_system($cmd);
    }
    my $out = new FileHandle "> " . $_ . "-pkg.tex" or
	croak "unable to open file $_-pkg.tex: $!\n";
    &do_header($_, $out);
    &do_tex_files($latexDir, $out);
    &do_trailer($out);
    $out->close;
    if($was_zipped) {
	croak "Removing unzipped latex files failed.\n" if
	    R_system("rm -f $latexDir*.tex");
    }
}

sub do_header {
    my( $pkgname, $outfile ) = @_;
    $outfile->print("\n\\chapter\{The \\texttt\{$pkgname\} package\}\n");
}

sub foldorder {($b =~ /-package$/) cmp ($a =~ /-package$/) or uc($a) cmp uc($b) or $a cmp $b;}

sub do_tex_files {
    my( $latexDir, $outfile ) = @_;
    my $fh = new FileHandle;
    my $fname;
    my $fline;
    my %filenames;
    my $internal;

    opendir DIR, $latexDir or
	croak "can't open directory $latexDir: $!\n";
    foreach $fname ( grep /^[A-za-z].*\.tex$/, readdir DIR ) 
    {
	$fh->open( $latexDir . $fname ) 
	    or croak "unable to open file $_:$!\n";
	    
	## first line is usually \HeaderA{object}{object}{...}	
	## but may need to skip \inputencoding line	
	$fline = <$fh> until $fline =~ s/\\HeaderA\{\s*([^}]*)\}//;

        ## omit internal help pages
        my $internal = 0;
        while(<$fh>) {
	    if(/\\keyword\{\s*internal\s*\}/) { $internal = 1; last; }
	}
        next if $internal;
	$filenames{$1} = $fname;
    }
    close $fh;

    foreach $fname (sort foldorder keys %filenames)
    {
	$fh->open( $latexDir . $filenames{$fname} ) 
	    or croak "unable to open file $_:$!\n";
	$outfile->print( <$fh> );
    }
    close $fh;
}

sub do_trailer {
    my $outfile = shift;
    $outfile->print("\\clearpage");
}

sub usage {

    print "$name version $version\n";
    print "Usage: $name [--help/-h] file ..." ;

    exit 0;
}

### Local variables: ***
### mode: perl ***
### perl-indent-level: 4 ***
### End: ***