#!/usr/bin/perl
#
#  readabstract.pl - filter an abstract from a submitted article
#

@skip = ("documentstyle\\[[^]]+\\]{[^}]+}", 
#	 "begin\\s*{document}", "end\\s*{document}", 
	 "email{[^}]+}", "contact{[^}]+}", "paspbook", 
	 "paspheader");


sub readAbstract {

    local($texfile) = @_;
    $texfile .= ".tex" if ($texfile !~ /\.[^\.]*$/);

    open(INTEX, "$texfile") || &fatal("$texfile: $!");

    $out = '';
    $inAbs = 0;
    
    &inform("Filtering $texfile...") if ($verbose);

    $endfound = $false;
    LINE: while ($line = <INTEX>) {

	# skip comment lines
	next LINE if ($line =~ /^\s*%/);  
	($line, $tmp) = remove_TeXcomment($line);

	# check to see if line contains a skip macro
	foreach $macro (@skip) {
	    if ($line =~ /\\$macro/) {
		($pre, $match, $post, $stat) = &grep_TeXmacro($macro, $line);
		$line = "$pre $post" if ($stat);
	    }
	}

	# do any substitutions
	if ($line =~ /\\begin\s*{\s*abstract\s*}/){
	    $line =~ s/\\s*begin\s*{\s*abstract\s*}//;
            $inAbs = 1;
        }

	# collapse consecutive blank lines
	if ($line =~ /^\s*$/) {
	    next LINE if ($blank);
	    $blank = $true;
	}
	elsif ($blank) {
	    $blank = $false;
	}

	if ($line =~ /\\end\s*{\s*abstract\s*}/) {
	    $line =~ s/\\s*end\s*{\s*abstract\s*}//;
            chomp $line;
	    $endfound = $true;
	}

	#$line .= "\n" if ($line !~ /\n$/);
        $out .= $line if $inAbs;
	last if ($endfound);
    }

    return $out;
    close(INTEX);
    
}

1;
