2 # postbar - Massage chbar.sh output into valid LaTeX
3 # Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
4 # Use is subject to license terms.
6 # Gord Eagle <gord@clusterfs.com>, 2002-08-10
10 my $CHANGE_ENVIRONMENT = '\\\\(begin|end)\\{([^\\}]+)\\}';
11 my (@envname, @envdepth, @envbuf);
12 my $phony_preamble = 0;
16 # Tell whether an environment cannot have arbitrary changebars.
17 sub fragile_environment
20 return $env ne 'document';
24 # Tell whether we can hava arbitrary stuff.
28 return $env eq 'document';
35 if ($#envbuf < 0 || toplevel($envname[0])) {
38 $envbuf[0] .= join('', @msg);
43 # Leave an environment.
48 #out("%$progname end $env\n");
49 if ($envname[0] ne $env) {
50 die "Expecting \\end{$envname[0]} but got \\end{$env}\n";
54 # Did we find a changebar?
55 $cbfound = !toplevel($envname[1]);
57 # We found one, and the parent environment is the top level.
58 if ($cbdepth == $envdepth[0]) {
59 # There was no change in depth, so mark the environment.
60 $envbuf[0] = "\\cbstart{}%$progname\n" . $envbuf[0];
61 out("\\cbend{}%$progname\n");
62 } elsif ($envdepth[0] > $cbdepth) {
63 # There were more ends in the environment, so append them.
64 for (my $i = 0; $i < $envdepth[0] - $cbdepth; $i ++) {
65 out("\\cbend{}%$progname\n");
68 # There were more starts, so prepend them.
70 for (my $i = 0; $i < $cbdepth - $envdepth[0]; $i ++) {
71 $starts .= "\\cbstart{}%$progname\n";
73 $envbuf[0] = $starts . $envbuf[0];
78 # Drop the environment from the list.
85 while ($_ = <STDIN>) {
88 if (!/\\begin.*\\end/ && /$CHANGE_ENVIRONMENT/o) {
91 # Enter the new environment.
92 unshift(@envname, $env);
93 unshift(@envdepth, $cbdepth);
95 #out("%$progname depth=$cbdepth, $#envname ($env)\n");
96 } elsif (!$phony_preamble) {
98 end_environment($env);
103 if ($#envname >= 0 && /^\\documentclass/) {
107 if ($phony_preamble) {
108 # Comment out and ignore the redundant preambles.
109 out("%$progname $_\n");
110 $phony_preamble = 0 if ($env eq 'document');
112 } elsif ($#envname >= 0) {
113 # Track the current changebar depth.
116 if (!toplevel($envname[0])) {
118 out("%$progname $_\n");
121 } elsif (/^\\cbend/) {
123 die "$progname: Too many \\cbend{}s\n";
126 if (!toplevel($envname[0])) {
128 out("%$progname $_\n");
131 } elsif (/^\\cbdelete/ && fragile_environment($envname[0])) {
132 # What to do with delete bars?
133 out("%$progname $_\n");
139 # Add the options to the usepackage.
140 if (/^\\usepackage.*\{changebar\}$/) {
141 # Prevent PostScript dictionary overflow errors.
142 out("\\def\\cb\@maxpoint{15}\n");
145 out("\\outerbarstrue\n");