$kwd = qr/\b(absolute|and|array|asm|begin|break|case|const|constructor|continue|destructor|div|do|downto|else|end|file|for|function|goto|if|implementation|in|inherited|inline|interface|label|mod|nil|not|object|of|on|operator|or|packed|procedure|program|record|reintroduce|repeat|self|set|shl|shr|string|then|to|type|unit|until|uses|var|while|with|xor)\b/i;
print "Pas2HTML converter 0.0.2 beta
(c) Alexandr Ignatiev, 2007\n";
do{print 'File of Pascal program <cancel>: ';
  chomp ($fname = <STDIN>);
} until ((open INF,($fname or die)) or not print("Can\'t read $fname!\n"));
$fname=~/([^\\\/]*)$/;$fname=$1;
print "File to produce <program.htm>: ";
chomp($fn = <STDIN>);
$fn='program.htm' if $fn eq '';
open OUTF,">$fn";
print OUTF "<html><head>
<meta http-equiv=Content-Type content=\"text/html; charset=cp866\">
<title>$fname</title></head><body><pre>";
while($_ = <INF>){A:{
         if(/\G$kwd/cg){
                      print OUTF '<b>',$1,'</b>'
         }elsif(/\G\'/gc){
	              print OUTF '<font color=navy>\'';
                      STR:while(/\G(.)/gc){
                                if ($1 eq ' '){
                                    print OUTF '&nbsp;'
                                }elsif($1 eq '<'){
                                    print OUTF '&lt;'
                                }elsif($1 eq '>'){
                                    print OUTF '&gt;'
                                }elsif($1 eq '"'){
                                    print OUTF '&quot;'
                                }elsif($1 eq "'"){
                                    print OUTF '\'</font>';last STR
                                }else{print OUTF $1}
                      };
         }elsif(/\G(\#\d+)/gc){
	              print OUTF '<font color=navy>',$1,'</font>'
         }elsif(/\G\b(\d+(?:\.(?!\.)\d*\b)?|\$[a-zA-Z\d]*)/gc){
	              print OUTF '<font color=cyan>',$1,'</font>'
         }elsif(/\G\{/gc){
                       my $a='';
                       if (/\G\$/gc){
                          $a=1;print OUTF '<font color=red><b>{$'}
                       else{print OUTF '<font color=gray><i>{'};
                       NEWCOMM:while(/\G([^\}])/gc){
                                if($1 eq '<'){
                                    print OUTF '&lt;'
                                }elsif($1 eq '>'){
                                    print OUTF '&gt;'
                                }elsif($1 eq '"'){
                                    print OUTF '&quot;'
                                }else{print OUTF $1};
                                if (/\G\}/gc){
                                   print OUTF '}</',$a?'b':'i','></font>';
                                   last NEWCOMM
                                }elsif(/\G\Z/gc){last NEWCOMM
                                                      unless $_=<INF>;
                                                      print OUTF "\n"
                                };
                       };
         }elsif(/\G\(\*/gc){
                       my $a='';
                       if (/\G\$/gc){
                          $a=1;print OUTF '<font color=red><b>(*$'}
                       else{print OUTF '<font color=gray><i>(*'};
                       OLDCOMM:while(/\G(.)/gc){
                                if($1 eq '<'){
                                    print OUTF '&lt;'
                                }elsif($1 eq '>'){
                                    print OUTF '&gt;'
                                }elsif($1 eq '"'){
                                    print OUTF '&quot;'
                                }else{print OUTF $1};
                                if (/\G\*\)/gc){
                                   print OUTF '*)</',$a?'b':'i','></font>';
                                   last OLDCOMM
                                }elsif(/\G\Z/gc){last OLDCOMM
                                                      unless $_=<INF>;
                                                      print OUTF "\n"
                                };
                       }
         }elsif(/\G\Z/gc){print OUTF "\n";last A
         }elsif(/\G(.)/gc){
         	                if($1 eq '<'){
                                    print OUTF '&lt;'
                                }elsif($1 eq '>'){
                                    print OUTF '&gt;'
                                }elsif($1 eq '"'){
                                    print OUTF '&quot;'
                                }else{print OUTF $1}
         };redo A
} };

print OUTF '</tt></body></html>';
close OUTF;
