#!/usr/bin/perl -w use strict; use Text::Wrap qw(wrap $columns); use Getopt::Std; our ($opt_t, $opt_w); $columns = 80; my %exceptions; #Words not in /usr/share/dict/words but in the *.bib files. @exceptions{qw(microcalcification microcalcifications)} = (); # Game plan: # bib2txt search_criteria # 1) use search_bib.pl to get entries from search_criteria, sort + parse # 2) make a .tex file using those entries, compile # 3) run dvi2tty on resulting .dvi # 4) parse + format the dvi2tty output sensibly (my $basename = $0) =~ s!.*/!!; my $bibdir = '~/documents/refs'; my $bibstyle = 'spiebib'; #my $bibstyle = 'IEEEtran'; my $texfile = 'bib2txt.tmp'; sub usage () { return <$commandfile") or die "$basename: Could not open $commandfile for write, $!"; print CMD "$0 @{[map{qq{'$_'} if /\s/;} @ARGV]}\n"; close (CMD) or die "$basename: Could not close $commandfile, $!"; my $search_criteria = shift; # if ($search_criteria eq '-t') { # $TeXify = 1; # $search_criteria = shift; # } $bibdir = shift || $bibdir; my %bibfiles; my @entries; open (SEARCH, "search_bib.pl '$search_criteria' $bibdir/*.bib |") or die "$basename: Could not run search_bib.pl: $!"; while () { next unless /(\S+)\.bib => (\S+)/; $bibfiles{$1} = (); push @entries, $2; } close (SEARCH) or die "$basename: Could not close search_bib.pl pipe: $!"; open (LATEX, ">$texfile.tex") or die "$basename: Could not open $texfile.tex: $!"; local $" = ','; print LATEX <) { next if /^\s*(References|\d+)?\s*$/; s/^\s+//; s/\s+$//; s/\s+/ /; s/ ([-,])/$1/g; #This tends to only happen for "italic correction" #(eg math, like "N -class"); it used to be after the #hyphenation code below, but I can't imagine any #situation where I would _want_ a space in front of a #hyphen... while (/(\w+)-$/) { my $frag = lc($1); my $nextline = ; $nextline = while ($nextline =~ /^\s*$/); $nextline =~ s/^\s+//; $nextline =~ s/\s+$//; $nextline =~ s/\s+/ /; $nextline =~ s/ ([-,])/$1/g; $nextline =~ /^(\w*)/; my $ment = $1; chop unless ($frag =~ /^\d+$/ || (`grep "^$frag\$" /usr/share/dict/words` && !`grep "^$frag$ment\$" /usr/share/dict/words` && !exists($exceptions{"$frag$ment"}))); chomp($_ .= $nextline); } push @entry, $_; $_ = ; redo; } continue { if (@entry) { $text .= wrap('', ' ', @entry) . "\n"; @entry = (); } } close (DVI) or die "$basename: could not close dvi2tty: $!"; if ($TeXify) { $text =~ s/\"(.*?)\"/\`\`$1\'\'/sg; $text =~ s{(\w+)-(\w+)}{my $left = $1; my $right = $2; $left . ($left =~ /\d/ && $right =~ /\d/ ? '--' : '-'). $right}eg; $text =~ s/_/---/g; } $text =~ s/_/--/g; if ($word_procify) { do { s/\t/ /g; s/ +/ /g; s/^(\[\d+\]) /$1\t/mg; s/\n(?!\[\d+\])//g; } for $text; } my $outfile = $texfile . ($word_procify ? '.wp' : '') . '.txt'; open (TXT, ">$outfile") or die "$basename: Could not open $outfile for write, $!"; print TXT $text; close (TXT) or die "$basename: Could not close $texfile.txt, $!";