fixnavi.pl 1.72 KB
Newer Older
1 2 3 4
#! /usr/bin/perl -w

use strict;

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
5
@ARGV == 1 or die "usage: $0 <qdoc-file>\n";
6
my $file = $ARGV[0];
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
7
open FILE, $file or die "File $file cannot be opened.\n";
8 9 10 11 12 13
my @toc = ();
my %title2page = ();
my $doctitle = "";
my $curpage = "";
my $intoc = 0;
while (<FILE>) {
14 15 16 17
    if (keys(%title2page) == 1 && /^\h*\\list/) {
        $intoc++;
    } elsif (!$intoc) {
        if (/^\h*\\page\h+(\H+)/) {
18 19 20
            $curpage = $1;
        } elsif (/^\h*\\title\h+(.+)$/) {
            if ($curpage eq "") {
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
21
                die "Title '$1' appears in no \\page.\n";
22 23 24 25 26 27 28
            }
            $title2page{$1} = $curpage;
            $doctitle = $1 if (!$doctitle);
            $curpage = "";
        }
    } else {
        if (/^\h*\\endlist/) {
29
            $intoc--;
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
        } elsif (/^\h*\\o\h+\\l{(.*)}$/) {
            push @toc, $1;
        }
    }
}
close FILE;

my %prev = ();
my %next = ();
my $last = $doctitle;
for my $title (@toc) {
    $next{$last} = $title2page{$title};
    $prev{$title} = $title2page{$last};
    $last = $title;
}

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
46 47
open IN, $file or die "File $file cannot be opened a second time?!\n";
open OUT, '>'.$file.".out" or die "File $file.out cannot be created.\n";
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
my $cutting = 0;
while (<IN>) {
    if (!$cutting) {
        if (/^\h*\\contentspage/) {
            $cutting = 1;
        }
    } else {
        if (/^\h*\\title\h+(.+)$/) {
            print OUT "    \\previouspage ".$prev{$1} if ($prev{$1});
            print OUT "    \\page ".$title2page{$1};
            print OUT "    \\nextpage ".$next{$1} if ($next{$1});
            print OUT "\n";
            $cutting = 0;
        } else {
            next;
        }
    }
    print OUT $_;
}
close OUT;
close IN;

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
70
rename($file.".out", $file) or die "Cannot replace $file with new version.\n";