#!/usr/local/bin/perl

## Input parameters

$input = $ARGV[0];
$table = 1  if ($ARGV[1] eq "-t");

# table parameters

$t_width = "800px";
$t_border = "1px";
$t_cellpadding = "50px";
$t_cellspacing = "0px";
$m_marginwidth = "100px";

## Open input/output files

unless (open(INPUT, $input))  {
  die "\nCan't find input file: $input\n\n";
}

while (<INPUT>)  {
  @input_data = (@input_data,$_);
}

close(INPUT);

## Search/Replace/Convert routine

$length = @input_data;

for ($i=0;$i<=$length;$i++)  {
  $a = $input_data[$i];
  study($a);
  if ($a =~ /^>/)  {					# Replace >s on wrong line
    $a =~ s/^>//g;
    $input_data[$i-1] =~ s/\n//g;
    $input_data[$i-1] = $input_data[$i-1] . ">\n";
  }
  if ($a =~ /<[^>]*\n$/)  {				# Get <tag>s on same line as
    $a =~ s/\n//g;					# parameters
    if ($input_data[$i+1] =~ />/  &&  $input_data[$i] !~ /DOCTYPE/)  {
      $input_data[$i+1] =~ s/^[^>]*>/ /;
      $a = $a . $& . "\n";
    }  elsif ($input_data[$i+2] =~ />/  &&  $input_data[$i] !~ /DOCTYPE/)  {
      $input_data[$i+1] =~ s/\n/ /;
      $a = $a . $input_data[$i+1];
      $input_data[$i+1] = "\n";
      $input_data[$i+2] =~ s/^[^>]*>/ /;
      $a = $a . $& . "\n";
    }
  }
  $a =~ s/\n?\s*\n?>/>/g;				#
  $a =~ s/\s*(\/>)/ $1/g;				# Get rid of some extra spaces
  $a =~ s/(<\w*)\s+/$1 /g;				# 
  $a =~ s/>/>\n/g;
  $a =~ s/</\n</g;

  $input_data[$i] = $a;
}

$length = @input_data;

for ($i=0;$i<=$length;$i++)  {
  $a = $input_data[$i];
  if ($a =~ /^\s*\n\s*$/)  {
    $a = "\n";
  }  else  {
    $a =~ s/\n?\s*\n?>/>/g;				#
    $a =~ s/\s*(\/>)/ $1/g;				# Get rid of some extra spaces
    $a =~ s/(<\w*)\s+/$1 /g;				# 
    $a =~ s/^\s*</</;					# Get rid of extra space at start of newline
    if ($a =~ /(<body[^>]*)/  &&  $table == 1)  {
      ($before,$after) = split(/<body[^>]*>/, $a);
      $a = $before . $1 . " marginwidth=\"" . $m_marginwidth . "\">\n";
      $a = $a . "<table width=\"" . $t_width . "\" border=\"" . $t_border . "\" cellpadding=\"" . 
                    $t_cellpadding . "\" cellspacing=\"" . $t_cellspacing . "\">\n";
      $a = $a . "<tr>\n<td>\n";
      $a = $a . $after . "\n"  unless ($after =~ /^\s*\n/);
    }  elsif ($a =~ /(<\/body[^>]*>)/  &&  $table == 1)  {
      ($before,$after) = split(/<\/body[^>]*>/, $a);
      $a = $before . "\n"  unless ($before =~ /^\s*\n/);
      $a = $a . "</td>\n</tr>\n</table>\n";
      $a = $a . $1 . "\n";
      $a = $a . $after . "\n"  unless ($after =~ /^\s*\n/);
    }
    $a =~ s/\n+/\n/g;					# Get rid of extra newlines
    $a =~ s/\n\s+\n/\n/g;				# Get rid of extra newlines with whitespace
    $input_data[$i] = $a;
  }
}

for ($i=0;$i<=$length;$i++)  {
  $input_data[$i] =~ s/\s*\n+\s*\n*\s*/\n/g;
  @temp = split("\n", $input_data[$i]);
  $temp_length = @temp;
  for ($k=0;$k<=$temp_length;$k++)  {
    $temp[$k] = $temp[$k] . "\n"  unless (!$temp[$k]);
  }
  @input_data_2 = (@input_data_2, @temp);
}

$length = @input_data_2;
for ($i=0;$i<=$length;$i++)  {
  $a = $input_data_2[$i];
  study($a);
  if ($a =~ /<(\w*)[^>]*>/)  {			# put <tag>data</tag> pairs on same line
    if ($input_data_2[$i+1] =~ /<\/$1>/)  {
      $a =~ s/\n//g;
      $input_data_2[$i+1] =~ s/\n//g;
      $a = $a . $input_data_2[$i+1] . "\n";
      $input_data_2[$i+1] = "\n";
    }
    if ($input_data_2[$i+2] =~ /<\/$1>/)  {
      $a =~ s/\n//g;
      $input_data_2[$i+1] =~ s/\n//g;
      $input_data_2[$i+2] =~ s/\n//g;
      $a = $a . $input_data_2[$i+1] . $input_data_2[$i+2] . "\n";
      $input_data_2[$i+1] = "\n";
      $input_data_2[$i+2] = "\n";
    }
  }

  # Replace various characters, strings
  $a =~ s/\&emsp\;/\&nbsp\;/g;				# Replace spacing character
  $a =~ s/Ť/\&Lt\;/g;					# Replace spacing character
  $a =~ s/class="equation"/mode="display"/g;		# Replace equation identifier
  $a =~ s/×/\&times\;/g;				# Replace multiplication sign
  $a =~ s/\&mmlns\;/\&mathml\;/g;			# Updated xml-namespace
  $a =~ s/ą/\&PlusMinus\;/g;				# Replace plus-minus character

  # Add various glyphs which the conversion program should have done correctly, replacing image tags
  if ($a =~ /alt="==&gt\;"[^>]*>/)  {
    $a =~ s/<img[^>]*>/&rArr\;/g;			# Add ==> glyph
  }  if ($a =~ /alt="--&gt\;"[^>]*>/)  {
    $a =~ s/<img[^>]*>/&rarr\;/g;			# Add --> glyph
  }  if ($a =~ /alt="e"[^>]*>/)  {
    $a =~ s/<img[^>]*>/&epsi\;/g;			# Add epsilon glyph
  }  if ($a =~ /alt=" -~ "[^>]*>/)  {
    $a =~ s/<img[^>]*>/&sime\;/g;			# Add -~ glyph
  }  if ($a =~ /alt=" ~~ "[^>]*>/)  {
    $a =~ s/<img[^>]*>/&ap\;/g;				# Add ~~ glyph
  }  if ($a =~ /alt="p"[^>]*>/)  {
    $a =~ s/<img[^>]*>/&pi\;/g;				# Add Pi glyph
  }  if ($a =~ /alt=" V~ "[^>]*>/)  {
    $a =~ s/<img[^>]*>//g;				# Get rid of weird " V~ " (sqrt attempt..?)
  }  if ($a =~ /alt=" integral "[^>]*>/)  {
    $a =~ s/<img[^>]*>/&int\;/g;			# Add Integral glyph
  }  if ($a =~ /alt=" =_ "[^>]*>/)  {
    $a =~ s/<img[^>]*>/&equiv\;/g;			# Add 'equivalence' glyph
  }  if ($a =~ /alt="a"[^>]*>/)  {
    $a =~ s/<img[^>]*>/&alpha\;/g;			# Add alpha glyph
  }  if ($a =~ /alt="b"[^>]*>/)  {
    $a =~ s/<img[^>]*>/&beta\;/g;			# Add beta glyph
  }  if ($a =~ /alt="g"[^>]*>/)  {
    $a =~ s/<img[^>]*>/&gamma\;/g;			# Add gamma glyph
  }  if ($a =~ /alt=" sum "[^>]*>/)  {
    $a =~ s/<img[^>]*>/&sum\;/g;			# Add summation glyph
  }  if ($a =~ /alt=" oo "[^>]*>/)  {
    $a =~ s/<img[^>]*>/&infin\;/g;			# Add infinity glyph
  }

  # Bring equation numbers outside <math> pair inside and format all equation numbers
  if ($a =~ /<\/math>/  &&  $input_data_2[$i+1] =~ /\(\d+\.\d+\)/)  {
    $input_data_2[$i+1] =~ s/\(\d+\.\d+\)//g;
    $a = "<mn>&nbsp\;&nbsp\;&nbsp\;&nbsp\;&nbsp\;" . $& . "</mn>\n</math>\n";
  }
  if ($a =~ /<mtext/  &&  $input_data_2[$i+1] =~ /\(\d+\.\d+\)/)  {
    $input_data_2[$i+1] = "&nbsp\;&nbsp\;&nbsp\;&nbsp\;&nbsp\;" . $input_data_2[$i+1];
  }

  #  Add <mrow> pair surrounding math block, just inside <math> pair
  if ($a =~ /<math[^>]*>/)  {
    $a = $` . $& . "<mrow>\n" . $';
  }
  if ($a =~ /<\/math>/)  {
    $a = $` . "</mrow>\n" . $& . $';
  }

#  if ($a =~ /<math[^>]*>/  &&  $input_data_2[$i+1] =~ /<mtable[^>]*>/)  {
#    $a = $a . "<mrow>\n";
#  }
#  if ($a =~ /<\/math>/  &&  $input_data_2[$i-1] =~ /<\/mtable>/)  {
#    $a = "</mrow>\n" . $a;
#  }

  $input_data_2[$i] = $a;
}

for ($i=0;$i<=$length;$i++)  {
  $input_data_2[$i] =~ s/\s*\n+\s*\n*\s*/\n/g;
  @temp = split("\n", $input_data_2[$i]);
  $temp_length = @temp;
  for ($k=0;$k<=$temp_length;$k++)  {
    $temp[$k] = $temp[$k] . "\n"  unless (!$temp[$k]);
  }
  @input_data_3 = (@input_data_3, @temp);
}

unless (open(OUTPUT, ">" . $input))  {
  die "\nCan't open output file: $input for writing\n\n";
}

for ($i=0;$i<=$length;$i++)  {
  $a = $input_data_3[$i];
  study($a);

  # Force separators to be null unless they are defined
  $a =~ s/^(<mfenced[^>]*)/$1 separators=""/g  unless ($a =~ /separators="[^"]*"/);

  # Double and triple primes to be rendered in one <mi></mi> block
  if ($a =~ /^<mi>'<\/mi>/				&&  
      $input_data_3[$i+1]  =~  /^<mi>'<\/mi>/		&&
      $input_data_3[$i+2]  !~  /^<mi>'<\/mi>/)  {
    $a = "<mi>\'\'</mi>\n";
    $input_data_3[$i+1] = "\n";
  }
  if ($a =~ /^<mi>'<\/mi>/				&&  
      $input_data_3[$i+1]  =~  /^<mi>'<\/mi>/		&&
      $input_data_3[$i+2]  =~  /^<mi>'<\/mi>/		&&
      $input_data_3[$i+3]  !~  /^<mi>'<\/mi>/)  {
    $a = "<mi>\'\'\'</mi>\n";
    $input_data_3[$i+1] = "\n";
    $input_data_3[$i+2] = "\n";
  }

  print OUTPUT $a  unless ($a =~ /^\s*\n\s*$/);
}

close(OUTPUT);






