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
8 my $CHANGE_ENVIRONMENT = '\\\\(begin|end)\\{([^\\}]+)\\}';
9 my (@envname, @envdepth, @envbuf);
10 my $phony_preamble = 0;
14 # Tell whether an environment cannot have arbitrary changebars.
15 sub fragile_environment
18 return $env ne 'document';
22 # Tell whether we can hava arbitrary stuff.
26 return $env eq 'document';
33 if ($#envbuf < 0 || toplevel($envname[0])) {
36 $envbuf[0] .= join('', @msg);
41 # Leave an environment.
46 #out("%$progname end $env\n");
47 if ($envname[0] ne $env) {
48 die "Expecting \\end{$envname[0]} but got \\end{$env}\n";
52 # Did we find a changebar?
53 $cbfound = !toplevel($envname[1]);
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");
66 # There were more starts, so prepend them.
68 for (my $i = 0; $i < $cbdepth - $envdepth[0]; $i ++) {
69 $starts .= "\\cbstart{}%$progname\n";
71 $envbuf[0] = $starts . $envbuf[0];
76 # Drop the environment from the list.
83 while ($_ = <STDIN>) {
86 if (!/\\begin.*\\end/ && /$CHANGE_ENVIRONMENT/o) {
89 # Enter the new environment.
90 unshift(@envname, $env);
91 unshift(@envdepth, $cbdepth);
93 #out("%$progname depth=$cbdepth, $#envname ($env)\n");
94 } elsif (!$phony_preamble) {
96 end_environment($env);
101 if ($#envname >= 0 && /^\\documentclass/) {
105 if ($phony_preamble) {
106 # Comment out and ignore the redundant preambles.
107 out("%$progname $_\n");
108 $phony_preamble = 0 if ($env eq 'document');
110 } elsif ($#envname >= 0) {
111 # Track the current changebar depth.
114 if (!toplevel($envname[0])) {
116 out("%$progname $_\n");
119 } elsif (/^\\cbend/) {
121 die "$progname: Too many \\cbend{}s\n";
124 if (!toplevel($envname[0])) {
126 out("%$progname $_\n");
129 } elsif (/^\\cbdelete/ && fragile_environment($envname[0])) {
130 # What to do with delete bars?
131 out("%$progname $_\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");
143 out("\\outerbarstrue\n");