#! /usr/bin/perl # postbar - Massage chbar.sh output into valid LaTeX # Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved. # Use is subject to license terms. # # Gord Eagle , 2002-08-10 my $progname = $0; $progname =~ s|^.*/||; my $CHANGE_ENVIRONMENT = '\\\\(begin|end)\\{([^\\}]+)\\}'; my (@envname, @envdepth, @envbuf); my $phony_preamble = 0; my $cbdepth = 0; my $cbfound = 0; # Tell whether an environment cannot have arbitrary changebars. sub fragile_environment { my ($env) = @_; return $env ne 'document'; } # Tell whether we can hava arbitrary stuff. sub toplevel { my ($env) = @_; return $env eq 'document'; } sub out { my (@msg) = @_; if ($#envbuf < 0 || toplevel($envname[0])) { print @msg; } else { $envbuf[0] .= join('', @msg); } } # Leave an environment. sub end_environment { my ($env) = @_; #out("%$progname end $env\n"); if ($envname[0] ne $env) { die "Expecting \\end{$envname[0]} but got \\end{$env}\n"; } if ($cbfound) { # Did we find a changebar? $cbfound = !toplevel($envname[1]); if (!$cbfound) { # We found one, and the parent environment is the top level. if ($cbdepth == $envdepth[0]) { # There was no change in depth, so mark the environment. $envbuf[0] = "\\cbstart{}%$progname\n" . $envbuf[0]; out("\\cbend{}%$progname\n"); } elsif ($envdepth[0] > $cbdepth) { # There were more ends in the environment, so append them. for (my $i = 0; $i < $envdepth[0] - $cbdepth; $i ++) { out("\\cbend{}%$progname\n"); } } else { # There were more starts, so prepend them. my $starts; for (my $i = 0; $i < $cbdepth - $envdepth[0]; $i ++) { $starts .= "\\cbstart{}%$progname\n"; } $envbuf[0] = $starts . $envbuf[0]; } } } # Drop the environment from the list. shift(@envname); shift(@envdepth); out(shift(@envbuf)); } while ($_ = ) { chomp; my $env; if (!/\\begin.*\\end/ && /$CHANGE_ENVIRONMENT/o) { $env = $2; if ($1 eq 'begin') { # Enter the new environment. unshift(@envname, $env); unshift(@envdepth, $cbdepth); unshift(@envbuf, ''); #out("%$progname depth=$cbdepth, $#envname ($env)\n"); } elsif (!$phony_preamble) { out("$_\n"); end_environment($env); next; } } if ($#envname >= 0 && /^\\documentclass/) { $phony_preamble = 1; } if ($phony_preamble) { # Comment out and ignore the redundant preambles. out("%$progname $_\n"); $phony_preamble = 0 if ($env eq 'document'); next; } elsif ($#envname >= 0) { # Track the current changebar depth. if (/^\\cbstart/) { $cbdepth ++; if (!toplevel($envname[0])) { $cbfound = 1; out("%$progname $_\n"); next; } } elsif (/^\\cbend/) { if ($cbdepth == 0) { die "$progname: Too many \\cbend{}s\n"; } $cbdepth --; if (!toplevel($envname[0])) { $cbfound = 1; out("%$progname $_\n"); next; } } elsif (/^\\cbdelete/ && fragile_environment($envname[0])) { # What to do with delete bars? out("%$progname $_\n"); next; } out("$_\n"); } else { out("$_\n"); # Add the options to the usepackage. if (/^\\usepackage.*\{changebar\}$/) { # Prevent PostScript dictionary overflow errors. out("\\def\\cb\@maxpoint{15}\n"); # Show the bars. out("\\outerbarstrue\n"); } } if (defined($env)) { } } exit(0);