Chris@10: #!/usr/bin/perl -- Chris@10: # Copyright (C) 1993-1995 Ian Jackson. Chris@10: Chris@10: # This file is free software; you can redistribute it and/or modify Chris@10: # it under the terms of the GNU General Public License as published by Chris@10: # the Free Software Foundation; either version 2, or (at your option) Chris@10: # any later version. Chris@10: Chris@10: # It is distributed in the hope that it will be useful, Chris@10: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@10: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@10: # GNU General Public License for more details. Chris@10: Chris@10: # You should have received a copy of the GNU General Public License Chris@10: # along with GNU Emacs; see the file COPYING. If not, write to Chris@10: # the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Chris@10: # Boston, MA 02111-1307, USA. Chris@10: Chris@10: # (Note: I do not consider works produced using these BFNN processing Chris@10: # tools to be derivative works of the tools, so they are NOT covered Chris@10: # by the GPL. However, I would appreciate it if you credited me if Chris@10: # appropriate in any documents you format using BFNN.) Chris@10: Chris@10: @outputs=('ascii','info','html'); Chris@10: Chris@10: while ($ARGV[0] =~ m/^\-/) { Chris@10: $_= shift(@ARGV); Chris@10: if (m/^-only/) { Chris@10: @outputs= (shift(@ARGV)); Chris@10: } else { Chris@10: warn "unknown option `$_' ignored"; Chris@10: } Chris@10: } Chris@10: Chris@10: $prefix= $ARGV[0]; Chris@10: $prefix= 'stdin' unless length($prefix); Chris@10: $prefix =~ s/\.bfnn$//; Chris@10: Chris@10: if (open(O,"$prefix.xrefdb")) { Chris@10: @xrefdb= ; Chris@10: close(O); Chris@10: } else { Chris@10: warn "no $prefix.xrefdb ($!)"; Chris@10: } Chris@10: Chris@10: $section= -1; Chris@10: for $thisxr (@xrefdb) { Chris@10: $_= $thisxr; Chris@10: chop; Chris@10: if (m/^Q (\w+) ((\d+)\.(\d+)) (.*)$/) { Chris@10: $qrefn{$1}= $2; Chris@10: $qreft{$1}= $5; Chris@10: $qn2ref{$3,$4}= $1; Chris@10: $maxsection= $3; Chris@10: $maxquestion[$3]= $4; Chris@10: } elsif (m/^S (\d+) /) { Chris@10: $maxsection= $1; Chris@10: $sn2title{$1}=$'; Chris@10: } Chris@10: } Chris@10: Chris@10: open(U,">$prefix.xrefdb-new"); Chris@10: Chris@10: for $x (@outputs) { require("m-$x.pl"); } Chris@10: Chris@10: &call('init'); Chris@10: Chris@10: while (<>) { Chris@10: chop; Chris@10: next if m/^\\comment\b/; Chris@10: if (!m/\S/) { Chris@10: &call('endpara'); Chris@10: next; Chris@10: } Chris@10: if (s/^\\section +//) { Chris@10: $line= $_; Chris@10: $section++; $question=0; Chris@10: print U "S $section $line\n"; Chris@10: $|=1; print "S$section",' 'x10,"\r"; $|=0; Chris@10: &call('endpara'); Chris@10: &call('startmajorheading',"$section", Chris@10: "Section $section", Chris@10: $section<$maxsection ? "Section ".($section+1) : '', Chris@10: $section>1 ? 'Section '.($section-1) : 'Top'); Chris@10: &text($line); Chris@10: &call('endmajorheading'); Chris@10: if ($section) { Chris@10: &call('endpara'); Chris@10: &call('startindex'); Chris@10: for $thisxr (@xrefdb) { Chris@10: $_= $thisxr; Chris@10: chop; Chris@10: if (m/^Q (\w+) (\d+)\.(\d+) (.*)$/) { Chris@10: $ref= $1; $num1= $2; $num2= $3; $text= $4; Chris@10: next unless $num1 == $section; Chris@10: &call('startindexitem',$ref,"Q$num1.$num2","Question $num1.$num2"); Chris@10: &text($text); Chris@10: &call('endindexitem'); Chris@10: } Chris@10: } Chris@10: &call('endindex'); Chris@10: } Chris@10: } elsif (s/^\\question \d{2}[a-z]{3}((:\w+)?) +//) { Chris@10: $line= $_; Chris@10: $question++; Chris@10: $qrefstring= $1; Chris@10: $qrefstring= "q_${section}_$question" unless $qrefstring =~ s/^://; Chris@10: print U "Q $qrefstring $section.$question $line\n"; Chris@10: $|=1; print "Q$section.$question",' 'x10,"\r"; $|=0; Chris@10: &call('endpara'); Chris@10: &call('startminorheading',$qrefstring, Chris@10: "Question $section.$question", Chris@10: $question < $maxquestion[$section] ? "Question $section.".($question+1) : Chris@10: $section < $maxsection ? "Question ".($section+1).".1" : '', Chris@10: $question > 1 ? "Question $section.".($question-1) : Chris@10: $section > 1 ? "Question ".($section-1).'.'.($maxquestion[$section-1]) : Chris@10: 'Top', Chris@10: "Section $section"); Chris@10: &text("Question $section.$question. $line"); Chris@10: &call('endminorheading'); Chris@10: } elsif (s/^\\only +//) { Chris@10: @saveoutputs= @outputs; Chris@10: @outputs=(); Chris@10: for $x (split(/\s+/,$_)) { Chris@10: push(@outputs,$x) if grep($x eq $_, @saveoutputs); Chris@10: } Chris@10: } elsif (s/^\\endonly$//) { Chris@10: @outputs= @saveoutputs; Chris@10: } elsif (s/^\\copyto +//) { Chris@10: $fh= $'; Chris@10: while(<>) { Chris@10: last if m/^\\endcopy$/; Chris@10: while (s/^([^\`]*)\`//) { Chris@10: print $fh $1; Chris@10: m/([^\\])\`/ || warn "`$_'"; Chris@10: $_= $'; Chris@10: $cmd= $`.$1; Chris@10: $it= `$cmd`; chop $it; Chris@10: print $fh $it; Chris@10: } Chris@10: print $fh $_; Chris@10: } Chris@10: } elsif (m/\\index$/) { Chris@10: &call('startindex'); Chris@10: for $thisxr (@xrefdb) { Chris@10: $_= $thisxr; Chris@10: chop; Chris@10: if (m/^Q (\w+) (\d+\.\d+) (.*)$/) { Chris@10: $ref= $1; $num= $2; $text= $3; Chris@10: &call('startindexitem',$ref,"Q$num","Question $num"); Chris@10: &text($text); Chris@10: &call('endindexitem'); Chris@10: } elsif (m/^S (\d+) (.*)$/) { Chris@10: $num= $1; $text= $2; Chris@10: next unless $num; Chris@10: &call('startindexmainitem',"s_$num", Chris@10: "Section $num.","Section $num"); Chris@10: &text($text); Chris@10: &call('endindexitem'); Chris@10: } else { Chris@10: warn $_; Chris@10: } Chris@10: } Chris@10: &call('endindex'); Chris@10: } elsif (m/^\\call-(\w+) +(\w+)\s*(.*)$/) { Chris@10: $fn= $1.'_'.$2; Chris@10: eval { &$fn($3); }; Chris@10: warn $@ if length($@); Chris@10: } elsif (m/^\\call +(\w+)\s*(.*)$/) { Chris@10: eval { &call($1,$2); }; Chris@10: warn $@ if length($@); Chris@10: } elsif (s/^\\set +(\w+)\s*//) { Chris@10: $svalue= $'; $svari= $1; Chris@10: eval("\$user_$svari=\$svalue"); $@ && warn "setting $svalue failed: $@\n"; Chris@10: } elsif (m/^\\verbatim$/) { Chris@10: &call('startverbatim'); Chris@10: while (<>) { Chris@10: chop; Chris@10: last if m/^\\endverbatim$/; Chris@10: &call('verbatim',$_); Chris@10: } Chris@10: &call('endverbatim'); Chris@10: } else { Chris@10: s/\.$/\. /; Chris@10: &text($_." "); Chris@10: } Chris@10: } Chris@10: Chris@10: print ' 'x25,"\r"; Chris@10: &call('finish'); Chris@10: rename("$prefix.xrefdb-new","$prefix.xrefdb") || warn "rename xrefdb: $!"; Chris@10: exit 0; Chris@10: Chris@10: Chris@10: sub text { Chris@10: local($in,$rhs,$word,$refn,$reft,$fn,$style); Chris@10: $in= "$holdover$_[0]"; Chris@10: $holdover= ''; Chris@10: while ($in =~ m/\\/) { Chris@10: #print STDERR ">$`##$'\n"; Chris@10: $rhs=$'; Chris@10: &call('text',$`); Chris@10: $_= $rhs; Chris@10: if (m/^\w+ $/) { Chris@10: $holdover= "\\$&"; Chris@10: $in= ''; Chris@10: } elsif (s/^fn\s+([^\s\\]*\w)//) { Chris@10: $in= $_; Chris@10: $word= $1; Chris@10: &call('courier'); Chris@10: &call('text',$word); Chris@10: &call('endcourier'); Chris@10: } elsif (s/^tab\s+(\d+)\s+//) { Chris@10: $in= $_; &call('tab',$1); Chris@10: } elsif (s/^nl\s+//) { Chris@10: $in= $_; &call('newline'); Chris@10: } elsif (s/^qref\s+(\w+)//) { Chris@10: $refn= $qrefn{$1}; Chris@10: $reft= $qreft{$1}; Chris@10: if (!length($refn)) { Chris@10: warn "unknown question `$1'"; Chris@10: } Chris@10: $in= "$`\\pageref:$1:$refn:$reft\\endpageref.$_"; Chris@10: } elsif (s/^pageref:(\w+):([^:\n]+)://) { Chris@10: $in= $_; Chris@10: &call('pageref',$1,$2); Chris@10: } elsif (s/^endpageref\.//) { Chris@10: $in= $_; &call('endpageref'); Chris@10: } elsif (s/^(\w+)\{//) { Chris@10: $in= $_; $fn= $1; Chris@10: eval { &call("$fn"); }; Chris@10: if (length($@)) { warn $@; $fn= 'x'; } Chris@10: push(@styles,$fn); Chris@10: } elsif (s/^\}//) { Chris@10: $in= $_; Chris@10: $fn= pop(@styles); Chris@10: if ($fn ne 'x') { &call("end$fn"); } Chris@10: } elsif (s/^\\//) { Chris@10: $in= $_; Chris@10: &call('text',"\\"); Chris@10: } elsif (s,^(\w+)\s+([-A-Za-z0-9.\@:/]*\w),,) { Chris@10: #print STDERR "**$&**$_\n"; Chris@10: $in= $_; Chris@10: $style=$1; $word= $2; Chris@10: &call($style); Chris@10: &call('text',$word); Chris@10: &call("end$style"); Chris@10: } else { Chris@10: warn "unknown control `\\$_'"; Chris@10: $in= $_; Chris@10: } Chris@10: } Chris@10: &call('text',$in); Chris@10: } Chris@10: Chris@10: Chris@10: sub call { Chris@10: local ($fnbase, @callargs) = @_; Chris@10: local ($coutput); Chris@10: for $coutput (@outputs) { Chris@10: if ($fnbase eq 'text' && eval("\@${coutput}_cmds")) { Chris@10: #print STDERR "special handling text (@callargs) for $coutput\n"; Chris@10: $evstrg= "\$${coutput}_args[\$#${coutput}_args].=\"\@callargs\""; Chris@10: eval($evstrg); Chris@10: length($@) && warn "call adding for $coutput (($evstrg)): $@"; Chris@10: } else { Chris@10: $fntc= $coutput.'_'.$fnbase; Chris@10: &$fntc(@callargs); Chris@10: } Chris@10: } Chris@10: } Chris@10: Chris@10: Chris@10: sub recurse { Chris@10: local (@outputs) = $coutput; Chris@10: local ($holdover); Chris@10: &text($_[0]); Chris@10: } Chris@10: Chris@10: Chris@10: sub arg { Chris@10: #print STDERR "arg($_[0]) from $coutput\n"; Chris@10: $cmd= $_[0]; Chris@10: eval("push(\@${coutput}_cmds,\$cmd); push(\@${coutput}_args,'')"); Chris@10: length($@) && warn "arg setting up for $coutput: $@"; Chris@10: } Chris@10: Chris@10: sub endarg { Chris@10: #print STDERR "endarg($_[0]) from $coutput\n"; Chris@10: $evstrg= "\$${coutput}_cmd= \$cmd= pop(\@${coutput}_cmds); ". Chris@10: "\$${coutput}_arg= \$arg= pop(\@${coutput}_args); "; Chris@10: eval($evstrg); Chris@10: length($@) && warn "endarg extracting for $coutput (($evstrg)): $@"; Chris@10: #print STDERR ">call $coutput $cmd $arg< (($evstrg))\n"; Chris@10: $evstrg= "&${coutput}_do_${cmd}(\$arg)"; Chris@10: eval($evstrg); Chris@10: length($@) && warn "endarg running ${coutput}_do_${cmd} (($evstrg)): $@"; Chris@10: }