#!/usr/bin/perl -w
# 
# thm2txt,  Harry Plantinga.  This program may be copied
# under the terms of the Artistic License.
#
# This script is a first attempt at ThML to text conversion.
use strict;
use ThMLutil;

my ($bookID, $footnotes, $notenum, $body, $input);
$footnotes = "";
$notenum = "1";

while (<>) 			#read entire file into $input
  { $input .= $_; }
$_ = $input;
print STDERR "\nthm2txt: " . length($input) . " bytes read\n";
$bookID=$1        if m|<bookID\s*>(.*?)</bookID\s*>|is; 

s|(<note.*?>.*?</note>)|&note($1,$notenum)|gsie;
$_ .= "\n___________________\n$footnotes\n"; 

s|.*?<div1|<div1|is;	# delete head
s|<div1 title="Indexes.*?</div1>||is;
s|<p[^>]*>&nbsp;</p>\s*|\n|gis;
s|&nbsp;| |g;
s|&amp;|\&|g;
s|&#8217;|'|g;
s|&#8212;|--|g;
s|&#8211;|\&|g;
s|&#822[01];|"|g;
s|<i>(.*?)</i>|$1|gis;
s|<b>(.*?)</b>|$1|gis;
s|</?foreign[^>]*>||gis;
s|</?pb[^>]*>||gis;
s|</?span[^>]*>||gis;
s|</?sup[^>]*>||gis;
s|</?scrip[^>]*>||gis;
s|</?index[^>]*>||gis;
s|</?insertIndex[^>]*>||gis;
s|</?insertIndex[^>]*>||gis;
s|</?div[^>]*>||gis;
s|</?ThML[^>]*>||gis;
s|<deleted.*?</deleted>||gsi;	#delete deleted stuff
s|<added.*?</added>||gsi;	#delete added stuff

s|<p class="footnote[^>]*>(.*?)</p>|&wrap($1)|gsie;
s|<p class="endnote[^>]*>(.*?)</p>|&wrap($1)|gsie;
s|<p[^>]*>(.*?)</p>|&cwrap($1)|gsie;


print STDERR "output: " . length($_) . " bytes\n";
print;
exit(0);



#-------------------------subroutines----------------------------


sub cwrap
{
  my $stuff = shift;
  my $result = "";
  $stuff =~ s/\s+/ /gis;

  if (length($stuff)<72) {
    return " " x (36 - length($stuff)/2) . $stuff . "\n";
  } 

  while (length($stuff)>72 and $stuff =~ s/^(.{1,72}\s+)//is) {
    $result .= $1 . "\n"; }  
  $result .= $stuff;
  $result .= "\n";
  return $result;
}
  

sub wrap
{
  my $stuff = shift;
  my $result = "";
  $stuff =~ s/\s+/ /gis;

  while (length($stuff)>72 and $stuff =~ s/^(.{1,72}\s+)//is) {
    $result .= $1 . "\n"; }  
  $result .= $stuff;
  $result .= "\n";
  return $result;
}
  

sub note
{
  my $note=shift;
# print "Processing footnote $note -- number $notenum\n" if $debug;

  $note =~ s|</?note[^>]*>||g;
  $footnotes .= "\n[$notenum]$note";
  my $fref="[$notenum]";
  $notenum++;
# print "Returning: $fref\n";
  return $fref; 
}

