#! /usr/bin/perl

# I fscking hate perl

#$open_paragraph = "<paragraph>\n";
#$close_paragraph = "</paragraph>\n\n";
#$open_question = "<question>\n";
#$close_question = "</question>\n\n";
#$open_answer = "<answer>\n";
#$close_answer = "</answer>\n\n";
#$open_note = "<note>\n";
#$close_note = "</note>\n\n";
#$open_name = "<name>";
#$close_name = "</name>";

$open_paragraph = "<p>\n";
$close_paragraph = "</p>\n\n";
$open_question = "<p>\n<b>Q.</b> ";
$close_question = "</p>\n\n";
$open_answer = "<p>\n<b>A.</b> ";
$close_answer = "</p>\n\n";
$open_note = "<h2>\n";
$close_note = "</h2>\n\n";
$open_name = "<b>";
$close_name = "</b>";

while ( <> ) { last if /<[pP][rR][eE]>/; }

print $open_paragraph;
$close = $close_paragraph;

while ( <> )
{
    last if /<\/[pP][rR][eE]>/;
    next if /^ *[0-9]* *$/;
    s/^.......//;

    if (s/^         //)
    {
        print $close;
        if (s/^\((.*)\)/\1/)
        {
            print $open_note;
            $close = $close_note;
            print;
        }
        else
        {
            print $open_paragraph;
            $close = $close_paragraph;
            s/^([A-Z \.]*):/$open_name\1$close_name:/;
            print;
        }
    }
    elsif (/^[^a-z0-9]*$/)
    {
        print $close;
        print $open_note;
        $close = $close_note;
        print;
    }
    elsif (/^A\. /)
    {
        print $close;
        s/^A\. /$open_answer/;
        $close = $close_answer;
        print;
    }
    elsif (/^Q\. /)
    {
        print $close;
        s/^Q\. /$open_question/;
        $close = $close_question;
        print;
    }
    else
    {
        print;
    }

}

print $close;


