#!/usr/bin/perl -w use Term::ANSIColor qw(:constants); sub help { print "Use: $0 [-d] filter < input.tex > output.tex\n\n"; print " -d: Include color changes for debugging. Use less -r to page it.\n"; print " green: unfiltered raw text\n"; print " red: filtered text\n"; print " yellow: LaTeX command\n"; print " cyan: {text}\n"; print "man b1ff for suggested filters.\n"; exit 2; } my $RT = ''; my $FT = ''; my $LC = ''; my $BT = ''; my $CC = ''; help() if($#ARGV < 0); if($ARGV[0] eq '-d'){ $RT = GREEN; $FT = RED; $LC = YELLOW; $BT = CYAN; $CC = RESET; shift @ARGV; } help() if($#ARGV < 0); my $filt = $ARGV[0]; #$/ = ''; my $inbrace = 0; my $inp = do { local $/; }; parselatex($inp); sub parselatex { my $inp = $_[0]; my @ftlcbt = $inp =~ /([^\\]*) # filterable text. ((?:\\\w*(?:\[[^]]*\])?)*) # LaTeX command(s) ((?:{(?:{[^}]*}|[^}])*})*)/gx; # Stuff in braces. my $lc = ''; my $bt = ''; while(@ftlcbt){ my $ft = shift @ftlcbt; if($ft =~ /\w/ && ($lc ne '\begin' || $bt eq '{abstract}')){ $ft = applyfilt($ft, $filt); print $FT; } else{ print $RT; } print $ft, $CC; $lc = shift @ftlcbt; print $LC, $lc, $CC; $bt = shift @ftlcbt; if($lc =~ /section|keywords|caption|acknowledgements|footnote|title|affil/){ print '{'; $bt =~ s/^\{//; $bt =~ s/\}$//; parselatex($bt); print '}'; } else{ print $BT, $bt, $CC; } } } sub applyfilt { my ($inp, $filt) = @_; local $/; open(FILT, "|$filt > tmp.filtertex") or die "Could not |$filt > tmp.filtertex"; print FILT $inp; close(FILT) or warn "Could not close |$filt > tmp.filtertex"; open(FILT, "tmp.filtertex") or die "Could not read tmp.filtertex"; my $outp = do { local $/; }; close(FILT) or warn "Could not close tmp.filtertex"; # unlink("tmp.filtertex"); return $outp; }