#!/usr/bin/perl

# Ouverture des fichiers en lecture et écriture
open(IN,"<$ARGV[0]");
open(OUT,">:encoding(utf8)","$ARGV[0].html");

my $nomfichier;
if ($ARGV[0] =~ /^(.+?).pl$/) {
	$nomfichier = $1;
}

print OUT '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="script.css" media="screen" />
<title>Version HTML du script '.$ARGV[0].'</title>
</head><body>
<div id="titre"><h1>Version HTML du script '.$ARGV[0].'</h1>
<h2>Pour télécharger le script : <a href="ressources/scripts/'.$nomfichier.'.zip">cliquez ici</a></h2></div>
<div id="script"><pre>';

my $dump = "";

while (my $ligne = <IN>) {
	$ligne =~ s/&/&amp;/g;
	$ligne =~ s/</&lt;/g;
	$ligne =~ s/>/&gt;/g;
	unless ($ligne =~ /\$\#\w+/) {
		$ligne =~ s/^(.*?)(\#.+)$/$1<i>$2<\/i>/g;
	}
	$ligne =~ s/\b(sub|if|else|elsif|for|foreach|my|while|unless|chomp|open|close|until|print|exit|closedir|opendir|sort|system|exists|readdir|shift|use|push|die|next|split|unshift|join|each|map|keys|not|delete)\b/<span class=\"fonction\">$1<\/span>/g;
	$ligne =~ s/(\$\w+)/<span class=\"var\">$1<\/span>/g;
	$ligne =~ s/(\$\#\w+)/<span class=\"var\">$1<\/span>/g;
	$ligne =~ s/(@\w+)/<span class=\"tableau\">$1<\/span>/g;
	$ligne =~ s/(@)\{/<span class=\"tableau\">$1<\/span>\{/g;
	$ligne =~ s/(@)(<span class=\"var\">)(\$\w+)/<span class=\"tableau\">$1$3/g;
	$ligne =~ s/(%\w+)/<span class=\"hash\">$1<\/span>/g;
	$dump .= $ligne;
}

print OUT $dump;
print OUT '</div></pre></body></html>';

close(IN);
close(OUT);

exit;