#--geezer.pl
#-- "englishizer" -
#-- Produces printable ASCII of the House provided
#-- composition code format, using the format documentation set collected
#-- under QuickGuide.doc.  Several other forms of the data are produced.
#-- 
#-- The COMMAND LINE has not been formalized - 
#-- the way it works currently assumes a source file naming convention, eg,
#-- perl -w geezer.pl 1 1
#-- to process ../data/uscode01.bell
#-- or
#-- perl -w geezer.pl 3 17
#-- to process ../data/uscode03.bell through ../data/uscode17.bell
#-- 
#-- In addition to the one-to-one filter output mentioned above,
#-- the OUTPUT is a directory for each input file (USC title) containing
#-- one HTML file for each USC fragment found, with fragmentation level of
#-- (Super-)Section, plus a number of pop-up index HTMLs
#-- 
#-- Each HTML page produced here points to the XML page for the corresponding
#-- fragment in ../emely/uscodenn
#-- 
#-- David Shetland Dec-2001 ff
#******************************************************************************************

use strict;
# use IO::File;
use LDMS_NG::USCRinger;
use LDMS_NG::USCXrefs;
use Roman;  #-- from CPAN

#-- mySQL --
# use DBI;
# $|= 1;				# Autoflush
# my $host         = "";
# my $opt_user     = "";
# my $opt_password = "";
# my $usc_db="usc_process";
# print "Connection to database $usc_db\n";
# my $dbh = DBI->connect("DBI:mysql:$usc_db:$host",$opt_user,$opt_password) || die "Can't connect: $DBI::errstr\n";
# my $table="title_summary";
#-----------


#-- titles to process
my $titleFirst =   "";
my $titleLast  =   "";
   $titleFirst =   shift(@ARGV);
   $titleLast  =   shift(@ARGV);
if (!$titleFirst) {  print( "oops - need an initial title number \(1-50\)\n" ); exit; }
if (!$titleLast ) {  
# print( "using initial title number for terminal as well\n" ); 
$titleLast=$titleFirst; }

my $titlenum = "00";

my $chunk = "";

my $tmpkey = "00T00";

#-- file fragment counters
my $ctrFragStr = "";
my $ctrFrag    = 0;
my $flgFragStr = sprintf("%05d",$ctrFrag); #-- marker for last flagged fragment

my $rCode = 0;

my @headlist = (); #-- expcite style list of super-section heads maintained as a stack 
my @hdtykeys = (); #-- ssdiv keys that match headlist entries
# my $prevLvl  = 0;
my $pushHead = 0;
my $replHead = 0;
my $popHead  = 0;

my $ctrTOFEntry = 0;
my $ctrTOFEntStr = "";
my $ctrTOTEntry = 0;
my $ctrTOTEntStr = "";
my $ctrAREFEntry = 0;
my $ctrAREFEntStr = "";

my $uscExists = 0;

my $curfmt = "5800";
my $fmttbl = "5800";
my $inTable = 0;
my $centering=0;

my $curcod = "00";

my $inNotes = 0;

my @curFrag = ("","","","","","","","","","","","","","","","","","","","");
my @cntFrag = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
my $artNum  =0;

my $lcLen = length("[=LC=1234567]");  #-- length of locator code

#-------------------------------------
#-- Loop through the files
#-------------------------------------

for (my $i=$titleFirst; $i<=$titleLast; $i++) {

#-- try both base name and appendix...
#-- leading zero file numbering
foreach my $titlenumalt (sprintf( "%02d",$i ), sprintf( "%02da",$i ) ) {
$titlenum = $titlenumalt;
# print ("looking for uscode".$titlenum.".bell\n");

#-- file open US Code data in - 
$uscExists = 1;
open (USCODE, "< ../data/uscode".$titlenum.".bell") || do { $uscExists = 0; };

if ($uscExists) {
# print ("found uscode".$titlenum.".bell\n");

#-- tell gizer about the new $titlenum, and reset the gizer level tracker
&set_titlenum($titlenum);
&reset_prevLvl();

#-- make new directory, if needed, for this title
if(mkdir "uscode$titlenum"){print("Started new directory: uscode$titlenum\n");};

#-- start data tables of scope "input file"
&startTXTFile();
&startLBLFile(); #-- just the header records detected
# &startREFFile();

#-- start link tables of scope "input file"
&startTOCPage();
&startTOTPage();
for (my $i=1; $i<15; $i++) {
&startAREFPage($i); }


#-- reset file fragment counters
$ctrFragStr = "";
$ctrFrag    = 0;
$flgFragStr = sprintf("%05d",$ctrFrag); #-- marker for last flagged fragment

$rCode = 0;

@headlist = (); #-- expcite style list of super-section heads maintained as a stack 
@hdtykeys = (); #-- ssdiv keys that match headlist entries
# $prevLvl  = 0;
$pushHead = 0;
$replHead = 0;
$popHead  = 0;

$ctrTOFEntry = 0;
$ctrTOFEntStr = "";
$ctrTOTEntry = 0;
$ctrTOTEntStr = "";
$ctrAREFEntry = 0;
$ctrAREFEntStr = "";

&genHeaderPage();

&startDataPage(); #-- leader DataPage to collect file header oddments

#-- buffers for checking transitions

my $lineCntr	= 0;

my $dataRecTot	= 0;

# $ta	= "T";  #-- Title vs Appendix flag, used in document level hash key 
&reset_ta();  #-- (USCRinger.pm) Title vs Appendix flag, used in document level hash key 

my $cur_expcite;
my $cur_head;

my $rCodeChunk = "";

$curfmt = "5800";
$fmttbl = "5800";
$inTable = 0;
$centering=0;

@curFrag = ("","","","","","","","","","","","","","","","","","","","");
@cntFrag = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
$artNum  = 0;

#-------------------------------------
#-- Loop through the lines of the file
#-- chuncking at each newline
#-------------------------------------

$dataRecTot	= 0;

while (<USCODE>) {

$lineCntr++;
$chunk	= $_;

# print ("debug: length-chunk: ".length($chunk)."\n");

#-------------------------------------
#-- TRANSLATE to readable codes --
#-- NB: set_titlenum is called once per title, above
#-------------------------------------
my $outChunk = "";
$curcod = "00";
my $curlab = "";

#-- empty line (new record)
if (length($chunk)==1) { $chunk = "[=newrec]"; }
#-- otherwise, geezerize
else { $chunk  = &gizer($chunk); #-- first translate, then
       #-- sync with various initial-scan-related flags and variables --
       $rCode  = &get_rCode();
       $curcod = &get_curcod();
       ($pushHead, $replHead, $popHead) = &get_head_flags();
       $tmpkey = &get_tmpkey();
       $inTable = &get_inTable();
# print ("debug: tmpkey, push, repl, pop: ".$tmpkey.$pushHead.$replHead.$popHead."\n");
     }

if ( $chunk =~ /\[=EOF\]/ ) { &lastDataPage(); }


#---------------------------------------------
#---------- OUTPUT ---------------------------
#---------------------------------------------

#-- Line Break
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(06)|(07)|([1-3][0-8])|(45)|(47)|(48)|(53)|(55)|(70)|(74)|(79)|(81)/ ) ) { print(HTMOUT "<BR>"); }
if ( ($curfmt == "5804") and ( $curcod =~ /([2-3]7)|(70)/ ) ) { print(HTMOUT "<BR>"); }
if ( ($inTable         ) and ( $curcod =~ /(01)/ ) ) { print(HTMOUT "<BR>"); }
if ( $chunk =~ /(\[=table)|(\[=endtable)|(\[=fn)|(\[=boxhead)|(\[=endboxheads)|(\[=vertSpace)/ ) { print(HTMOUT "<BR>"); }

#-- Leader note
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(53)/ ) ) { $inNotes=1; print(HTMOUT "<BR><FONT COLOR=\"PURPLE\"><B>loc580x.53 - Source Credits</B></FONT><BR>\n"); }
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(06)/ ) ) { $inNotes=1; print(HTMOUT "<BR><FONT COLOR=\"PURPLE\"><B>loc580x.06 - Running Head</B></FONT><BR>\n"); }
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(73)/ ) ) { $inNotes=1; print(HTMOUT "<BR><FONT COLOR=\"PURPLE\"><B>loc580x.73 - Referred to in other</B></FONT><BR>\n"); }
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(75)/ ) ) { $inNotes=1; print(HTMOUT "<BR><FONT COLOR=\"PURPLE\"><B>loc580x.75 - References in text</B></FONT><BR>\n"); }
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(76)/ ) ) { $inNotes=1; print(HTMOUT "<BR><FONT COLOR=\"PURPLE\"><B>loc580x.76 - Codification</B></FONT><BR>\n"); }
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(78)/ ) ) { $inNotes=1; print(HTMOUT "<BR><FONT COLOR=\"PURPLE\"><B>loc580x.78 - Change of Name</B></FONT><BR>\n"); }
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(84)/ ) ) { $inNotes=1; print(HTMOUT "<BR><FONT COLOR=\"PURPLE\"><B>loc580x.84 - Italic Amendment Note</B></FONT><BR>\n"); }
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(85)/ ) ) { $inNotes=1; print(HTMOUT "<BR><FONT COLOR=\"PURPLE\"><B>loc580x.85 - Transfer of functions</B></FONT><BR>\n"); }
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(86)/ ) ) { $inNotes=1; print(HTMOUT "<BR><FONT COLOR=\"PURPLE\"><B>loc580x.86 - Ex, Ord. No. etc.</B></FONT><BR>\n"); }
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(87)/ ) ) { $inNotes=1; print(HTMOUT "<BR><FONT COLOR=\"PURPLE\"><B>loc580x.87 - Federal Rules, cross refs</B></FONT><BR>\n"); }


#-- mySQL ----------------------
#if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /04/ ) ) {
#  my $tmp1 = "\'".$titlenum."\'";
#  my $tmp2 = "\'".substr( $chunk, $lcLen )."\'";
#  $tmp2 =~ s/\[=md\]/\ \-\ /g;            #-- translate ndash
# #  $dbh->do("insert into $table (titlenum, titlenam) values ($tmp1, $tmp2)") or die $DBI::errstr;
#  $dbh->do("update $table set titlenam=$tmp2 where titlenum=$tmp1" ) or die $DBI::errstr;
#}
#-- end mySQL ----------------------

#-- Super-Section header update
if ( ( ($curfmt =~ /5800/) and ( $curcod =~ /(81)|(52)|(04)/ ) ) ||
     ( ($curfmt =~ /5802/) and ( $curcod =~ /(52)|(04)/ ) ) || 
     ( ($curfmt =~ /5804/) and ( $curcod =~ /(83)/ ) ) ) { 
	if ( $rCode ) { 
	if ($pushHead) { push ( @headlist, $chunk); push ( @hdtykeys, $tmpkey); $pushHead=0; &reset_pushHead(); }
	if ($replHead) { pop  ( @headlist ); pop (@hdtykeys); push ( @headlist, $chunk); push ( @hdtykeys, $tmpkey); $replHead=0; &reset_replHead();  }
	if ($popHead)  { 
	until ( (scalar(@hdtykeys)<3) || ($tmpkey eq pop(@hdtykeys)) ) {
		pop(@headlist) }
	pop(@headlist);  
	push(@headlist, $chunk); push ( @hdtykeys, $tmpkey); $popHead=0; &reset_popHead();  }
	my $headsize = @headlist;
	if ($headsize>0) {$headsize--;}
	my $headIndent = "";
	if ( "________________" =~ /((_){$headsize})/  ) { $headIndent = $1; }
	&startDataPage();
	&outDataChunk($rCodeChunk);
	&addTOCEntry($headIndent.$chunk);
	}
	}

#-- Section
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(80)/ ) ) {
	&startDataPage();
	print(HTMOUT "<BR><HR><FONT COLOR=\"PURPLE\" SIZE=\"+2\">Section</FONT><BR><HR>\n"); 
	chomp($chunk);
	}


#-- Tables
if ( $chunk =~ /(\[=table)/ ) { print(HTMOUT "\n<BR><FONT COLOR=\"PURPLE\" SIZE=\"+2\"><B>Table</B></FONT><BR>"); }
if ( ($inTable         ) and ( $curcod =~ /(95)/ ) ) { 
	$ctrTOTEntry++;
	$ctrTOTEntStr = sprintf("T%05d",$ctrTOTEntry); }
if ( ($inTable         ) and ( $curcod =~ /(95)/ ) ) { print(HTMOUT "<A NAME=\"".$ctrTOTEntStr."\"></A><BR><FONT COLOR=\"PURPLE\"><B>SGL0.95 - Table Title</B></FONT><BR>\n"); }
if ( ($inTable         ) and ( $curcod =~ /(96)|(97)/ ) ) { print(HTMOUT "<BR><FONT COLOR=\"PURPLE\"><B>SGL0.96(97?) - Headnote</B></FONT><BR>\n"); }
#-- Table of Tables entry
if ( ($inTable         ) and ( $curcod =~ /(95)/ ) ) { &addTOTEntry($chunk); }

#-- Labels
if ( 
    ( $chunk =~ /\[=LC=[0-9=]{7}\]((((?:[A-Z0-9]{1,5}\.)|(?:\([A-Za-z0-9]{1,5}\)))(\ \[=typeface[a-zA-Z0-9\ \[\]\=\/\,]+?\.\[=md\])?){1,4})/ )
 || ( $chunk =~ /\[=LC=[0-9=]{7}\]((((?:[A-Z0-9]{1,5}\.)|(?:\([A-Za-z0-9]{1,5}\)))(\ \[=typeface[a-zA-Z0-9\ \[\]\=\/\,]+?\.\[=md\])?){1,4})/ )
   ) { $curlab = $1;   }
if ( ($curcod == 18) || ($curcod == 19)  || ($curcod == 20) || ($curcod == 79) ) 
     { $curlab = substr( $chunk, $lcLen );   }  #-- $lcLen is length of locator code
if ( $curlab ) { 
#	print(HTMOUT "<BR>LocLab: [$curcod]$curlab<BR>\n");
	print(HTMOUT "<BR>\n");
	&addLBLFileLine("[$curcod]$curlab\n");
	}

# print(HTMOUT "</FONT>" );

#-- Centered things
if ( ($curfmt =~ /(5800)|(5802)/) and ( $curcod =~ /(04)|(51)|(52)|(54)|(61)|(71)|(74)|(81)|(83)/ ) ) { print(HTMOUT "<CENTER>\n<BR><B>"); $centering=1; }
if ( ($inTable         ) and ( $curcod =~ /(95)/ ) ) { print(HTMOUT "<CENTER>\n"); $centering=1; }



if ( $rCode ) { $rCodeChunk = $chunk; }


#-------------------------------------
#-- output every chunk -----------------
&outDataChunk($chunk);
#-- send the chunk to the plain text filtered file
&addTXTFileLine($chunk);
#-- send reference chunk to the reference candidate file
# if ( $curcod == 82) { &addREFFileLine($chunk); }
#---------------------------------------
#-------------------------------------

#-------------------------------------
#--- end centering
if ( $centering ) { print(HTMOUT "</B><\/CENTER>\n"); $centering=0; }
#-------------------------------------

} #-- end (while more lines in file)

close (USCODE);

} #-- file existence check
} #-- try both base name and appendix

} #-- end of loop through files

#-- final closings
close (TXTOUT);
close (HTMOUT);
close (HTMTOC);
close (HTMTOT);

for (my $i=1; $i<16; $i++){
print ( "debug cntFrag $i: ".$cntFrag[$i]."\n" );
}

#-- end geezer main (subs below) --

#####################################################################################################
#####################################################################################################
#---------------------------------------------------
sub genHeaderPage{
#---------------------------------------------------
$ctrFragStr = sprintf("%05d",$ctrFrag);
open (TITLHD, "> uscode".$titlenum."\/T".$titlenum."F".$ctrFragStr.".HTML") || die("\ncan't open output file uscode".$titlenum."\\T".$titlenum."F".$ctrFragStr.".HTML\n");
print(TITLHD q~<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
~ );
print(TITLHD "<HTML>\n<HEAD><TITLE>T".$titlenum."F".$ctrFragStr."</TITLE>\n");

#-- make keyboard script
print(TITLHD q~
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript1.2" type="text/javascript">
function link_keycode(){
~ );

$ctrFragStr = sprintf("%05d",$ctrFrag+1);
print(TITLHD "if \(event.keyCode==110\) window\.location=\"T".$titlenum."F".$ctrFragStr.".HTML\"\n" );
print(TITLHD "if \(event.keyCode==109\) window\.location=\"\.\.\\COMP_TEST.HTML\"\n" );

print(TITLHD q~
}
function opentoc(){
win2=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~TOC.HTML","","width=600,height=100,screenX=10,screenY=10,top=10,scrollbars=1,resizable=1")
win2.creator=self
}
function opentot(){
win3=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~TOT.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1")
win3.creator=self
}
function openlbl(){
win3=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~LBL.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1")
win3.creator=self
}
function openaref01(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF01.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref02(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF02.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref03(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF03.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref04(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF04.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref05(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF05.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref06(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF06.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref07(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF07.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref08(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF08.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref09(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF09.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref10(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF10.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref11(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF11.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref12(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF12.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref13(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF13.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
function openaref14(){
winaref=window.open("T~ );
print(TITLHD $titlenum );
print(TITLHD q~AREF14.HTML","","width=600,height=100,screenX=20,screenY=20,top=10,scrollbars=1,resizable=1");
winaref.creator=self;
}
</script>
</HEAD>
<BODY onKeyPress="link_keycode()" >
~ );
$ctrFragStr = sprintf("%05d",$ctrFrag);
print(TITLHD "\nT".$titlenum."F".$ctrFragStr."\n");
#-- make Next/Prev buttons --
$ctrFragStr = sprintf("%05d",$ctrFrag+1);
print(TITLHD "<A HREF=\"T".$titlenum."F".$ctrFragStr.".HTML\">(N)ext</A>\n");
print(TITLHD "<A HREF=\"\.\.\/COMP_TEST.HTML\">(M)ain</A>\n");
print(TITLHD "---".localtime(time())."\n");

print(TITLHD "<FONT COLOR=\"PURPLE\">" );
print (TITLHD  "*** uscode".$titlenum);
print (TITLHD  " (of titles $titleFirst through $titleLast, ".localtime(time()).")\n");
print ( "*** uscode".$titlenum);
print ( " (of titles $titleFirst through $titleLast, ".localtime(time()).")\n");
print (TITLHD  "<BR>\n");
print (TITLHD  "<B>Notes on the HTML</B><BR>\n");
print (TITLHD  "Highlights in purple, vertical space, and H-rules are added metadata.<BR>\n");
print (TITLHD  "Original data is either black (bold or not) for printing ASCII, or blue square braketted non-printing codes as translated.<BR>\n");
print (TITLHD  "<BR>\n");
print (TITLHD  "<HR>\n");
print(TITLHD "</FONT>" );

print (TITLHD  "<BR><A NAME=\"1\" HREF=\"#1\" onClick=\"opentoc()\">Table of Contents</A>\n");

print (TITLHD  "<BR><A NAME=\"1\" HREF=\"#1\" onClick=\"opentot()\">Table of Tables, Flags, etc.</A>\n");

print (TITLHD  "<BR><A NAME=\"1\" HREF=\"#1\" onClick=\"openlbl()\">Flags on labels, etc.</A>\n");

#-- close with last page (stats available)

#-- dummy file to give startDataPage something to close the first time.
open (HTMOUT, "> uscode".$titlenum."\/z.HTML") || die("\ncan't open output file uscode".$titlenum."\\T".$titlenum."\/z.HTML\n");
}
#---------------------------------------------------


#---------------------------------------------------
sub startDataPage{
#-- starts a fresh data fragment, currently for
#-- (1) an Rcode, or
#-- (2) a Section
#---------------------------------------------------
print(HTMOUT "\n</BODY>\n</HTML>");
close(HTMOUT);
$ctrFrag++;
$ctrFragStr = sprintf("%05d",$ctrFrag);
open (HTMOUT, "> uscode".$titlenum."\/T".$titlenum."F".$ctrFragStr.".HTML") || die("\ncan't open output file uscode".$titlenum."\\T".$titlenum."F".$ctrFragStr.".HTML\n");
print(HTMOUT q~<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
~ );
print(HTMOUT "<HTML>\n<HEAD><TITLE>T".$titlenum."F".$ctrFragStr."</TITLE>\n");

#-- make keyboard script
print(HTMOUT q~
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript1.2" type="text/javascript">
function link_keycode(){
~ );

print(HTMOUT "if (event.keyCode==98)  history.go(-1)\n" );

$ctrFragStr = sprintf("%05d",$ctrFrag-1);
print(HTMOUT "if \(event.keyCode==112\) window\.location=\"T".$titlenum."F".$ctrFragStr.".HTML\"\n" );
$ctrFragStr = sprintf("%05d",$ctrFrag+1);
print(HTMOUT "if \(event.keyCode==110\) window\.location=\"T".$titlenum."F".$ctrFragStr.".HTML\"\n" );
#-- T=TOC
$ctrFragStr = sprintf("%05d",$ctrFrag);
print(HTMOUT "if \(event.keyCode==120\) window\.location=\"\.\.\/\.\.\/emely\/uscode".$titlenum."\/T".$titlenum."F".$ctrFragStr.".XML\"\n" );
print(HTMOUT "if \(event.keyCode==116\) window\.location=\"T".$titlenum."F00000.HTML\"\n" );
# print(HTMOUT "if \(event.keyCode==102\) window\.location=\"T".$titlenum."F".$flgFragStr.".HTML\"\n" );
# print(HTMOUT "if \(event.keyCode==109\) window\.location=\"\.\.\\\\COMP_TEST.HTML\"\n" );

print(HTMOUT q~
}
</script>
<STYLE  TYPE="text/css">
I  { color: blue }
I  { font-style: normal }
U  { color: black }
U  { font-style: normal }
U  { font-weight: normal }
U  { font-size: normal }
U  { text-decoration: none }
U  { background-color: #FFDDEE }
</STYLE>
</HEAD>
<BODY onKeyPress="link_keycode()" >
~ );

$ctrFragStr = sprintf("%05d",$ctrFrag);
print(HTMOUT "T".$titlenum."F".$ctrFragStr."\n");

#-- make control buttons --
$ctrFragStr = sprintf("%05d",$ctrFrag-1);
print(HTMOUT "<A HREF=\"T".$titlenum."F".$ctrFragStr.".HTML\">(p)rev</A>\n");
$ctrFragStr = sprintf("%05d",$ctrFrag+1);
print(HTMOUT "<A HREF=\"T".$titlenum."F".$ctrFragStr.".HTML\">(n)ext</A>\n");
$ctrFragStr = sprintf("%05d",$ctrFrag);
print(HTMOUT "<A HREF=\"\.\.\/\.\.\/emely\/uscode".$titlenum."\/T".$titlenum."F".$ctrFragStr.".XML\">(x)ml</A>\n");
print(HTMOUT "<A HREF=\"T".$titlenum."F00000.HTML\">(t)OC</A>\n");
# print(HTMOUT "<A HREF=\"\.\.\/COMP_TEST.HTML\">(m)ain</A>\n");
# print(HTMOUT "<A HREF=\"T".$titlenum."F".$flgFragStr.".HTML\">(F)lagged</A>\n");
print(HTMOUT "---".localtime(time())."\n");

   my $tmphead = join("\n<BR>",@headlist);
   $tmphead =~ s/\[=md\]/\ \-\ /g;
   $tmphead =~ s/\[=nd\]/\-/g;
   $tmphead =~ s/\[=fn\][0-9]\[=fn\]//g;     #-- discard footnote numbers
   $tmphead =~ s/\[[a-zA-Z0-9\ \=\.]+\]//g;
if ($rCode) { #-- trim the last entry, since it duplicates the current code head
   $tmphead =~ s/<BR>.+?$//; }
   print(HTMOUT "\n<BR><BR><FONT COLOR=\"PURPLE\">".$tmphead."<\/FONT>\n<BR>");

if ($rCode) {
my $RcodeNomenTmp = &getRcodeNomen("$tmpkey");
if ($RcodeNomenTmp) {
   print(HTMOUT "<BR><HR><FONT COLOR=\"PURPLE\" SIZE=+1>".$RcodeNomenTmp."<BR><HR><\/FONT>\n"); 
   } else {
   $ctrTOFEntry++;
   &addTOFEntry( "Heading Type not found; Key=".$tmpkey );
   print(HTMOUT "<BR><HR><FONT COLOR=RED SIZE=+1>Heading Type not found; Key=".$tmpkey."<BR><HR><\/FONT>\n"); 
   }
   $rCode = 0;
   &reset_rCode();
   }
$inNotes=0;
}
#---------------------------------------------------

#---------------------------------------------------
sub outDataChunk($){
#-- filters a chunk for HTML and writes it
#---------------------------------------------------
my $nextOut = shift;

#-- experimental xref detection (highlights and collects stats)
$nextOut = &markArefs($nextOut);

# $nextOut =~ s/<\/AREF>/<\/U>/g;
while (
$nextOut =~ s/<AREF\ TY=\"([0-9]{1,2})\">(.*?)<\/AREF>/<FONT COLOR=\"PURPLE\">\{ART\-$1\}<\/FONT><U>$2<\/U>/g
) { # print("debug: $1-$2\n");
	$artNum  = $1;
     my $curAREF = $2;
     #-- stats --
	 $curFrag[$artNum] = "T".$titlenum."F".$ctrFragStr;
	 $cntFrag[$artNum]++;
     #-- increment and stringify AREF counter
	$ctrAREFEntry++; 
	$ctrAREFEntStr = sprintf("R%05d",$ctrAREFEntry);
     #-- use AREF counter as a label for (here) in the html
	print(HTMOUT "\n<A NAME=\"".$ctrAREFEntStr."\"></A>\n");
     #-- add to the appropriate toc
     if ( $cntFrag[$artNum] < 99 ) { #-- don't make lists too long (fancier thinning idea, anyone?
	&addAREFEntry( $artNum, $curAREF );
	}
}

#-- flag uncaptured bell codes...
if ( $nextOut =~ /(\[=bell\-[A-Za-z]\])/ ) {
   $ctrTOFEntry++;
   my $ctrTOFEntStr = sprintf("Flg%05d",$ctrTOFEntry);
   print(HTMOUT "\n<BR><A NAME=\"".$ctrTOFEntStr."\"></A><FONT COLOR=RED>Bell code not captured; code=".$1."</FONT></A><BR>\n" );
   &addTOFEntry( "Bell code not captured; code=".$1 ); }

#-- look for indicative index noun phrase candidates
#-- current guess: simple one to four word phrase in double quotes
#while ( !$inNotes && $nextOut =~ /\[=ldq\]([a-zA-Z]+(?:\ [a-zA-Z]+){0,3})\[=rdq\]/g ) {
#print ("[T".$titlenum."F".$ctrFragStr."] $1\n");
#}

#-- sample the strange new codes
#while ( $nextOut =~ /\[=(AEAF CD=[A-Z]{4})\]/g ) {
#print ("debug: $1\n");
#}

#-- translating to blue indirectly through CSS override of <I>
   $nextOut =~ s/(\[=.+?\])/<I>$1<\/I>/g;
   
print(HTMOUT $nextOut);
}
#---------------------------------------------------


#---------------------------------------------------
sub lastDataPage{
#---------------------------------------------------

#-- First, finish up 
print (TITLHD  "<HR>\n");
for ( my $i=1; $i<15; $i++ ) {
my $artNumStr = sprintf("%02d",$i);
print (TITLHD  "<BR><A NAME=\"1\" HREF=\"#1\" onClick=\"openaref$artNumStr()\">Table of Abstract Reference Type ".$artNumStr." (".$cntFrag[$i].")</A>\n");
}

print (TITLHD "</BODY>\n</HTML>");
close (TITLHD);

#---------------------------------------------------

print(HTMOUT "\n</BODY>\n</HTML>");
close(HTMOUT);
$ctrFragStr = sprintf("%05d",++$ctrFrag);
open (HTMOUT, "> uscode".$titlenum."\/T".$titlenum."F".$ctrFragStr.".HTML") || die("\ncan't open output file uscode".$titlenum."\\T".$titlenum."F".$ctrFragStr.".HTML\n");
print(HTMOUT q~<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
~ );
print(HTMOUT "<HTML>\n<HEAD><TITLE>T".$titlenum."F".$ctrFragStr."</TITLE>\n");

#-- make keyboard script
print(HTMOUT q~
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript1.2" type="text/javascript">
function link_keycode(){
~ );

print(HTMOUT "if (event.keyCode==98)  history.go(-1)\n" );

$ctrFragStr = sprintf("%05d",$ctrFrag-1);
print(HTMOUT "if \(event.keyCode==112\) window\.location=\"T".$titlenum."F".$ctrFragStr.".HTML\"\n" );
# $ctrFragStr = sprintf("%05d",$ctrFrag+1);
# print(HTMOUT "if \(event.keyCode==110\) window\.location=\"T".$titlenum."F".$ctrFragStr.".HTML\"\n" );
# print(HTMOUT "if \(event.keyCode==102\) window\.location=\"T".$titlenum."F".$flgFragStr.".HTML\"\n" );
print(HTMOUT "if \(event.keyCode==109\) window\.location=\"\.\.\\\\COMP_TEST.HTML\"\n" );

print(HTMOUT q~
}
</script>
</HEAD>
<BODY onKeyPress="link_keycode()" >
~ );

$ctrFragStr = sprintf("%05d",$ctrFrag);
print(HTMOUT "\nT".$titlenum."F".$ctrFragStr."\n");

#-- make Next/Prev buttons --
$ctrFragStr = sprintf("%05d",$ctrFrag-1);
print(HTMOUT "<A HREF=\"T".$titlenum."F".$ctrFragStr.".HTML\">(P)rev</A>\n");
# $ctrFragStr = sprintf("%05d",$ctrFrag+1);
# print(HTMOUT "<A HREF=\"T".$titlenum."F".$ctrFragStr.".HTML\">(N)ext</A>\n");
print(HTMOUT "<A HREF=\"\.\.\/COMP_TEST.HTML\">(M)ain</A>\n");
# print(HTMOUT "<A HREF=\"T".$titlenum."F".$flgFragStr.".HTML\">(F)lagged</A>\n");
print(HTMOUT "---".localtime(time())."\n");

print(HTMOUT "<BR><HR><FONT COLOR=\"PURPLE\" SIZE=\"+2\">End of File<BR><HR><\/FONT>\n"); 

}
#---------------------------------------------------


############## Table of Contents ###########################################
#---------------------------------------------------
sub startTOCPage{
#---------------------------------------------------
open (HTMTOC, "> uscode".$titlenum."\/T".$titlenum."TOC.HTML") || die("\ncan't open output file\n");
print(HTMTOC q~<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
~ );
print(HTMTOC "<HTML>\n<HEAD><TITLE>T".$titlenum."TOC</TITLE>\n");
print(HTMTOC q~
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function pager(url){
window.opener.location=url
}
</script>
~);
print(HTMTOC "</HEAD>\n<BODY>\nT".$titlenum."TOC\n");
print(HTMTOC "---".localtime(time())."\n");
print(HTMTOC "<FONT COLOR=\"PURPLE\" SIZE=+1>TOC<BR><HR><\/FONT>\n"); 
}
#---------------------------------------------------

#---------------------------------------------------
sub addTOCEntry($){
#---------------------------------------------------
my $ctrFragStr = sprintf("%05d",$ctrFrag);
my $nextEntry = shift;
   $nextEntry =~ s/_/\.\.\./g;                 #-- play with indentation
   $nextEntry =~ s/\[=md\]/\ \-\ /g;        #-- translate emdashes
   $nextEntry =~ s/\[=fn\][0-9]\[=fn\]//g;     #-- discard footnote numbers
   $nextEntry =~ s/\[=[a-zA-Z0-9\ \=\.]+\]//g; #-- discard all other tags
my $fn = "T".$titlenum."F".$ctrFragStr.".HTML";
print(HTMTOC "<BR><A NAME=\"$ctrFragStr\" HREF=\"#".$ctrFragStr."\" onClick=\"pager(\'".$fn."\')\" onFocus=\"pager(\'".$fn."\')\">".$nextEntry."<\/FONT></A>\n");
}
#---------------------------------------------------


############## Table of Flagged Things ###########################################
#---------------------------------------------------
sub startTOTPage{
#---------------------------------------------------
open (HTMTOT, "> uscode".$titlenum."\/T".$titlenum."TOT.HTML") || die("\ncan't open output file\n");
print(HTMTOT q~<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
~ );
print(HTMTOT "<HTML>\n<HEAD><TITLE>T".$titlenum."TOT</TITLE>\n");
print(HTMTOT q~
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function pager(url){
window.opener.location=url
}
</script>
~);
print(HTMTOT "</HEAD>\n<BODY>\nT".$titlenum."TOT\n");
print(HTMTOT "---".localtime(time())."\n");
print(HTMTOT "<FONT COLOR=\"PURPLE\" SIZE=+1>Table of Tables and other flagged things<BR><HR><\/FONT>\n"); 
}
#---------------------------------------------------

#---------------------------------------------------
sub addTOTEntry($){
#---------------------------------------------------
my $ctrTOTEntStr = sprintf("T%05d",$ctrTOTEntry);
my $ctrFragStr = sprintf("%05d",$ctrFrag);
my $nextEntry = shift;
   $nextEntry =~ s/\[[a-zA-Z0-9\ \=\.]+\]//g;  #-- discard all other tags
my $fnp = "T".$titlenum."F".$ctrFragStr.".HTML#".$ctrTOTEntStr;
print(HTMTOT "<BR><A NAME=\"$ctrFragStr\" HREF=\"#".$ctrFragStr."\" onClick=\"pager(\'".$fnp."\')\" onFocus=\"pager(\'".$fnp."\')\"><FONT SIZE=-1>".$ctrTOTEntStr." - ".$nextEntry."<\/FONT></A>\n");
}
#---------------------------------------------------

#---------------------------------------------------
#--- for now, put flags in the table of tables
sub addTOFEntry($){
#---------------------------------------------------
my $ctrTOFEntStr = sprintf("Flg%05d",$ctrTOFEntry);
my $ctrFragStr = sprintf("%05d",$ctrFrag);
my $nextEntry = shift;
   $nextEntry =~ s/\[[a-zA-Z0-9\ \=\.]+\]//g;  #-- discard all other tags
# print(HTMTOT "<BR><A HREF=\"T".$titlenum."F".$ctrFragStr.".HTML#".$ctrTOFEntStr."\"><FONT SIZE=-1>".$ctrTOFEntStr." - ".$nextEntry."<\/FONT></A>\n"); 
my $fnp = "T".$titlenum."F".$ctrFragStr.".HTML#".$ctrTOFEntStr;
print(HTMTOT "<BR><A NAME=\"".$ctrFragStr.$ctrTOFEntStr."\" HREF=\"#".$ctrFragStr.$ctrTOFEntStr."\" onClick=\"pager(\'".$fnp."\')\" onFocus=\"pager(\'".$fnp."\')\">".$ctrTOFEntStr." - ".$nextEntry."<\/FONT></A>\n");
}
#---------------------------------------------------


############## Table of Abstract References ###########################################
#---------------------------------------------------
sub startAREFPage($){
#---------------------------------------------------
no strict 'refs'; #-- strict refs doesn't let the variable filehandle thing work
my $artNum = shift;
my $arfNam = sprintf("AREF%02d",$artNum);
open ($arfNam, "> uscode".$titlenum."\/T".$titlenum.$arfNam."\.HTML") || die("\ncan't open output file\n");
print($arfNam q~<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
~ );
print($arfNam "<HTML>\n<HEAD><TITLE>T".$titlenum.$arfNam."</TITLE>\n");
print($arfNam q~
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function pager(url){
window.opener.location=url
}
</script>
~);
print($arfNam "</HEAD>\n<BODY>\nT".$titlenum.$arfNam."\n");
print($arfNam "---".localtime(time())."\n");
print($arfNam "<FONT COLOR=\"PURPLE\" SIZE=+1>Table of Abstract References<BR><HR><\/FONT>\n"); 
}
#---------------------------------------------------

#---------------------------------------------------
sub addAREFEntry(@){
#---------------------------------------------------
no strict 'refs'; #-- strict refs doesn't let the variable filehandle thing work
my $artNum = shift;
my $arfNam = sprintf("AREF%02d",$artNum);
my $ctrAREFEntStr = sprintf("R%05d",$ctrAREFEntry);
my $ctrFragStr = sprintf("%05d",$ctrFrag);
my $nextEntry = shift;
if ($nextEntry){
   $nextEntry =~ s/\[=st\]\[=ts\]/&#167;/g;  #-- translate section twist
   $nextEntry =~ s/\[=nd\]/\-/g;            #-- translate ndash
   $nextEntry =~ s/\[[a-zA-Z0-9\ \=\.]+\]//g;  #-- discard all other tags
my $fnp = "T".$titlenum."F".$ctrFragStr.".HTML#".$ctrAREFEntStr;
print($arfNam "<BR><A NAME=\"".$ctrFragStr.$ctrAREFEntStr."\" HREF=\"#".$ctrFragStr.$ctrAREFEntStr."\" onClick=\"pager(\'".$fnp."\')\" onFocus=\"pager(\'".$fnp."\')\"><FONT SIZE=-1>".$ctrAREFEntStr." - ".$nextEntry."<\/FONT></A>\n");
}
}
#---------------------------------------------------

############## Plain Text File ###########################################
#---------------------------------------------------
sub startTXTFile{
#---------------------------------------------------
open (TXTOUT, "> uscode".$titlenum."\/T".$titlenum.".TXT") || die("\ncan't open output file uscode".$titlenum."\/T".$titlenum.".TXT\n");
print(TXTOUT "[=comment]\nThis file contains USC composition code data,\nwith original codes translated into readable ascii\nin [=square brackets] (<<[=>> to distinguish from ordinary text <<[>>)\n[=endcomment]\n");
}
#---------------------------------------------------

#---------------------------------------------------
sub addTXTFileLine($){
#---------------------------------------------------
my $nextEntry = shift;
$nextEntry =~ s/\[=newrec\]/\[=newrec\]\n/g; #-- symbolic for empty line (new record)
print(TXTOUT $nextEntry ); 
}
#---------------------------------------------------

############## Reference Candidates File ###########################################
#---------------------------------------------------
sub startREFFile{
#---------------------------------------------------
open (REFOUT, "> R".$titlenum.".TXT") || die("\ncan't open output file uscode".$titlenum."\/T".$titlenum.".TXT\n");
print(REFOUT "[=comment]\nThis file contains USC composition code data,\nwith original codes translated into readable ascii\nin [=square brackets] (<<[=>> to distinguish from ordinary text <<[>>)\n*** Reference Candidates Only ***\n[=endcomment]\n");
}
#---------------------------------------------------

#---------------------------------------------------
sub addREFFileLine($){
#---------------------------------------------------
my $nextEntry = shift;
$nextEntry =~ s/\[=newrec\]/\[=newrec\]\n/g; #-- symbolic for empty line (new record)
print(REFOUT $nextEntry ); 
}
#---------------------------------------------------

############## Header record File ###########################################
#---------------------------------------------------
sub startLBLFile{
#---------------------------------------------------
open (LBLOUT, "> uscode".$titlenum."\/T".$titlenum."LBL.TXT") || die("\ncan't open output file uscode".$titlenum."\/T".$titlenum."LBL.TXT\n");
}
#---------------------------------------------------

#---------------------------------------------------
sub addLBLFileLine($){
#---------------------------------------------------
my $nextEntry = shift;
print(LBLOUT $nextEntry ); 
}
#---------------------------------------------------


1;
__END__;
