use strict;
use FileHandle;
use MParserCore;

package MParser;

sub HilitToH1
{
    my $this = shift;
    my $Row = $this->{ 'row' } || $this->{ 'fh' }->getline();

    while( $Row )
    {
        chomp $Row;
        $Row =~ /^\s*\%/ && do {
            print "<span class=h1>";
            PrintOther( $Row );
            print "</span><br>\n";
            last;
        };

        $this->HilitCodeRow( $Row );
        print "<br>\n";
        $Row = $this->{ 'fh' }->getline();
    }

    $this->{ 'row' } = '';
}

sub HilitHelp1
{
    my $this = shift;
    my $Row = $this->{ 'row' } || $this->{ 'fh' }->getline();
    my $First = 1;
    my ($Fld, $Ot );
    my $Some = 0;
    my $Acc = '';

    while( $Row )
    {
        chomp $Row;

        if( $Row =~ /^\s*$/ ) { 
            $First = 1;
            $Acc .= "\n";
            $Row = $this->{ 'fh' }->getline();
            next;
        } elsif( $Row =~/^(\s*\%\s*)(Last change:)/ ) {
            $Fld = $2;
            $Row = $';
            $Ot = $1;
            $Acc and do { $Acc =~ s/\n/<br>\n/g; print $Acc; $Acc = ''; };
            print "<span class=help1>";
            PrintOther( $Ot );
            print "<span class=help1_field>$Fld</span>";
            PrintOther( $Row );
            print "</span>";
            $First = 0;
            $Some = 1;
        }
        elsif( $Row =~/^(\s*\%\s*)(\w+:)/ )
        {
            $Fld = $2;
            $Row = $';
            $Ot = $1;
            $Acc and do { $Acc =~ s/\n/<br>\n/g; print $Acc; $Acc = ''; };
            print "<span class=help1>";
            PrintOther( $Ot );
            print "<span class=help1_field>$Fld</span>";
            PrintOther( $Row );
            print "</span>";
            $First = 0;
            $Some = 1;
        }
        elsif( $Row =~ /^(\s*\%\s*)(\(c\))/ && $First ) 
        {
            $Fld = $2;
            $Row = $';
            $Ot = $1;
            $Acc and do { $Acc =~ s/\n/<br>\n/g; print $Acc; $Acc = ''; };
            print "<span class=help1>";
            PrintOther( $Ot );
            print "<span class=help1_field>$Fld</span>";
            PrintOther( $Row );
            print "</span>";
            $First = 0;
            $Some = 1;
        }
        elsif( $Row =~/^\s*\%/ && !$First ) 
        {
            print "<span class=help1>";
            PrintOther( $Row );
            print "</span>";
            $Some = 1;
        }
        else { last; }
        print "<br>\n";
        $Row = $this->{ 'fh' }->getline();
    }

    $this->{ 'row' } = $Acc . $Row;
    return $Some;
}

sub HilitToEnd
{
    my $this = shift;
    my $Row = $this->{ 'row' } || $this->{ 'fh' }->getline();

    $Row =~ /^[ \t]*\n/ and $Row = $';

    while( $Row =~ /^[ \t]*\n/ )
    {
        $Row = $';
        print "<br>\n";
    }

    while( defined( $Row ) )
    {
        chomp $Row;

        $this->HilitCodeRow( $Row );
        print "<br>\n";
        $Row = $this->{ 'fh' }->getline();
    }
}

sub HilitCodeRow
{
    my $this = shift;
    my $Row = shift;
    

    while( $Row )
    {
        if( $Row =~ /^\%/ )
        {
            print "<span class=comment>";
            PrintOther( $Row );
            print "</span>";
            last;
        }

        if( $Row =~ /^(\s*)(function)(\s+)/ )   # function line
        {
            my $tmp = $3;
            $Row = $';
            PrintOther( $1 );
            print "<span class=$this->{ 'classes'}{ 'function' }>function" .
                "</span>";
            PrintOther( $tmp );

            if( $Row =~ /^([^=]+)=/ )
            {
                $Row = $';
                print "<span class=$this->{ 'classes'}{ '_outputname' }>";
                PrintOther( $1 );
                print "</span>=";
            }
            if( $Row =~ /^(\s*)(\w+)/ )
            {
                $Row = $';
                $tmp = $2;
                PrintOther( $1 );
                print "<span class=$this->{ 'classes'}{ '_funname' }>";
                print "$tmp</span>";
            }
            if( $Row =~ /^(\s*)\(([^\)]+)\)/ )
            {
                $Row = $';
                $tmp = $2;
                PrintOther( $1 );
                print "(<span class=$this->{ 'classes'}{ '_inputname' }>";
                PrintOther( $tmp );
                print "</span>)";
            }
        }
        elsif( $Row =~ /^\'[^\']*\'/ )
        {
            $Row = $';
            print "<span class=quotes>";
            PrintOther( $& );
            print "</span>";
        }
        elsif( $Row =~ /^\w+/ )
        {
            $Row = $';
            if( defined( $this->{ 'classes'}{ $& } ) )
            {
                print "<span class=$this->{ 'classes'}{ $& }>$&</span>";
            }
            else
            { 
                print $&;
            }
        }
        else
        {
            $Row =~ /^((\')|([^\'\w\%]+))/;
            $Row = $';
            PrintOther( $& );
        }
    }
}



1;
