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