[PLUGINS] ~maj des plugins
[lhc/web/www.git] / www / plugins / enluminures_typographiques_v3 / typoenluminee.php
1 <?php
2 if (!defined("_ECRIRE_INC_VERSION")) return;
3
4 /*
5 * Ce plugin rajoute des raccourcis typographique et ameliore les possibilites de la barre typographique pour les redacteurs
6 */
7
8 /*
9 * Fonctions de ces filtres :
10 * Ils rajoutent quelques racourcis typo a SPIP
11 *
12 * Syntaxe des raccourcis :
13 * [/texte/] : aligner le texte a droite
14 * [!texte!] : aligner le texte a gauche
15 * [|texte|] : centrer le texte
16 * [(texte)] : encadrer le texte (occupe toute la largeur de la page, a mettre autour d'un paragraphe)
17 * [*texte*] : encadrer/surligner le texte (une partie a l'interieur d'un paragraphe)
18 * [**texte*] : variante encadrer/surligner le texte (une partie a l'interieur d'un paragraphe)
19 * <sup>texte</sup> : mettre en exposant le texte selectionne
20 * <sub>texte</sub> : mettre en indice le texte selectionne
21 *
22 * Styles pour les encadrements a rajouter dans votre feuille de style :
23 * .texteencadre-spip {
24 * background: #FFE;
25 * border-bottom: 2px solid #999999;
26 * border-left: 1px solid #EEEEEE;
27 * border-right: 2px solid #999999;
28 * border-top: 1px solid #EEEEEE;
29 * padding: .25em;
30 * }
31 * .caractencadre-spip {
32 * border: 1px solid #666;
33 * padding: 0px .5em 0px .5em;
34 * }
35 *
36 */
37
38 // Gerer les variables de personnalisation, fonction depreciee sous SPIP 2.0
39 // A suivre sur la methode...
40 if(!function_exists('tester_variable')) {
41 function tester_variable($var, $val){
42 if (!isset($GLOBALS[$var])) $GLOBALS[$var] = $val;
43 }
44 }
45
46 function typoenluminee_pre_propre($texte) {
47 if(!$texte) return $texte;
48
49 static $chercher_raccourcis=NULL;
50 static $remplacer_raccourcis=NULL;
51
52 if ($chercher_raccourcis===NULL) {
53
54 // tous les elements block doivent etre introduits ici
55 // pour etre pris en charge par paragrapher
56
57 // Definition des differents intertitres possibles, si pas deja definies
58 if ((!function_exists('lire_config')) OR (isset($GLOBALS['config_intertitre']))) {
59 tester_variable('debut_intertitre', '<h3 class="spip">');
60 tester_variable('fin_intertitre', '</h3>');
61 tester_variable('debut_intertitre_2', '<h4 class="spip">');
62 tester_variable('fin_intertitre_2', '</h4>');
63 tester_variable('debut_intertitre_3', '<h5 class="spip">');
64 tester_variable('fin_intertitre_3', '</h5>');
65 tester_variable('debut_intertitre_4', '<h6 class="spip">');
66 tester_variable('fin_intertitre_4', '</h6>');
67 tester_variable('debut_intertitre_5', '<strong class="spip titraille5">');
68 tester_variable('fin_intertitre_5', '</strong>');
69 } else {
70 $GLOBALS['debut_intertitre'] = lire_config('bte/titraille1open','<h3 class="spip">');
71 $GLOBALS['fin_intertitre'] = lire_config('bte/titraille1close','</h3>');
72 $GLOBALS['debut_intertitre_2'] = lire_config('bte/titraille2open','<h4 class="spip">');
73 $GLOBALS['fin_intertitre_2'] = lire_config('bte/titraille2close','</h4>');
74 $GLOBALS['debut_intertitre_3'] = lire_config('bte/titraille3open','<h5 class="spip">');
75 $GLOBALS['fin_intertitre_3'] = lire_config('bte/titraille3close','</h5>');
76 $GLOBALS['debut_intertitre_4'] = lire_config('bte/titraille4open','<h6 class="spip">');
77 $GLOBALS['fin_intertitre_4'] = lire_config('bte/titraille4close','</h6>');
78 $GLOBALS['debut_intertitre_5'] = lire_config('bte/titraille5open','<strong class="spip titraille5">');
79 $GLOBALS['fin_intertitre_5'] = lire_config('bte/titraille5close','</strong>');
80 }
81
82 tester_variable('toujours_paragrapher', false);
83
84 global $debut_intertitre, $fin_intertitre;
85 global $debut_intertitre_2, $fin_intertitre_2;
86 global $debut_intertitre_3, $fin_intertitre_3;
87 global $debut_intertitre_4, $fin_intertitre_4;
88 global $debut_intertitre_5, $fin_intertitre_5;
89
90 $chercher_raccourcis=array();
91 $remplacer_raccourcis=array();
92
93 /* 9b */ $chercher_raccourcis[]="/(^|[^{])[{][{][{]\*\*\*\*\*(.*)[}][}][}]($|[^}])/SUms";
94 /* 9 */ $chercher_raccourcis[]="/(^|[^{])[{][{][{]\*\*\*\*(.*)[}][}][}]($|[^}])/SUms";
95 /* 7 */ $chercher_raccourcis[]="/(^|[^{])[{][{][{]\*\*\*(.*)[}][}][}]($|[^}])/SUms";
96 /* 5 */ $chercher_raccourcis[]="/(^|[^{])[{][{][{]\*\*(.*)[}][}][}]($|[^}])/SUms";
97 /* 3 */ $chercher_raccourcis[]="/(^|[^{])[{][{][{]\*(.*)[}][}][}]($|[^}])/SUms";
98 /* 1 */ $chercher_raccourcis[]="/(^|[^{])[{][{][{](.*)[}][}][}]($|[^}])/SUms";
99 /* 11 */ $chercher_raccourcis[]="/\{(�|§)\{/S"; # § Pour gerer l'unicode aussi !
100 /* 12 */ $chercher_raccourcis[]="/\}(�|§)\}/S"; # ne pas sauvergarder ce fichier en utf8 !
101
102 /* 9b */ $remplacer_raccourcis[]="\$1\n\n$debut_intertitre_5\$2$fin_intertitre_5\n\n\$3";
103 /* 9 */ $remplacer_raccourcis[]="\$1\n\n$debut_intertitre_4\$2$fin_intertitre_4\n\n\$3";
104 /* 7 */ $remplacer_raccourcis[]="\$1\n\n$debut_intertitre_3\$2$fin_intertitre_3\n\n\$3";
105 /* 5 */ $remplacer_raccourcis[]="\$1\n\n$debut_intertitre_2\$2$fin_intertitre_2\n\n\$3";
106 /* 3 */ $remplacer_raccourcis[]="\$1\n\n$debut_intertitre\$2$fin_intertitre\n\n\$3";
107 /* 1 */ $remplacer_raccourcis[]="\$1\n\n$debut_intertitre\$2$fin_intertitre\n\n\$3";
108 /* 11 */ $remplacer_raccourcis[]="<sc>";
109 /* 12 */ $remplacer_raccourcis[]="</sc>";
110 }
111
112 // Conversion des intertitres d'enluminures type {n{titre}n}
113 // ou n est un nombre en intertitres avec des etoiles type {{{* (avec n etoiles)
114 // {1{ sera converti en {{{* ; {2{ sera converti en {{{** ; etc.
115 // Ne faire la recherche que s'il y a au moins un titre ancienne mode a convertir
116 if (strpos($texte, '{1{')!==false
117 OR strpos($texte, '{2{')!==false
118 OR strpos($texte, '{3{')!==false
119 OR strpos($texte, '{4{')!==false
120 OR strpos($texte, '{5{')!==false) {
121 $texte=preg_replace_callback ("/\{(\d)\{(.*)\}(\\1)\}/Ums",
122 create_function (
123 '$matches',
124 'return "{{{".str_repeat("*",$matches[1]).trim($matches[2])."}}}";'
125 ),
126 $texte);
127 }
128 $texte = preg_replace($chercher_raccourcis, $remplacer_raccourcis, $texte);
129
130 return $texte;
131 }
132
133 function typoenluminee_post_propre($texte) {
134 if(!$texte) return $texte;
135 static $cherche1 = NULL;
136 static $remplace1 = NULL;
137 if ($cherche1===NULL) {
138 # Le remplacement des intertitres de premier niveau a deja ete effectue dans inc/texte.php
139 $cherche1 = array();
140 $remplace1 = array();
141 $cherche1[] = /* 15 */ ",\[/(.*)/\],Ums";
142 $cherche1[] = /* 16 */ ",\[!(.*)!\],Ums";
143 $cherche1[] = /* 17 */ ",\[\|(.*)\|\],Ums";
144 $cherche1[] = /* 19 */ ",\[\((.*)\)\],Ums";
145 $cherche1[] = /* 21 */ "/\[\*\*/S";
146 $cherche1[] = /* 21b */ "/\[\*/S";
147 $cherche1[] = /* 22 */ "/\*\]/S";
148
149 $remplace1[] = /* 15 */ "<div class=\"spip\" style=\"text-align:right;\">$1</div>";
150 $remplace1[] = /* 16 */ "<div class=\"spip\" style=\"text-align:left;\">$1</div>";
151 $remplace1[] = /* 17 */ "<div class=\"spip\" style=\"text-align:center;\">$1</div>";
152 $remplace1[] = /* 19 */ "<div class=\"texteencadre-spip spip\">$1</div>";
153 $remplace1[] = /* 21 */ "<strong class=\"caractencadre2-spip spip\">";
154 $remplace1[] = /* 21b */ "<strong class=\"caractencadre-spip spip\">";
155 $remplace1[] = /* 22 */ "</strong>";
156 }
157 $texte = preg_replace($cherche1, $remplace1, $texte);
158 $texte = paragrapher($texte,$GLOBALS['toujours_paragrapher']); // il faut reparagrapher a cause des raccourcis typo que l'on a ajoute (block div)
159 return $texte;
160 }
161
162 function typoenluminee_pre_liens($texte) {
163 if (!isset($GLOBALS['barre_typo_pas_de_fork_typo']) OR $GLOBALS['barre_typo_pas_de_fork_typo'] === true)
164 return $texte;
165
166 $texte = str_replace('<-->','&harr;',$texte);
167 $texte = str_replace('-->','&rarr;',$texte);
168
169 return $texte;
170 }
171
172 function typoenluminee_pre_typo($texte) {
173 if(!$texte) return $texte;
174 static $local_barre_typo_pas_de_fausses_puces = null;
175 static $chercher_raccourcis;
176 static $remplacer_raccourcis;
177 global $debut_italique, $fin_italique;
178 if (!isset($GLOBALS['barre_typo_pas_de_fork_typo']) OR $GLOBALS['barre_typo_pas_de_fork_typo'] === true)
179 return $texte;
180
181 if ($local_barre_typo_pas_de_fausses_puces===null){
182 // remplace les fausses listes a puce par de vraies ?
183 // (recherche en debut de lignes - suivi d'un ou plusieurs caracteres blancs, en mode multiligne)
184 // Mettre $GLOBALS['barre_typo_pas_de_fausses_puces'] = true; dans mes_options.php pour avoir ce comportement
185 if (isset($GLOBALS['barre_typo_pas_de_fausses_puces'])) {
186 $local_barre_typo_pas_de_fausses_puces = $GLOBALS['barre_typo_pas_de_fausses_puces'];
187 } else {
188 if (function_exists('lire_config')) {
189 $local_barre_typo_pas_de_fausses_puces = (lire_config('bte/puces','Non') == 'Oui')?true:false;
190 }
191 }
192 global $class_spip;
193 tester_variable('debut_italique', "<i$class_spip>");
194 tester_variable('fin_italique', '</i>');
195
196 $chercher_raccourcis = array(
197 /* 9 */ "/(?<![{\d])[{](?![{\d])/S", // Expressions complexes car on n'a pas encore traite les titres ici
198 /* 10 */ "/(?<![}\d])[}](?![}\d])/S", // puisque italique utilisent les memes caracteres en nombre inferieur
199 );
200
201 $remplacer_raccourcis = array(
202 /* 9 */ $debut_italique,
203 /* 10 */ $fin_italique,
204 );
205 }
206 if ($local_barre_typo_pas_de_fausses_puces === true) {
207 $texte = preg_replace('/^-\s+/m','-* ',$texte);
208 }
209
210 $texte = str_replace('<--','&larr;',$texte);
211 $texte = str_replace('<==>','&hArr;',$texte);
212 $texte = str_replace('==>','&rArr;',$texte);
213 $texte = str_replace('<==','&lArr;',$texte);
214 $texte = str_ireplace('(c)','&copy;',$texte);
215 $texte = str_ireplace('(r)','&reg;',$texte);
216 $texte = str_ireplace('(tm)','&trade;',$texte);
217 $texte = str_replace('...','&hellip;',$texte);
218 $texte = preg_replace($chercher_raccourcis, $remplacer_raccourcis, $texte);
219 /*
220 Cas particulier pour le gras
221 Il ne faut pas traiter la mise en gras ici si le texte contient un tableau
222 */
223 if (!preg_match(',.(\|([[:space:]]*{{[^}]+}}[[:space:]]*|<))+.,sS', $texte)) {
224 $chercher_raccourcisg = array(
225 /* 7 */ "/(?<![{])[{][{](?![{])/S", // Expressions complexes car on n'a pas encore traite les titres ici
226 /* 8 */ "/(?<![}])[}][}](?![}])/S" // En gros, verification qu'on n'est pas a l'interieur d'un titre
227 );
228 $remplacer_raccourcisg = array(
229 /* 7 */ "<strong class=\"spip\">",
230 /* 8 */ "</strong>"
231 );
232 $texte = preg_replace($chercher_raccourcisg, $remplacer_raccourcisg, $texte);
233 }
234 return $texte;
235 }
236
237 function typoenluminee_post_typo($texte) {
238 if(!$texte) return $texte;
239 if (!isset($GLOBALS['barre_typo_pas_de_fork_typo']) OR $GLOBALS['barre_typo_pas_de_fork_typo'] === true)
240 return $texte;
241 $texte = str_replace('[^','<sup>',$texte);
242 $texte = str_replace('^]','</sup>',$texte);
243 $texte = str_replace('[**','<strong class="caractencadre2-spip spip">',$texte);
244 $texte = str_replace('[*','<strong class="caractencadre-spip spip">',$texte);
245 $texte = str_replace('*]','</strong>',$texte);
246
247 // Correction des & en &amp;
248 $texte = preg_replace('/&([A-Za-z#0-9]*);/','@@@amp:\1:amp@@@',$texte); // echapement des entites html deja presentes
249 $texte = str_replace('&','&amp;',$texte);
250 $texte = preg_replace('/@@@amp:([A-Za-z#0-9]*):amp@@@/','&\1;',$texte);
251 // Raccourci typographique <sc></sc>
252 $texte = str_replace('<sc>', '<span class="caps">', $texte);
253 $texte = str_replace('</sc>', '</span>', $texte);
254 $texte = acronymes_traiter_raccourcis($texte);
255 return $texte;
256 }
257
258 function typoenluminee_nettoyer_raccourcis_typo($texte){
259 $texte = preg_replace(',\{[1-5]\{,','',$texte);
260 $texte = preg_replace(',\}[1-5]\},','',$texte);
261 $texte = preg_replace(',\{\{\{\*+,','{{{',$texte);
262 $texte = str_replace('&hellip;','...',$texte);
263 return $texte;
264 }
265
266 // traite les raccourcis de la forme [SNCF|societe nationale...]
267 // reprise du plugin acronymes
268 if (!function_exists('acronymes_traiter_raccourcis')) {
269 function acronymes_traiter_raccourcis($letexte){
270 $pattern="{\[([^\|\]-]+)\|([^\|\]-]+)\]}";
271 preg_match_all ($pattern, $letexte, $tagMatches, PREG_SET_ORDER);
272 $textMatches = preg_split ($pattern, $letexte);
273
274 $tag_attr=array();
275 foreach ($tagMatches as $key => $value) {
276 $tag_attr[]="<acronym title='".texte_backend($value[2])."'>".$value[1]."</acronym>";
277 }
278 for ($i = 0; $i < count ($textMatches); $i ++) {
279 $textMatches [$i] = $textMatches [$i] . $tag_attr [$i];
280 }
281 return implode ("", $textMatches);
282 }
283 }
284
285
286 ?>