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