#!/usr/local/bin/perl # # David MacKay April 2000 # # includer.p - reads a file and follows \input{} commands # and writes a flat file without those commands # # includer.p shah | website.p # # note the include command must come at start of line # # Wed 18/4/01 added feature to strip "......." from included files # Substitutions like \pounds -> £ are done by website.p using s/// # $verbose = 0 ; $enddocends = 1 ; # whether \end{document} causes this program to halt its operation $skipcomments = 0 ; # whether to not print comment lines (%) eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $filelist = "" ; $filenumber=0 ; $level = 1 ; # WHLOOP: while( <>) { if (!( /^\%/ ) ) { if ( $enddocends && /\\end\{document\}/ ) { print ; last WHLOOP; } while ( s/^[#\\](input|include)\s*[\{"]([^}]+)[\}"]// ) { $f = $2 ; print STDERR "INCLUDER following input file $f \n" if ( $verbose ); &processfile ( $f ) ; } } if ( (!( /^\%/ ) ) || (!$skipcomments) ) { # print ; } } # print STDERR $filelist if ($verbose) ; # sub processfile { ($file,$rest) = @_ ; $filenumber ++ ; local($opencommand) = "open( O$filenumber , \"< $file\" ) " ; local($readcommand) = "\$_ = " ; print STDERR $opencommand , " " , $readcommand , "\n" ; if ( eval($opencommand) ) { $level ++ ; # print "\n" if ($verbose) ; $filelist .= "%%"." "x$level."$file\n" ; while ( eval($readcommand) ) { # if ( /\<[Hh][Ee][Aa][Dd]\>/ ) { # strip out all lines until we hit a end head print STDERR "stripping head\n" if ($verbose) ; while ( eval($readcommand) && (!/\<\/\s*[Hh][Ee][Aa][Dd]\s*\>/ ) ) { # # do nothing } # eval($readcommand) ; # flush out the s/^.*\<\/\s*[Hh][Ee][Aa][Dd]\s*\>// ; } if (!( /^\%/ ) ) { # if ( $enddocends && /\\end\{document\}/ ) { print ; last WHLOOP; } while ( s/[#\\](input|include)\s*[\{"]([^}]+)[\}"]// ) { # while ( s/\\(input|include)\{([^\}]+)\}// ) { $f = $2 ; print STDERR "following input file $f \n" if ( $verbose ); &processfile ( $f ) ; # } # } if ( (!( /^\%/ ) ) || (!$skipcomments) ) { # print ; } } $level -- ; } else { print STDERR "couldn't open $file\n" ; # } # if ( !/\n/ ) { # print "\n" ; # extra end line in case last line of included file did not have it } } #