#!/usr/bin/perl -w

use strict;
use FileHandle;

use MParser;

sub List
{
    my ( $Opts, $Dirs, $DirNum, $File ) = @_[0..3];

    $DirNum >= 0 and defined( $Dirs->[ $DirNum * 2 + 1 ] ) or
        do { Head($Opts); print "Wrong \"$DirNum\", possible attack!\n"; exit;};

    $File =~ /^\w+.m$/ or $File =~ /^private\/\w+.m$/ or
        do { Head($Opts); print "Wrong \"$File\", possible attack!\n"; exit; };

    my $fh = new FileHandle;

    $fh->open( $Dirs->[$DirNum * 2 + 1] . $File ) or do { 
        Head($Opts); print "Cannot open \"$Dirs->[$DirNum * 2 + 1]$File\"\n"; 
        exit; };

# keyword table
    my $Classes = { 
                 'if' => 'keyword', 'else' => 'keyword', 'elseif' => 'keyword',
                 'end' => 'keyword', 'while' => 'keyword', 'for' => 'keyword',
                 'switch' => 'keyword',

                 'break' => 'jump', 'return' => 'jump',

                 'case' => 'label', 'otherwise' => 'label',

                 'try' => 'eval', 'catch' => 'eval', 'eval' => 'eval',
                 'evalc' => 'eval', 'feval' => 'eval', 'evalin' => 'eval',
                 'builtin' => 'eval', 'assignin' => 'eval',

                 'uimenu' => 'graph', 'uicontrol' => 'graph', 'plot' => 'graph',
                 'axis' => 'graph', 'axes' => 'graph', 'set' => 'graph',
                 'get' => 'graph', 'line' => 'graph', 'image' => 'graph',
                 'imagesc' => 'graph', 'figure' => 'graph', 'gcf' => 'graph',
                 'gca' => 'graph', 'clf' => 'graph', 'cla' => 'graph',
                 'gco' => 'graph', 'colormap' => 'graph',

                 'uint8' => 'type', 'double' => 'type', 'global' => 'type',
                 'persistent' => 'type', 

                 'warning' => 'error', 'error' => 'error',
                 'lasterr' => 'error', 'lastwarn' => 'error',
                 'errortrap' => 'error',

                 'disp' => 'io', 'display' => 'io',
                 'fprintf' => 'io', 'sprintf' => 'io',

                 'input' => 'io', 'keyboard' => 'io', 'pause' => 'io',

                 'exist' => 'stack', 'isglobal' => 'stack', 
                 'mlock' => 'stack', 'munlock' => 'stack', 
                 'mislocked' => 'stack', 

# def functions                 
                 'function' => 'defun_kw',
                 'nargin' => 'stack', 'nargout' => 'stack', 
                 'varargin' => 'stack', 'varargout' => 'stack',
                 'inputname' => 'stack',
                 '_inputname' => 'defun_in', '_mfilename' => 'defun_name',
                 '_outputname' => 'defun_out',
                 '_funname' => 'defun_name',
                 };

   
    my $Parser = new MParser( $fh, $Classes );

    my $rh = $Opts->{'raw_head'};

 #   print $Opts->{'css'};
    if( defined( $Opts->{'listcss'} ) )
    {
        if( ref( $Opts->{'listcss'} ) ne 'ARRAY' ) 
        { $Opts->{'listcss'} = [ $Opts->{'listcss'} ]; }

        foreach my $css ( @{$Opts->{'listcss'}} )
        {
            $Opts->{'raw_head'} .=
                "<link rel=\"stylesheet\" type=\"text/css\" href=\"$css\">\n";
        }
    }
    else
    {
        $Opts->{'raw_head'} .=
        "  <STYLE TYPE=\"text/css\">\n" .
        "    .comment { color: darkgreen;  }\n" . #font-style: italic
        "    .help    { color: darkgreen; font-weight: bold }\n" .
        "    .help_field  { color: black; font-weight: bold }\n" .
        "    .help1    { color: darkgreen; }\n" .
        "    .help1_field { color: darkgreen; font-weight: bold  }\n" .
        "    .h1      { color: darkgreen; font-weight: bold;\n" .
        "      text-decoration: underline }\n" .
        "    .quotes  { color: gray }\n" .

        "    .keyword  { color: blue; font-weight: bold }\n" .
        "    .jump   { color: yellow; font-weight: bold }\n" .
        "    .label  { color: darkgreen; font-weight: bold }\n" .
        "    .eval   { color: orange; font-weight: bold }\n" .
        "    .graph  { color: darkred;  }\n" .
        "    .type   { color: blue; }\n" .
        "    .error  { color: red }\n" .
        "    .debug  { color: red }\n" .
        "    .io     { color: black; font-weight: bold }\n" .


        "    .defun_kw  { color: brown; font-weight: bold }\n" .
        "    .defun_out  { color: blue;  }\n" .
        "    .defun_name  { color: brown; text-decoration: underline }\n" .
        "    .defun_in   { color: blue; }\n" .
        "    .stack  { color: magenta; }\n" .
        "  </STYLE>\n";
    }

    $Opts->{ 'title' } = $File;    

    Head( $Opts );
    $Opts->{'raw_head'} = $rh;

    print "<body>\n";

    print "<code>\n";
    $Parser->HilitToH1();
    $Parser->HilitHelp();
    print "</code>\n";
    print "<hr>\n";
    print "<code>\n";
    $Parser->HilitHelp1() and print "</code>\n<hr>\n<code>\n";
    $Parser->HilitToEnd;
    print "</code>\n";
}

1;











