#!/usr/bin/perl
###############################################################################
# mkwct.pl
# Chris Cera
###############################################################################
# this is soooo DIRTY, I should be ashamed of myself
###############################################################################
# parses tags in an html document to derive the "wct" format sheet.
# wct format is really just webct's quiz format
###############################################################################
# ONE step closer to XML
# it would be really cool to make a xml dtd or xml schema from this format
###############################################################################
# WISHLIST
#
# support for case insensitive tags
# fix category bug
# real solution to the unsupported tag issue
# change, comment, start and finish, must be on line by itself
# ALL SHOWURL's were simply removed in their entirety!!
###############################################################################

$webct_on=0;
$comment_on=0;

while ($line = <stdin>) {
    if    ( ($pos = index ($line,"<webct") ) > -1) {
        if    ( ($pos = index ($line,"begin") ) > -1) {
            $webct_on=1;
            next;
        }
        elsif ( ($pos = index ($line,"end"  ) ) > -1) {
            $webct_on=0;
#            print "<SCRIPT><!--\n";
#            print "    ContentsBar()\n";
#            print "    //--></SCRIPT>\n\n";

            print ":ANSWER:$aform\n";
            print ":CAT:$qcate\n\n";
            next;
        }
        else {
            print STDERR "weird <webct> tag";
            exit(1);
        }
    }
    elsif ( ($pos = index ($line,"SCRIPT") ) > -1 || 
	        ($pos = index ($line,"script") ) > -1 ) { 
        # avoids catching the comment line
    }
    elsif ( ($pos = index ($line,"<!--") ) > -1) { 
        $comment_on = 1;
        next;
    }
    elsif ( ($pos = index ($line,"-->") ) > -1) { 
        $comment_on = 0;
        next;
    }

    if ($webct_on && $comment_on) {
        # parse tags out of everything 
        $starttag = 4;
        $endtag   = 9;
        $startatt = 10;
        $endatt = index ($line, "<", $startatt);
        $tag = substr ($line, $starttag, $endtag - $starttag);
        $att = substr ($line, $startatt, $endatt - $startatt);

        #print "$tag $att\n";

        if ($tag eq "qnumb") {
            $qnum = $att;
            print ":TYPE:P\n";
            print ":TITLE:$qnum\n";
        }
        if ($tag eq "qform") {
            $qform = $att;
            print ":QUESTION:$qform\n";
        }
        if ($tag eq "qcate") {
            $qcate = $att;
        }
        if ($tag eq "aform") {
            $aform = $att;
        }
    }

    elsif ($webct_on && $comment_on == 0) {
        print $line;
    }
}
