#!/usr/local/bin/perl # # art2chap # # Filter a single contribution from its original, standalone # article form into a form suitable for inclusion as a chapter # in a book. The name of the tex document is given on the # command line. An optional argument [-html] is used when # converting for the purpose of the on-line version, rather # than the print version. It makes the output filename # extension be "l2h" rather than the default "ltx". # use Cwd; $cwd = cwd() ; use lib $cwd ; use BOOKSTUFF ; # No arguments is an error. # $usg = "Usage: art2chap [-html] (-paperdir dirname|artfile[.tex])\n" ; ($#ARGV >= 0 ) || die($usg) ; # Any command line switches? # $forhtml = 0 ; while ($ARGV[0] =~ /^-/) { $_ = shift; if (/^-html$/) { $ext_out = $ext_out_html ; $forhtml = 1 ; } elsif( /^-paperdir$/ ) { $paperdir = shift ; } else { $usg ; die "Unrecognized switch: $_\n" ; } } if( defined( $paperdir ) ) { foreach $file (glob( "$paperdir$pathsep*.$ext_in" ) ) { # Filter the file. # &filter( $file ) ; } } else { foreach $file (@ARGV) { # Otherwise, process the remainder of the command line as file names. # # Was the file name extension supplied? Add it, if not. # (From latex2html:) # ( $ext = $file ) =~ s/(.*)\.([^\.$pathsep]*)$/$1/ ; if( $ext eq $file ) { $file = $file . "." . $ext_in ; } # Does the file exist? Skip, if not. # unless( -f $file ) { print "Cannot open input file $file.\n" ; next ; } # OK, filter the file. # &filter( $file ) ; } } sub filter{ my( $file ) = @_ ; my( $fileroot, $line, $outfile ) ; # Filename root, minus any path elements. # ( $fileroot = $file ) =~ s/\.$ext_in$// ; $fileroot =~ s/^.*$pathsep([^$pathsep]*)/$1/ ; # Append output filename extension to fileroot obtain output # file name. Open files. # $outfile = $fileroot . ".$ext_out" ; open( IN, "$file" ) || die "Could not open input file $file\n" ; open( OUT, ">$outfile" ) || die "Could not open output file $outfile\n" ; print OUT "%%% DO NOT EDIT THIS FILE! %%%\n", "%%% it was produced automatically from file $file.\n", "%%% Edit the source file, or change the filter subroutine\n", "%%% in the art2chap script.\n", "%%%\n", "{" ; while( defined( $line= ) ) { if( $line =~ /\\documentstyle/ && !$forhtml ) { # Comment out the \documentstyle command, and put entire # paper between braces, to limit damage caused by author # defined macros. # $line =~ s/$1/% \\documentstyle/ ; } elsif( $line =~ /\\begin\s*{\s*document\s*}/ && !$forhtml ) { # Eliminate "\begin{document}" # $line =~ s/$1// ; } elsif( $line =~ /\\nofiles/ ) { # Eliminate "\nofiles" # $line =~ s/$1// ; } elsif( $line =~ /\\title/ ) { # Replace "\title" with "\chapter" # $line =~ s/$1/\\chapter/ ; } elsif( $line =~ /\\begin{\s*abstract\s*}/ ) { # Prepend a label containing the fileroot, for # cross-links between papers. # $line =~ s/($1)/\\bookstufflabel{$fileroot}$1/ ; } elsif( $line =~ /\\end{document}/ && !$forhtml ) { # Close group opened at \documentstyle # $line =~ s/$1/}%/ ; } elsif( $forhtml ) { # Most of this is for processing the book.tex file. if( $line =~ /\\section\*/ ) { # \section* becomes \chapter # $line =~ s/$1/\\chapter/ ; } elsif( $line =~ /%\\chapter/ ) { # Restore commented out chapters (??? why?) # $line =~ s/$1/\\chapter/ ; } elsif( $line =~ /\\documentstyle/ ) { # Replace conference style file with a version # intended for the on-line proceedings. } elsif( $line =~ /\\tableofcontents/ ) { $line =~ s/$1// ; } elsif( $line =~ /\\makeindex/ ) { $line =~ s/$1// ; } elsif( $line =~ /\\makeaindex/ ) { $line =~ s/$1// ; } elsif( $line =~ /\\markboth/ ) { } elsif( $line =~ /\\addcontentsline/ ) { } } print OUT $line ; } }