#!/usr/bin/perl
die "Usage: gri2html file.gri file.html" if ($#ARGV !=1);
$filename_gri = $ARGV[0];
$filename_html = $ARGV[1];
open(IN,  "$filename_gri")   || die "Can't open `$filename_gri'  for input\n";
open(OUT, ">$filename_html") || die "Can't open `$filename_html' for output\n";

print OUT "<html>\n";
print OUT "<head>\n";
print OUT "<title>$ARGV[0]</title>\n";
print OUT "</head>\n";
print OUT "<body bgcolor=\"#FFFFFF\">\n";
print OUT "<i># This html document was prepared by gri2html based on the Gri script named</i>\n";
print OUT "<br><i>#    <a href=\"$ARGV[0]\">$ARGV[0]</a></i>\n";
print OUT "<br># <i>Gri is available for free at <a href=\"http://gri.sourceforge.net\">http://gri.sourceforge.net</a></i>\n";
print OUT "<pre>\n";

while(<IN>) {
    s,<,&lt;,g;
    s,>,&gt;,g;
    s,//(.*),<i>//$1</i>,;
       
    if (/^[ ]*\#/) {
        s,(\#.*),<font color=\#B22222>$1</font>,;
        print OUT "$_";
        next;
    }
    # #Comments
    s,(\#.*),<font color=\#B22222>$1</font>,;

    # \.factor. =
    s,(\\\.[^.]+\. [\+\*/^-]?=),<font color=\#0000EE>$1</font>,;
    # .gulf_emphasisGSL. = 
    s,(\.[^.]+\. [\+\*/^-]?=),<font color=\#0000EE>$1</font>,;
    # \xl = 
    s,(\\[^\\]+ [\+\*/^-]?=),<font color=\#0000EE>$1</font>,;

    # \.factor.
    s,(\\\.[^ .]+\.),<font color=\#CDAD00>$1</font>,g;
    # .factor.
    s,( \.[^ .]+\.),<font color=\#CDAD00>$1</font>,g;
    # ..ysize..
    s,(\.\.[^ .]+\.\.),<font color=\#006400>$1</font>,g;
    # \xl
    s,( \\[^\ ]+),<font color=\#0000EE>$1</font>,g;

    # `newcommand'
    s,^(\`.*\')$,<font color=\#0000EE>$1</font>,;

    # system stuff
       s,system (.*),<font color=\#9400D3>system</font> <font color=\#FF0000>$1</font>,;
    # "strings"
    s,(".*"),<font color=\#2F4F4F>$1</font>,g;

    s,\b(quit|return|if|else( if)?|end (if|while)|break|while|rpn)\b,<font color=\#9400D3>$1</font>,g;

#   s,"(.*)",<u>"$1"</u>,g;
    print OUT "$_";
}
print OUT "</pre>\n";
print OUT "</body>\n";
print OUT "</html>\n";
