#!/usr/local/bin/perl

##
## Very limited HTML -> XHTML/MathML converter
##
## Syntax example:
## perl convert.pl -i input.html [-o output.xml -s (stylesheet.css) -t]
##

## Main parameters

$math_xmlns = "http://www.w3.org/1998/Math/MathML";
$html_xmlns = "http://www.w3.org/TR/REC-html40";

## Input parameters

for ($i=0;$i<=@ARGV;$i++)  {
  $document_table = 1		if ($ARGV[$i] eq "-t");
  $output = $ARGV[$i+1]		if ($ARGV[$i] eq "-o");
  $input = $ARGV[$i+1]		if ($ARGV[$i] eq "-i");
  if ($ARGV[$i] eq "-s" && (($ARGV[$i+1] =~ /-\w\Z/) || !$ARGV[$i+1]))  {
    $extra_css = "none";
  }  elsif ($ARGV[$i] eq "-s" && $ARGV[$i+1] && $ARGV[$i+1] !~ /-\w\Z/)  {
    $extra_css = $ARGV[$i+1];
  }
  $help = 1  if (@ARGV == 1 || !@ARGV || $ARGV[$i] eq "-h");
}

$output = $input  if (!$output);

if ($extra_css eq "none")  {
  $extra_css = $output;
  $extra_css =~ s/\.\w*\b/\.css/;  
}

## Print quick syntax help

if ($help == 1)  {
  print "\nThis program automates some of the HTML -> XHTML/MathML conversion process.\n\n";
  print "Syntax:\n\n";
  print "perl convert.pl -i input.html [-o output.xml -s (stylesheet.css) -t]\n\n";
  print "-i:\tspecifies input file\n";
  print "-o:\tspecifies output file\n";
  print "-s:\tspecifies extra stylesheet file in output\n";
  print "\tif no file is specified but -s is used, output.css is used\n";
  print "-t:\tspecifies document-like tabular format\n";
  print "-h:\tthis help text\n";
  print "\nOptions in brackets and parenthesis are optional.";
  die "\n";
}

## Print input parameters

print("Input:\t\t" . $input . "\n");
print("Output:\t\t" . $output . "\n");
print("Stylesheet:\t" . $extra_css . "\n")  unless (!$extra_css);
print("Table:\t\t" . $document_table . "\n")  unless (!$document_table);

## 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);

unless (open(OUTPUT, ">" . $output))  {
  die "\nCan't open output file: $output for writing\n\n";
}

## Print header information

$extra_count = 3;
print OUTPUT "<?xml version=\"1.0\"?>\n";
print OUTPUT "<?xml-stylesheet type=\"text/css\" href=\"mathml.css\"?>\n";
if ($extra_css)  {
  print OUTPUT "<?xml-stylesheet type=\"text/css\" href=\"" . $extra_css . "\"?>\n";
  $extra_count = 4;
}
print OUTPUT "<!DOCTYPE html SYSTEM \"mathml.dtd\" [ <!ENTITY mathns \"http://www.w3.org/1998/Math/MathML\"> ]>\n";

## Search/Replace/Convert routine

$p = 0;
$html = 0;

for ($i=0;$i<=@input_data;$i++)  {
  $meta = 0;
  $a = $input_data[$i];
  study($a);
  $a =~ s/(<\/?\w*)/\L$1/g;				# Convert all <TAG to <tag
  $a =~ s/(\b\w*=")(#?\w*\.?\w*?")/\L$1\E$2/g;		# Convert all PARAMETER="value" to parameter="value"
  $a =~ s/(\b\w*=)(#?\w*\.?\w*?)/\L$1\E"$2"/g;		# Convert all PARAMETER="value" to parameter="value"
  $a =~ s/"""/"/g;					# Fixes bug in previous two regular expressions
  $a =~ s/<math/<math xmlns="$math_xmlns"/g;		# Add proper xmlns to all <math> tags
  $a =~ s/<html/<html xmlns="$html_xmlns"/g;		# Add proper xmlns to <html> tags
  $a =~ s/<p>/<\/p><p>/g  unless ($p == 0);		# Close all the <p> blocks
  $a =~ s/<br>/<br \/>/g;				# Close all <br> tags
  $a =~ s/\&emsp\;/\&nbsp\;/g;				# Replace spacing character
  $a =~ s/(<img.*)>/$1 \/>/g;				# Close all <img> tags
  $html = 1  if ($a =~ /<html/);			# Have we seen the <html> tag yet?
  $meta = 1  if ($a =~ /<meta/);			# Have we seen the <meta> tag(s) yet?
  if ($a =~ /<p>/)  {					# Count the <p> tags
    $p = $i;
  }
  unless ($html == 0  ||  $meta == 1)  {		# Print output, excluding pre-html stuff and <meta> tag
    @converted_data = (@converted_data, $a);
  }

}

  while ($a =~ /(<[^>]+)/)  {
    @tag_list = (@tag_list,$1.">");
    $a =~ s/<[^>]+//;
  }

$i = 0;
foreach (@converted_data)  {
  $_ =~ s/<\/p><p>/<\/p>/  if ($i == ($p - $extra_count));
  print OUTPUT;
  $i++;
}

close(OUTPUT);

for ($i=0;$i<=@tag_list;$i++)  {
  print("$tag_list[$i]\n");
}

















