Wikini

FiZ

PagePrincipale :: DerniersChangements :: DerniersCommentaires :: ParametresUtilisateur :: Vous êtes ec2-44-197-116-176.compute-1.amazonaws.com
Les traces de FiZ : Pages ayant un lien vers la page courante :
BenoitAudouard
ColorationSyntaxiqueJava
FiZ



Discussions


J'ai qqs petites améliorations à proposer avec les premiers retours utilisateur. Je vais les lister ici avant de pouvoir trouver la bonne page pour en faire la demande. Je ne connais pas php, mais je suis prêt à donner un coup de main dans le cadre de mes compétences (aide aux spécifications/uses case, recette, ...) -- FiZ

[J'ai un peu remanié votre page pour une meilleure lisibilité. N'hésitez pas à signer vos contributions -- CharlesNepote


Voici donc en vrac.

Rappel de la syntaxe WikiNi dans un bloc caché lors de l'édition


Export PDF/sxw

Coloriser le code Java/C++

Pour Java, je propose le fichier coloration_java.php ci-dessous. Il est honteusement et largement repris du fichier Delphi. Il colorise par défaut comme l'IDE eclipse. Il faut le placer dans wikini/formatters. Vous pouvez le récupérer directement ici: http://fzara.ml.free.fr/coloration_java.php.txt
<?php
/**
* Souligneur syntaxique Java
******************************
* D'apres le code originale de FEREY Damien et Dark Skull Software
* publié sur http://www.phpcs.com/article.aspx?Val=649
* Modifié par Eric Feldstein (mise sous forme de classe et adapté à WikiNi)
* Adapté pour Java par Florent Zara
******************************
* Peut facilement être adapté pour d'autres langages (vb, c, c++...)
* Il suffit de modifier le contenu des variables
*
* @version 1.0
* @copyright FEREY Damien 23/06/2003 
* @copyright Dark Skull Software
*          http://www.darkskull.net
**/


class JavaHightlighter{

 var 
$code ''//le code a analyser
 
var $newcode ''//le code genere
 
var $tok;            // Le mot en train d'être découpé
 
var $char;        // Le caractère en cours
 
var $i;          // La position en cours dans le code
 
var $codelength;  // La longueur de la chaine de code
 /****************************************************************/
 /* Les variables qui définissent le comportement de l'analyseur */
 /****************************************************************/
 
var $case_sensitive TRUE;                   // Langage sensible à la case ou pas
 
var $tokdelimiters " {}[]()=+-/*:;,.\n\t\r  "// Les délimiteurs de mots

 /***************************************************/
 /* Les couleurs associées à chaque type de données */
 /***************************************************/
 
var $colorkeyword "#7F0055";
 var 
$colortext "";
 var 
$colorstring   "#2A00FF";
 var 
$colorcomment "#3F7F5F";
 var 
$colorsymbol   "";
 var 
$colornumber   "#000080";
 var 
$colorjavadoc "#3F5FBF";

 
/*************************************************/
 /* Les styles donnés pour chaque type de données */
 /*************************************************/
 
var $stylekeyword = array("<b>""</b>");
 var 
$styletext = array("""");
 var 
$stylestring   = array("""");
 var 
$stylecomment = array("<i>""</i>");
 var 
$stylesymbol   = array("""");
 var 
$stylenumber   = array("""");
 var 
$stylejavadoc = array("""");

 
/*****************/
 /* Les mots clés */
 /*****************/
 
var $keywords = array(
    
'abstract','boolean','break','byte','case','catch','char','class','const',
    
'continue','default','do','double','else','extends','final','finally','float',
    
'for','goto','if','implements','import','instanceof','int','interface','long',
    
'native','new','package','private','protected','public','return','short','static',
    
'strictfp','super','switch','synchronized','this','throw','throws','transient',
    
'try','void','volatile','while');
    
 
/***********************************/
 /* Les délimiteurs de commentaires */
 /***********************************/
 
var $commentdelimiters = array(
   array(
"//""\n"),
   array(
"/*""*/")
 );

 
/********************************************/
 /* Les délimiteurs de chaines de caractères */
 /********************************************/
 
var $stringdelimiters = array(
   array(
"'""'"),
   array(
"\"""\"")
 );

 
/********************************************************/
 /* Les délimiteurs d'instructions pour le préprocesseur */
 /********************************************************/
 
var $javadocdelimiters = array(
   array(
"/**""*/")
 );

/////////////////////////////////////////////////////////////////////////////////////////
// Le code en lui-même
/////////////////////////////////////////////////////////////////////////////////////////

 /************************************************************************/
 /* Renvoie vrai si un caractère est visible et peut être mis en couleur */
 /************************************************************************/
 
function visiblechar($char) {
   
$inviblechars " \t\n\r  ";
   return (!
is_integer(strpos($inviblechars$char)));
 }

 
/************************************************************/
 /* Formatte un mot d'une manière spéciale (couleur + style) */
 /************************************************************/
 
function formatspecialtok($tok$color$style)
 {
   if (empty(
$color)) return sprintf("%s$tok%s"$style[0], $style[1]);
   return 
sprintf("%s<font color=\"%s\">$tok</font>%s"$style[0], $color$style[1]);
 }


 
/*******************************************************************/
 /* Recherche un élément dans un tableau sans se soucier de la case */
 /*******************************************************************/
 
function array_search_case($needle$array)
 {
   if (!
is_array($array)) return FALSE;
   if (empty(
$array)) return FALSE;
   foreach(
$array as $index=>$string)
     if (
strcasecmp($needle$string) == 0) return intval($index);
   return 
FALSE;
 }


 
/*****************************************************/
 /* Analyse un mot et le renvoie de manière formattée */
 /*****************************************************/
 
function analyseword($tok)
 {
   
// Si c'est un nombre
   
if (($tok[0] == '$') || ($tok[0] == '#') || ($tok == (string)intval($tok)))
     return 
$this->formatspecialtok($tok$this->colornumber$this->stylenumber);
   
// Si c'est vide, on renvoie une chaine vide
   
if (empty($tok)) return $tok;
   
// Si c'est un mot clé
   
if ((($this->case_sensitive) && (is_integer(array_search($tok$this->keywordsFALSE)))) ||
      ((!
$this->case_sensitive) && (is_integer($this->array_search_case($tok$this->keywords)))))
      return 
$this->formatspecialtok($tok$this->colorkeyword$this->stylekeyword);
   
// Sinon, on renvoie le mot sans formattage
   
return $this->formatspecialtok($tok$this->colortext$this->styletext);
 }


 
/***************************************************/
 /* On regarde si on ne tombe pas sur un délimiteur */
 /***************************************************/
 
function parsearray($array$color "#000080"$style = array("<i>""</i>"))
 {
   
// On effectue quelques vérifications
   
if (!is_array($array))   return FALSE;
   if (!
strlen($this->code))     return FALSE;
   if (!
sizeof($array))     return FALSE;

   
// On va essayer de comparer le caractère courrant avec le 1°
   // caractère de chaque premier délimiteur
   
foreach($array as $delimiterarray) {
     
$delimiter1 $delimiterarray[0];
     
// Si le 1° char correspond
     
if ($this->char == $delimiter1[0]) {
       
$match TRUE;
       
// On va tenter de comparer tous les autres caractères
       // Pour vérifier qu'on a bien le délimiteur complet
       
for ($j 1; ($j strlen($delimiter1)) && $match$j++) {
         
$match = ($this->code[$this->$j] == $delimiter1[$j]);
       } 
// for
       // Si on l'a en entier
       
if ($match) {
         
$delimiter2 $delimiterarray[1];
         
// Alors on recherche le délimiteur de fin
         
$delimiterend strpos($this->code$delimiter2$this->strlen($delimiter1));
         
// Si on ne trouve pas le délimiteur de fin, on prend tout le fichier
         
if (!is_integer($delimiterend)) $delimiterend strlen($this->code);
         
// Maintenant qu'on a tout, on analyse le mot avant le délimiteur, s'il existe
         
if (!empty($this->tok)) {
           
$this->newcode .= $this->analyseword($this->tok);
           
$this->tok "";
         }
         
// Ensuite, on place le texte contenu entre les délimiteurs
         
$this->newcode .= $this->formatspecialtok(substr($this->code$this->i$delimiterend $this->strlen($delimiter2)), $color$style);
         
// On replace l'indice au bon endroit
         
$this->$delimiterend strlen($delimiter2);
         
// Enfin on récupère le caractère en cours
         
if ($this->$this->codelength$this->char NULL;
         else 
$this->char $this->code[$this->i];
         
// On précise qu'on a trouvé
         
return TRUE;
       } 
//if
     
// if
   
// foreach
   
return FALSE;
 }


 
/******************************/
 /* On traite les cas spéciaux */
 /******************************/
 
function parsearrays()
 {
   
$haschanged TRUE;
   
// A chaque changement, on redemarre la boucle entière
   
while($haschanged){
     
// On regarde si on ne tombe pas sur un délimiteur de commentaire
     
$haschanged $this->parsearray($this->javadocdelimiters$this->colorjavadoc$this->stylejavadoc);
     if (!
$haschanged) {
       
// On regarde si on ne tombe pas sur un délimiteur de commentaire
       
$haschanged $this->parsearray($this->commentdelimiters$this->colorcomment$this->stylecomment);
       if (!
$haschanged) {
         
// Ou de chaine de caractère
         
$haschanged $this->parsearray($this->stringdelimiters$this->colorstring$this->stylestring);
       } 
// if
     
// if
   
// while
 
// parsearrays


 
function dump($var,$name){
//  echo "<pre>$name = \n";
//  print_r($var);
//  echo "</pre><br>";
 
}
 function 
trace($msg){
//  error_log("$msg");
 
}
 
/***************************/
 /* Analyse un code complet */
 /***************************/
 
function analysecode($text)
 {
  
// On initialise les variables
  
$this->newcode "";
  
$this->tok "";
  
$this->char NULL;
  
$this->code $text;
  
$this->codelength strlen($this->code);

  
$this->trace("debut analysecode");
  
$this->dump($this->codelength,"codelength");
  
$this->dump($this->code,"code");
  for (
$this->0$this->$this->codelength$this->i++ ) {
   
$this->dump($this->i,"i");
   
$this->char $this->code[$this->i];
   
$this->dump($this->char,"char");
   
// On regarde si on tombe sur un cas spécial
   
$this->parsearrays();
   
// On regarde si on est arrivé au bout de la chaine
   
if ($this->char == NULL) return $this->newcode;
   
// On a fini d'analyser les commentaires, on regarde si on a un mot complet
   
if (is_integer(strpos($this->tokdelimiters$this->char))) {
    
// On tombe sur un délimiteur, on coupe le mot
    
$this->newcode .= $this->analyseword($this->tok);
    
// On formatte le délimiteur
    
if ($this->visiblechar($this->char)) $this->newcode .= $this->formatspecialtok($this->char$this->colorsymbol$this->stylesymbol);
    else 
$this->newcode .= $this->char;
    
// On remet à 0 le mot en cours
    
$this->tok "";
   }
   else {
// On n'a pas de mot complet, on complete le mot
    
$this->tok .= $this->char;
   }
  } 
// for
  // On regarde si on arrive au bout du code
  
if (!empty($this->tok)) $this->newcode .= $this->analyseword($this->tok);
  return 
$this->newcode;
 }
}

/**********************/
/* On analyse le code */
/**********************/
$JH = new JavaHightlighter();
echo 
"<pre>".$JH->analysecode($text)."</pre>";
unset(
$JH);

?>


Résumé des modifications dans l'historique (comme sur Wikipédia ou encore pour un commit cvs)

[ProgFou a déja fait quelque chose identique dans son Wiki ... -- DavidDelon]

Pour conclure, pas la peine de déplacer une partie de cette discussion dans une autre page ! -- Fiz
Commentaires [Cacher commentaires/formulaire]