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

use strict;

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
my @files = ();
my %defines = ();
for (@ARGV) {
    if (/^-D(.*)$/) {
        $defines{$1} = 1;
    } elsif (/^-/) {
        printf STDERR "Unknown option '".$_."'\n";
        exit 1;
    } else {
        push @files, $_;
    }
}

int(@files) or die "usage: $0 [-D<define>]... <qdoc-file>\n";

20 21 22 23 24
my @toc = ();
my %title2page = ();
my $doctitle = "";
my $curpage = "";
my $intoc = 0;
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
my %prev_skips = ();
my %next_skips = ();
my %define_skips = ();
my %polarity_skips = ();
my $prev_skip = "";
my $next_skip = "";
my $define_skip = "";
my $polarity_skip = 0;
for my $file (@files) {
    my $skipping = 0; # no nested ifs!
    open FILE, $file or die "File $file cannot be opened.\n";
    while (<FILE>) {
        if (/^\h*\\if\h+defined\h*\(\h*(\H+)\h*\)/) {
            $skipping = !defined($defines{$1});
            if (!$intoc) {
                $define_skip = $1;
                $polarity_skip = $skipping;
            }
        } elsif (/^\h*\\else/) {
            $skipping = 1 - $skipping;
        } elsif (/^\h*\\endif/) {
            $skipping = 0;
        } elsif (keys(%title2page) == 1 && /^\h*\\list/) {
            $intoc++;
        } elsif (!$intoc) {
            if ($skipping && /^\h*\\previouspage\h+(\H+)/) {
                $prev_skip = $1;
            } elsif ($skipping && /^\h*\\nextpage\h+(\H+)/) {
                $next_skip = $1;
            } elsif (/^\h*\\page\h+(\H+)/) {
                $curpage = $1;
            } elsif (/^\h*\\title\h+(.+)$/) {
                if ($curpage eq "") {
                    die "Title '$1' appears in no \\page.\n";
                }
                if (length($define_skip)) {
                     $define_skips{$1} = $define_skip;
                     $polarity_skips{$1} = $polarity_skip;
                     $prev_skips{$1} = $prev_skip;
                     $next_skips{$1} = $next_skip;
                     $define_skip = $prev_skip = $next_skip = "";
                }
                $title2page{$1} = $curpage;
                $doctitle = $1 if (!$doctitle);
                $curpage = "";
            }
        } else {
            if (/^\h*\\endlist/) {
                $intoc--;
            } elsif (!$skipping && /^\h*\\o\h+\\l\h*{(.*)}$/) {
                push @toc, $1;
76 77 78
            }
        }
    }
79
    close FILE;
80 81 82 83 84 85 86 87 88 89 90
}

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

91 92 93 94 95 96 97 98 99
for my $file (@files) {
    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";
    my $cutting = 0;
    while (<IN>) {
        if (!$cutting) {
            if (/^\h*\\contentspage/) {
                $cutting = 1;
            }
100
        } else {
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 137
            if (/^\h*\\title\h+(.+)$/) {
                if (defined($define_skips{$1})) {
                    print OUT "    \\if defined(".$define_skips{$1}.")\n";
                    if ($polarity_skips{$1}) {
                        print OUT "    \\previouspage ".$prev_skips{$1} if ($prev_skips{$1});
                        print OUT "    \\else\n";
                    }
                }
                print OUT "    \\previouspage ".$prev{$1} if ($prev{$1});
                if (defined($define_skips{$1})) {
                    if (!$polarity_skips{$1}) {
                        print OUT "    \\else\n";
                        print OUT "    \\previouspage ".$prev_skips{$1} if ($prev_skips{$1});
                    }
                    print OUT "    \\endif\n";
                }
                print OUT "    \\page ".$title2page{$1};
                if (defined($define_skips{$1})) {
                    print OUT "    \\if defined(".$define_skips{$1}.")\n";
                    if ($polarity_skips{$1}) {
                        print OUT "    \\nextpage ".$next_skips{$1} if ($next_skips{$1});
                        print OUT "    \\else\n";
                    }
                }
                print OUT "    \\nextpage ".$next{$1} if ($next{$1});
                if (defined($define_skips{$1})) {
                    if (!$polarity_skips{$1}) {
                        print OUT "    \\else\n";
                        print OUT "    \\nextpage ".$next_skips{$1} if ($next_skips{$1});
                    }
                    print OUT "    \\endif\n";
                }
                print OUT "\n";
                $cutting = 0;
            } else {
                next;
            }
138
        }
139
        print OUT $_;
140
    }
141 142 143
    close OUT;
    close IN;
    rename($file.".out", $file) or die "Cannot replace $file with new version.\n";
144
}