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