[PLUGINS] +crayons et enluminures
[ptitvelo/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, '{2{')!==false
117 OR strpos($texte, '{3{')!==false
118 OR strpos($texte, '{4{')!==false
119 OR strpos($texte, '{5{')!==false) {
120 $texte=preg_replace_callback ("/\{(\d)\{(.*)\}(\\1)\}/Ums",
121 create_function (
122 '$matches',
123 'return "{{{".str_repeat("*",$matches[1]).trim($matches[2])."}}}";'
124 ),
125 $texte);
126 }
127 $texte = preg_replace($chercher_raccourcis, $remplacer_raccourcis, $texte);
128
129 return $texte;
130 }
131
132 function typoenluminee_post_propre($texte) {
133 if(!$texte) return $texte;
134 static $cherche1 = NULL;
135 static $remplace1 = NULL;
136 if ($cherche1===NULL) {
137 # Le remplacement des intertitres de premier niveau a deja ete effectue dans inc/texte.php
138 $cherche1 = array();
139 $remplace1 = array();
140 $cherche1[] = /* 15 */ ",\[/(.*)/\],Ums";
141 $cherche1[] = /* 16 */ ",\[!(.*)!\],Ums";
142 $cherche1[] = /* 17 */ ",\[\|(.*)\|\],Ums";
143 $cherche1[] = /* 19 */ ",\[\((.*)\)\],Ums";
144 $cherche1[] = /* 21 */ "/\[\*\*/S";
145 $cherche1[] = /* 21b */ "/\[\*/S";
146 $cherche1[] = /* 22 */ "/\*\]/S";
147
148 $remplace1[] = /* 15 */ "<div class=\"spip\" style=\"text-align:right;\">$1</div>";
149 $remplace1[] = /* 16 */ "<div class=\"spip\" style=\"text-align:left;\">$1</div>";
150 $remplace1[] = /* 17 */ "<div class=\"spip\" style=\"text-align:center;\">$1</div>";
151 $remplace1[] = /* 19 */ "<div class=\"texteencadre-spip spip\">$1</div>";
152 $remplace1[] = /* 21 */ "<strong class=\"caractencadre2-spip spip\">";
153 $remplace1[] = /* 21b */ "<strong class=\"caractencadre-spip spip\">";
154 $remplace1[] = /* 22 */ "</strong>";
155 }
156 $texte = preg_replace($cherche1, $remplace1, $texte);
157 $texte = paragrapher($texte,$GLOBALS['toujours_paragrapher']); // il faut reparagrapher a cause des raccourcis typo que l'on a ajoute (block div)
158 return $texte;
159 }
160
161
162 function typoenluminee_pre_typo($texte) {
163 if(!$texte) return $texte;
164 static $local_barre_typo_pas_de_fausses_puces = null;
165 static $chercher_raccourcis;
166 static $remplacer_raccourcis;
167 global $debut_italique, $fin_italique;
168 if (!isset($GLOBALS['barre_typo_pas_de_fork_typo']) OR $GLOBALS['barre_typo_pas_de_fork_typo'] === true)
169 return $texte;
170
171 if ($local_barre_typo_pas_de_fausses_puces===null){
172 // remplace les fausses listes a puce par de vraies ?
173 // (recherche en debut de lignes - suivi d'un ou plusieurs caracteres blancs, en mode multiligne)
174 // Mettre $GLOBALS['barre_typo_pas_de_fausses_puces'] = true; dans mes_options.php pour avoir ce comportement
175 if (isset($GLOBALS['barre_typo_pas_de_fausses_puces'])) {
176 $local_barre_typo_pas_de_fausses_puces = $GLOBALS['barre_typo_pas_de_fausses_puces'];
177 } else {
178 if (function_exists('lire_config')) {
179 $local_barre_typo_pas_de_fausses_puces = (lire_config('bte/puces','Non') == 'Oui')?true:false;
180 }
181 }
182 global $class_spip;
183 tester_variable('debut_italique', "<i$class_spip>");
184 tester_variable('fin_italique', '</i>');
185
186 $chercher_raccourcis = array(
187 /* 9 */ "/(?<![{\d])[{](?![{\d])/S", // Expressions complexes car on n'a pas encore traite les titres ici
188 /* 10 */ "/(?<![}\d])[}](?![}\d])/S", // puisque italique utilisent les memes caracteres en nombre inferieur
189 );
190
191 $remplacer_raccourcis = array(
192 /* 9 */ $debut_italique,
193 /* 10 */ $fin_italique,
194 );
195 }
196 if ($local_barre_typo_pas_de_fausses_puces === true) {
197 $texte = preg_replace('/^-\s+/m','-* ',$texte);
198 }
199
200 $texte = str_replace('<-->','&harr;',$texte);
201 $texte = str_replace('-->','&rarr;',$texte);
202 $texte = str_replace('<--','&larr;',$texte);
203 $texte = str_replace('<==>','&hArr;',$texte);
204 $texte = str_replace('==>','&rArr;',$texte);
205 $texte = str_replace('<==','&lArr;',$texte);
206 $texte = str_ireplace('(c)','&copy;',$texte);
207 $texte = str_ireplace('(r)','&reg;',$texte);
208 $texte = str_ireplace('(tm)','&trade;',$texte);
209 $texte = str_replace('...','&hellip;',$texte);
210 $texte = preg_replace($chercher_raccourcis, $remplacer_raccourcis, $texte);
211
212 /*
213 Cas particulier pour le gras
214 Il ne faut pas traiter la mise en gras ici si le texte contient un tableau
215 */
216 if (!preg_match(',.(\|([[:space:]]*{{[^}]+}}[[:space:]]*|<))+.,sS', $texte)) {
217 $chercher_raccourcisg = array(
218 /* 7 */ "/(?<![{])[{][{](?![{])/S", // Expressions complexes car on n'a pas encore traite les titres ici
219 /* 8 */ "/(?<![}])[}][}](?![}])/S" // En gros, verification qu'on n'est pas a l'interieur d'un titre
220 );
221 $remplacer_raccourcisg = array(
222 /* 7 */ "<strong class=\"spip\">",
223 /* 8 */ "</strong>"
224 );
225 $texte = preg_replace($chercher_raccourcisg, $remplacer_raccourcisg, $texte);
226 }
227 return $texte;
228 }
229
230 function typoenluminee_post_typo($texte) {
231 if(!$texte) return $texte;
232 if (!isset($GLOBALS['barre_typo_pas_de_fork_typo']) OR $GLOBALS['barre_typo_pas_de_fork_typo'] === true)
233 return $texte;
234 $texte = str_replace('[^','<sup>',$texte);
235 $texte = str_replace('^]','</sup>',$texte);
236 $texte = str_replace('[**','<strong class="caractencadre2-spip spip">',$texte);
237 $texte = str_replace('[*','<strong class="caractencadre-spip spip">',$texte);
238 $texte = str_replace('*]','</strong>',$texte);
239
240 // Correction des & en &amp;
241 $texte = preg_replace('/&([A-Za-z#0-9]*);/','@@@amp:\1:amp@@@',$texte); // echapement des entites html deja presentes
242 $texte = str_replace('&','&amp;',$texte);
243 $texte = preg_replace('/@@@amp:([A-Za-z#0-9]*):amp@@@/','&\1;',$texte);
244 // Raccourci typographique <sc></sc>
245 $texte = str_replace('<sc>', '<span class="caps">', $texte);
246 $texte = str_replace('</sc>', '</span>', $texte);
247 return $texte;
248 }
249
250 function typoenluminee_nettoyer_raccourcis_typo($texte){
251 $texte = preg_replace(',\{[1-5]\{,','',$texte);
252 $texte = preg_replace(',\}[1-5]\},','',$texte);
253 $texte = preg_replace(',\{\{\{\*+,','{{{',$texte);
254 $texte = str_replace('&hellip;','...',$texte);
255 return $texte;
256 }
257
258 ?>