#!/usr/bin/perl

require HTTP::Request;
require LWP::UserAgent;
require "/local/lib/helm/configuration.pl";
use CGI;

$url = $ENV{"REQUEST_URI"};
$url =~ s/.*mmlurl=(.*)/$1/;

$content = "";

$ua = LWP::UserAgent->new;
$request = HTTP::Request->new(GET => "$url");
$response = $ua->request($request, \&callback);

$content =~ s/\r//sg;
$content =~ s/\n/ /sg;
$content =~ s/<!DOCTYPE[^>]*>//sg;
$content =~ s/<\?xml[^?]*\?>//sg;
$content =~ s/"/'/g;
$content =~ s/&/&amp;/g;
$content =~ s/<m:/</g;
$content =~ s/<\/m:/<\//g;
$content =~ s/<math[^>]*>/<math>/sg;
$content =~ s/(<\/?semantics[^>]*>)/<!-- $1 -->/sg;
$content =~ s/(<annotation-xml)/<!-- $1/sg;
$content =~ s/(<\/annotation-xml>)/$1 -->/sg;
$content =~ s/<mo helm:xref='[^']*' xlink:href='([^']*)'>([^<]*)<\/mo>/<maction actiontype='href' other='$use_webeqp_url?uri=$1'><mo>$2<\/mo><\/maction>/sg;
$content =~ s/<mi helm:xref='[^']*' xlink:href='([^']*)'>([^<]*)<\/mi>/<maction actiontype='href' other='$use_webeqp_url?uri=$1'><mi>$2<\/mi><\/maction>/sg;
$content =~ s/helm:xref='[^']*'//sg;
$content =~ s/xmlns:helm='[^']*'//sg;
$content =~ s/xlink:href='[^']*'//sg;
$content =~ s/xmlns:xlink='[^']*'//sg;
$content =~ s/<mchar name='([^']*)' \/>/&$1;/sg;

print <<EOT;
Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Generato in automatico a partire da $url</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<applet codebase="http://www.webeq.com/WebEQ/2.2/classes" code="webeq.Main"
+width=600 height=2000 align=middle>
<param name=eq value="$content">
<param name=color value="#cccccc">
<param name=parser value="mathml">
<param name=size value="14">
</applet>
</body>
</html>
EOT

#=========================

sub callback
{ 
 my ($data) = @_;
 $content .= $data;
}
