Whamcloud - gitweb
Updated changebar generation to preserve nesting.
[fs/lustre-release.git] / lustre / doc / postbar
1 #! /usr/bin/perl
2 # postbar - Massage chbar.sh output into valid LaTeX
3 # Copyright (C) 2002  Cluster File Systems, Inc.
4 # Gord Eagle <gord@clusterfs.com>, 2002-08-10
5
6 my $progname = $0;
7 $progname =~ s|^.*/||;
8 my $CHANGE_ENVIRONMENT = '\\\\(begin|end)\\{([^\\}]+)\\}';
9 my (@envname, @envdepth, @envbuf);
10 my $phony_preamble = 0;
11 my $cbdepth = 0;
12 my $cbfound = 0;
13
14 # Tell whether an environment cannot have arbitrary changebars.
15 sub fragile_environment
16 {
17     my ($env) = @_;
18     return $env ne 'document';
19 }
20
21
22 # Tell whether we can hava arbitrary stuff.
23 sub toplevel
24 {
25     my ($env) = @_;
26     return $env eq 'document';
27 }
28
29
30 sub out
31 {
32     my (@msg) = @_;
33     if ($#envbuf < 0 || toplevel($envname[0])) {
34         print @msg;
35     } else {
36         $envbuf[0] .= join('', @msg);
37     }
38 }
39
40
41 # Leave an environment.
42 sub end_environment
43 {
44     my ($env) = @_;
45
46     #out("%$progname end $env\n");
47     if ($envname[0] ne $env) {
48         die "Expecting \\end{$envname[0]} but got \\end{$env}\n";
49     }
50
51     if ($cbfound) {
52         # Did we find a changebar?
53         $cbfound = !toplevel($envname[1]);
54         if (!$cbfound) {
55             # We found one, and the parent environment is the top level.
56             if ($cbdepth == $envdepth[0]) {
57                 # There was no change in depth, so mark the environment.
58                 $envbuf[0] = "\\cbstart{}%$progname\n" . $envbuf[0];
59                 out("\\cbend{}%$progname\n");
60             } elsif ($envdepth[0] > $cbdepth) {
61                 # There were more ends in the environment, so append them.
62                 for (my $i = 0; $i < $envdepth[0] - $cbdepth; $i ++) {
63                     out("\\cbend{}%$progname\n");
64                 }
65             } else {
66                 # There were more starts, so prepend them.
67                 my $starts;
68                 for (my $i = 0; $i < $cbdepth - $envdepth[0]; $i ++) {
69                     $starts .= "\\cbstart{}%$progname\n";
70                 }
71                 $envbuf[0] = $starts . $envbuf[0];
72             }
73         }
74     }
75
76     # Drop the environment from the list.
77     shift(@envname);
78     shift(@envdepth);
79     out(shift(@envbuf));
80 }
81
82
83 while ($_ = <STDIN>) {
84     chomp;
85     my $env;
86     if (!/\\begin.*\\end/ && /$CHANGE_ENVIRONMENT/o) {
87         $env = $2;
88         if ($1 eq 'begin') {
89             # Enter the new environment.
90             unshift(@envname, $env);
91             unshift(@envdepth, $cbdepth);
92             unshift(@envbuf, '');
93             #out("%$progname depth=$cbdepth, $#envname ($env)\n");
94         } elsif (!$phony_preamble) {
95             out("$_\n");
96             end_environment($env);
97             next;
98         }
99     }   
100
101     if ($#envname >= 0 && /^\\documentclass/) {
102         $phony_preamble = 1;
103     }
104
105     if ($phony_preamble) {
106         # Comment out and ignore the redundant preambles.
107         out("%$progname $_\n");
108         $phony_preamble = 0 if ($env eq 'document');
109         next;
110     } elsif ($#envname >= 0) {
111         # Track the current changebar depth.
112         if (/^\\cbstart/) {
113             $cbdepth ++;
114             if (!toplevel($envname[0])) {
115                 $cbfound = 1;
116                 out("%$progname $_\n");
117                 next;
118             }
119         } elsif (/^\\cbend/) {
120             if ($cbdepth == 0) {
121                 die "$progname: Too many \\cbend{}s\n";
122             }
123             $cbdepth --;
124             if (!toplevel($envname[0])) {
125                 $cbfound = 1;
126                 out("%$progname $_\n");
127                 next;
128             }
129         } elsif (/^\\cbdelete/ && fragile_environment($envname[0])) {
130             # What to do with delete bars?
131             out("%$progname $_\n");
132             next;
133         }
134         out("$_\n");
135     } else {
136         out("$_\n");
137         # Add the options to the usepackage.
138         if (/^\\usepackage.*\{changebar\}$/) {
139             # Prevent PostScript dictionary overflow errors.
140             out("\\def\\cb\@maxpoint{15}\n");
141
142             # Show the bars.
143             out("\\outerbarstrue\n");
144         }
145     }
146
147     if (defined($env)) {
148     }
149 }
150
151 exit(0);