Subversion Repositories Applications.projet

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 ddelon 1
<?php
2
/*
431 mathias 3
* $Id: hightlighter.class.inc,v 1.1 2005/09/22 14:02:49 ddelon Exp $
2 ddelon 4
*
5
* Souligneur générique pour colorier la syntaxe de langage de programmation
6
*
7
* copyrigth Eric Feldstein 2004 mailto:garfield_fr@tiscali.fr
8
*
9
* Licence : la meme que wikini(voir le fichier LICENCE).
10
* Vous êtes libre d'utiliser et de modifier ce code à condition de laisser le copyright
11
* d'origine. Vous pouvez  bien sur vous ajouter à la liste des auteurs.
12
*
13
* INSTALLATION : copier le fichier dans le repertoire "formatters" de WikiNi
14
* UTILISATION : importer la classe dans le script de coloration
15
* ATTRIBUTS DE LA CLASSE :
16
*   - isCaseSensitiv : booleen - indique si la syntaxe est sensible a la casse
17
*   - comment : array - tableau d'expressions regulieres definissant les commentaires multiligne
18
*				ex : array('({[^$][^}]*})',	//commentaires: { ... }
19
*						'(\(\*[^$](.*)\*\))'		//commentaires: (* ... *)
20
*					);
21
*	- commentLine : array tableau d'expressions regulieres definissant les commentaires monoligne
22
*				ex : array('(//.*\n)'); 		//commentaire //
23
*	- commentStyle : string - style CSS inline a utiliser pour la coloration(utilisé dans une
24
*			balise <SPAN style="..."></SPAN>)
25
*	- directive : array - tableau d'expression reguliere pour definir les directive de
26
*			compilation
27
*	- directiveStyle : string - style CSS inline a utiliser pour la coloration
28
*	- string : array - tableau d'expression reguliere pour definir les chaine de caracteres
29
*	- stringStyle : string - style CSS inline a utiliser pour la coloration
30
*	- number : array - tableau d'expression reguliere pour definir les nombres
31
*	- numberStyle : string - style CSS inline a utiliser pour la coloration
32
*	- keywords : array - tableau asociatif contenant des tableaux de liste de mots avec leur style. Ex :
33
*          $oHightlighter->keywords['Liste1']['words'] = array('liste1mot1','liste1mot2','liste1mot3');
34
*          $oHightlighter->keywords['Liste1']['style'] = 'color: red';
35
*          $oHightlighter->keywords['Liste2']['words'] = array('liste2mot1','liste2mot2');
36
*          $oHightlighter->keywords['Liste2']['style'] = 'color: yellow';
37
*    chaque tableau keywords['...'] DOIT posseder les 2 clé 'words' et 'style'.
38
*	- symboles : array - tableau conteant la liste des symboles
39
*	- symbolesStyle : string - style CSS inline a utiliser pour la coloration
40
*	- identifier : array - tableau d'expression reguliere pour definir les identifiants
41
*	- identStyle : string - style CSS inline a utiliser pour la coloration
42
* METHODE PUBLIQUE DE LA CLASSE :
43
*	- Analyse($text) : $text string Chaine a analyser
44
*		renvoie le texte colorié.
45
*
46
* NOTES IMPORTANTES
47
*  - Les expressions reguliere doivent être entre parenthèse capturante pour etre utilisé
48
* dans une fonction de remplacement. Voir le fichier coloration_delphi.php pour un exemple
49
*  - Lorsque un style est defini à vide, l'expression reguliere n'est pas prise en compte dans
50
* l'analyse.
51
*  - L'option de recherche est msU : multiligne, le . peut être un \n et la recherche
52
* est 'not greedy' qui inverse la tendance à la gourmandise des expressions régulières.
53
*/
54
 
55
class Hightlighter{
56
	//sensibilite majuscule/minuscule
57
	var $isCaseSensitiv = false;
58
	//commentaires
59
	var $comment = array();	//commentaire multiligne
60
	var $commentLine = array();	//commentaire monoligne
61
	var $commentStyle = '';//'color: red';
62
	//directives de compilation
63
	var $directive = array();
64
	var $directiveStyle = '';//'color: green';
65
	//chaine de caracteres
66
	var $string = array();
67
	var $stringStyle = '';
68
	//nombre
69
	var $number = array();
70
	var $numberStyle = '';
71
	//mots clé
72
	var $keywords = array();
73
	//séparateurs
74
	var $symboles = array();
75
	var $symbolesStyle = '';
76
	//identifiant
77
	var $identifier = array();
78
	var $identStyle = '';
79
	//*******************************************************
80
	// Variable privées
81
	//*******************************************************
82
	var $_patOpt = 'msU';		//option de recherche
83
	var $_pattern = '';			//modele complet
84
	var $_commentPattern = '';	//modele des commentaires
85
	var $_directivePattern = '';//modele des directives
86
	var $_numberPattern = '';	//modele des nombres
87
	var $_stringPattern = '';	//modele des chaine de caracteres
88
	var $_keywordPattern = '';	//modele pour le mots cle
89
	var $_symbolesPattern = '';	//modele pour les symbole
90
	var $_separatorPattern = '';//modele pour les sparateurs
91
	var $_identPattern = '';	//modele pour les identifiants
92
	/********************************************************
93
	Methodes de la classe
94
	*********************************************************/
95
	/**
96
	* Renvoie le pattern pour les commentaires
97
	*/
98
	function _getCommentPattern(){
99
		$a = array_merge($this->commentLine,$this->comment);
100
		return implode('|',$a);
101
	}
102
	/**
103
	* Renvoie le pattern pour les directives de compilation
104
	*/
105
	function _getDirectivePattern(){
106
		return implode('|',$this->directive);
107
	}
108
	/**
109
	* Renvoie le pattern pour les chaine de caracteres
110
	*/
111
	function _getStringPattern(){
112
		return implode('|',$this->string);
113
	}
114
	/**
115
	* Renvoie le pattern pour les nombre
116
	*/
117
	function _getNumberPattern(){
118
		return implode('|',$this->number);
119
	}
120
	/**
121
	* Renvoie le pattern pour les mots clé
122
	*/
123
	function _getKeywordPattern(){
124
      $aResult = array();
125
      foreach($this->keywords as $key=>$keyword){
126
         $aResult = array_merge($aResult, $keyword['words']);
127
         $this->keywords[$key]['pattern'] = '\b'.implode('\b|\b',$keyword['words']).'\b';
128
      }
129
		return '\b'.implode('\b|\b',$aResult).'\b';
130
	}
131
	/**
132
	* Renvoie le pattern pour les symboles
133
	*/
134
	function _getSymbolesPattern(){
135
		$a = array();
136
		foreach($this->symboles as $s){
137
			$a[] = preg_quote($s,'`');
138
		}
139
		return implode('|',$a);
140
	}
141
	/**
142
	* Renvoie le pattern pour les identifiants
143
	*/
144
	function _getIdentifierPattern(){
145
		return implode('|',$this->identifier);
146
	}
147
	/**
148
	* Liste des separateur d'apres la liste des symboles
149
	*/
150
	function _getSeparatorPattern(){
151
		$a = array_unique(preg_split('//', implode('',$this->symboles), -1, PREG_SPLIT_NO_EMPTY));
152
		$pattern = '['.preg_quote(implode('',$a),'`').'\s]+';
153
		return $pattern;
154
	}
155
	/**
156
	* Renvoie le modele a utiliser dans l'expression regulière
157
	*
158
	* @return string Modele de l'expression régulière
159
	*/
160
	function _getPattern(){
161
		$this->_separatorPattern = $this->_getSeparatorPattern();
162
		$this->_symbolesPattern = $this->_getSymbolesPattern();
163
		$this->_commentPattern = $this->_getCommentPattern();
164
		$this->_directivePattern = $this->_getDirectivePattern();
165
		$this->_stringPattern = $this->_getStringPattern();
166
		$this->_numberPattern = $this->_getNumberPattern();
167
		$this->_keywordPattern = $this->_getKeywordPattern();
168
		$this->_identPattern = $this->_getIdentifierPattern();
169
		//construction du modele globale en fonction de l'existance d'un style(optimisation)
170
		if($this->commentStyle){ $a[] = $this->_commentPattern; }
171
		if($this->directiveStyle){ $a[] = $this->_directivePattern; }
172
		if($this->stringStyle){ $a[] = $this->_stringPattern; }
173
		if($this->numberStyle){ $a[] = $this->_numberPattern; }
174
      if(count($this->keywords)>0){ $a[] = $this->_keywordPattern; }
175
		if($this->symbolesStyle){ $a[] = $this->_symbolesPattern; }
176
		if($this->identStyle){ $a[] = $this->_identPattern; }
177
		$this->_pattern = implode('|',$a);
178
    	return $this->_pattern;
179
	}
180
	/**
181
	* Fonction de remplacement de chaque élement avec leur style.
182
	*/
183
	function replacecallback($match){
184
		$text = $match[0];
185
		$pcreOpt = $this->_patOpt;
186
		$pcreOpt .= ($this->isCaseSensitiv)?'':'i';
187
		//commentaires
188
      if($this->commentStyle){
189
         if (preg_match('`'.$this->_commentPattern."`$pcreOpt",$text,$m)){
190
            return "<span style=\"$this->commentStyle\">".$match[0].'</span>';
191
         }
192
      }
193
		//directive de compilation
194
      if ($this->directiveStyle){
195
         if (preg_match('`'.$this->_directivePattern."`$pcreOpt",$text,$m)){
196
            return "<span style=\"$this->directiveStyle\">".$match[0].'</span>';
197
         }
198
      }
199
		//chaine de caracteres
200
      if ($this->stringStyle){
201
   		if (preg_match('`'.$this->_stringPattern."`$pcreOpt",$text,$m)){
202
   			return "<span style=\"$this->stringStyle\">".$match[0].'</span>';
203
   		}
204
      }
205
		//nombres
206
      if ($this->numberStyle){
207
   		if (preg_match('`'.$this->_numberPattern."`$pcreOpt",$text,$m)){
208
   			return "<span style=\"$this->numberStyle\">".$match[0].'</span>';
209
   		}
210
      }
211
		//mot clé
212
      if (count($this->keywords)>0){
213
         foreach($this->keywords as $key=>$keywords){
214
            if ($keywords['style']){
215
               if(preg_match('`'.$keywords['pattern']."`$pcreOpt",$text,$m)){
216
                  return "<span style=\"".$keywords['style']."\">".$match[0].'</span>';
217
               }
218
            }
219
         }
220
      }
221
		//symboles
222
      if ($this->symbolesStyle){
223
   		if (preg_match('`'.$this->_symbolesPattern."`$pcreOpt",$text,$m)){
224
   			return "<span style=\"$this->symbolesStyle\">".$match[0].'</span>';
225
   		}
226
      }
227
		//identifiants
228
      if ($this->identStyle){
229
   		if (preg_match('`'.$this->_identPattern."`$pcreOpt",$text,$m)){
230
   			return "<span style=\"$this->identStyle\">".$match[0].'</span>';
231
   		}
232
      }
233
		return $match[0];
234
	}
235
	/**
236
	* renvois le code colorié
237
	*
238
	* @param $text string Texte a analyser
239
	* @return string texte colorié
240
	*/
241
	function Analyse($text){
242
		$pattern = '`'.$this->_getPattern()."`$this->_patOpt";
243
		if (!$this->isCaseSensitiv){
244
			$pattern .= 'i';
245
		}
246
		$text = preg_replace_callback($pattern,array($this,'replacecallback'),$text);
247
		return $text;
248
	}
249
}	//class Hightlighter
250
?>