Completed
Push — master ( 0cb1bd...579209 )
by cam
03:28
created
ecrire/src/Texte/Collecteur/Multis.php 1 patch
Indentation   +205 added lines, -205 removed lines patch added patch discarded remove patch
@@ -29,210 +29,210 @@
 block discarded – undo
29 29
  */
30 30
 class Multis extends AbstractCollecteur {
31 31
 
32
-	protected static string $markPrefix = 'MULTI';
33
-
34
-	/**
35
-	 * La preg pour découper et collecter les modèles
36
-	 * @var string
37
-	 */
38
-	protected string $preg_multi;
39
-
40
-	public function __construct(?string $preg = null) {
41
-
42
-		$this->preg_multi = ($preg ?: '@<multi>(.*?)</multi>@sS');
43
-	}
44
-
45
-	/**
46
-	 * Sanitizer une collection d'occurences de multi : on sanitize chaque texte de langue séparemment
47
-	 *
48
-	 * @param array $collection
49
-	 * @param string $sanitize_callback
50
-	 * @return array
51
-	 */
52
-	protected function sanitizer_collection(array $collection, string $sanitize_callback): array {
53
-
54
-		foreach ($collection as &$multi) {
55
-			$changed = false;
56
-			foreach ($multi['trads'] as $lang => $trad) {
57
-				$t = $sanitize_callback($trad);
58
-				if ($t !== $trad) {
59
-					$changed = true;
60
-					$multi['trads'][$lang] = $t;
61
-				}
62
-			}
63
-			if ($changed) {
64
-				$texte = $this->agglomerer_trads($multi['trads']);
65
-				$multi['raw'] = str_replace($multi['texte'], $texte, $multi['raw']);
66
-				$multi['texte'] = $texte;
67
-			}
68
-		}
69
-		return $collection;
70
-	}
71
-
72
-
73
-	/**
74
-	 * Convertit le contenu d'une balise `<multi>` en un tableau
75
-	 *
76
-	 * Exemple de blocs.
77
-	 * - `texte par défaut [fr] en français [en] en anglais`
78
-	 * - `[fr] en français [en] en anglais`
79
-	 *
80
-	 * @param string $bloc
81
-	 *     Le contenu intérieur d'un bloc multi
82
-	 * @return array [code de langue => texte]
83
-	 *     Peut retourner un code de langue vide, lorsqu'un texte par défaut est indiqué.
84
-	 **/
85
-	protected function extraire_trads($bloc) {
86
-		$trads = [];
87
-
88
-		if (strlen($bloc)) {
89
-			$langs = $this->collecteur($bloc, ']', '[', '@[\[]([a-z]{2,3}(_[a-z]{2,3})?(_[a-z]{2,3})?)[\]]@siS');
90
-			$lang = '';
91
-			$pos_prev = 0;
92
-			foreach ($langs as $l) {
93
-				$pos = $l['pos'];
94
-				if ($lang or $pos > $pos_prev) {
95
-					$trads[$lang] = substr($bloc, $pos_prev, $pos - $pos_prev);
96
-				}
97
-				$lang = $l['match'][1];
98
-				$pos_prev = $pos + $l['length'];
99
-			}
100
-			$trads[$lang] = substr($bloc, $pos_prev);
101
-		}
102
-
103
-		return $trads;
104
-	}
105
-
106
-	/**
107
-	 * Recoller ensemble les trads pour reconstituer le texte dans la balise <multi>...</multi>
108
-	 * @param $trads
109
-	 * @return string
110
-	 */
111
-	protected function agglomerer_trads($trads) {
112
-		$texte = '';
113
-		foreach ($trads as $lang => $trad) {
114
-			if ($texte or $lang) {
115
-				$texte .= "[$lang]";
116
-			}
117
-			$texte .= $trad;
118
-		}
119
-		return $texte;
120
-	}
121
-
122
-	/**
123
-	 * @param string $texte
124
-	 * @param array $options
125
-	 *   bool $collecter_liens
126
-	 * @return array
127
-	 */
128
-	public function collecter(string $texte, array $options = []): array {
129
-		if (!$texte) {
130
-			return [];
131
-		}
132
-
133
-		// collecter les matchs de la preg
134
-		$multis = $this->collecteur($texte, '', '<multi', $this->preg_multi, empty($options['detecter_presence']) ? 0 : 1);
135
-
136
-		// si on veut seulement detecter la présence, on peut retourner tel quel
137
-		if (empty($options['detecter_presence'])) {
138
-			foreach ($multis as $k => &$multi) {
139
-				$multi['texte'] = $multi['match'][1];
140
-				// extraire les trads du texte
141
-				$multi['trads'] = $this->extraire_trads($multi['texte']);
142
-			}
143
-		}
144
-
145
-		return $multis;
146
-	}
147
-
148
-	/**
149
-	 * Traiter les multis d'un texte
150
-	 *
151
-	 * @uses approcher_langue()
152
-	 * @uses lang_typo()
153
-	 * @uses code_echappement()
154
-	 * @uses echappe_retour()
155
-	 *
156
-	 * @param string $texte
157
-	 * @param array $options
158
-	 *   ?string $lang
159
-	 *   ?string $lang_defaut
160
-	 *   ?bool echappe_span
161
-	 *   ?bool appliquer_typo
162
-	 * @return string
163
-	 */
164
-	public function traiter(string $texte, array $options) {
165
-		if ($texte) {
166
-
167
-			$multis = $this->collecter($texte);
168
-			if (!empty($multis)) {
169
-				$lang = $options['lang'] ?? $GLOBALS['spip_lang'];
170
-				$lang_defaut = $options['lang_defaut'] ?? _LANGUE_PAR_DEFAUT;
171
-				$echappe_span = $options['echappe_span'] ?? false;
172
-				$appliquer_typo = $options['appliquer_typo'] ?? true;
173
-
174
-				if (!function_exists('approcher_langue')) {
175
-					include_spip('inc/lang');
176
-				}
177
-				if (!function_exists('code_echappement')) {
178
-					include_spip('inc/texte_mini');
179
-				}
180
-
181
-				$offset_pos = 0;
182
-				foreach ($multis as $m) {
183
-
184
-					// chercher la version de la langue courante
185
-					$trads = $m['trads'];
186
-					if (empty($trads)) {
187
-						$trad = '';
188
-					}
189
-					elseif ($l = approcher_langue($trads, $lang)) {
190
-						$trad = $trads[$l];
191
-					} else {
192
-						if ($lang_defaut == 'aucune') {
193
-							$trad = '';
194
-						} else {
195
-							// langue absente, prendre le fr ou une langue précisée (meme comportement que inc/traduire.php)
196
-							// ou la premiere dispo
197
-							if (!$l = approcher_langue($trads, $options['lang_defaut'])) {
198
-								$l = array_keys($trads);
199
-								$l = reset($l);
200
-							}
201
-
202
-							// mais typographier le texte selon les regles de celle-ci
203
-							// Attention aux blocs multi sur plusieurs lignes
204
-							if ($appliquer_typo) {
205
-								$trad = $trads[$l];
206
-								$typographie = charger_fonction(lang_typo($l), 'typographie');
207
-								$trad = $typographie($trad);
208
-
209
-								// Tester si on echappe en span ou en div
210
-								// il ne faut pas echapper en div si propre produit un seul paragraphe
211
-								include_spip('inc/texte');
212
-								$trad_propre = preg_replace(',(^<p[^>]*>|</p>$),Uims', '', propre($trad));
213
-								$mode = preg_match(',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS', $trad_propre) ? 'div' : 'span';
214
-								if ($mode === 'div') {
215
-									$trad = rtrim($trad) . "\n\n";
216
-								}
217
-								$trad = code_echappement($trad, 'multi', false, $mode);
218
-								$trad = str_replace("'", '"', inserer_attribut($trad, 'lang', $l));
219
-								if (lang_dir($l) !== lang_dir($lang)) {
220
-									$trad = str_replace("'", '"', inserer_attribut($trad, 'dir', lang_dir($l)));
221
-								}
222
-								if (!$echappe_span) {
223
-									$trad = echappe_retour($trad, 'multi');
224
-								}
225
-							}
226
-						}
227
-					}
228
-
229
-					$texte = substr_replace($texte, $trad, $m['pos'] + $offset_pos, $m['length']);
230
-					$offset_pos += strlen($trad) - $m['length'];
231
-				}
232
-			}
233
-		}
234
-
235
-		return $texte;
236
-	}
32
+    protected static string $markPrefix = 'MULTI';
33
+
34
+    /**
35
+     * La preg pour découper et collecter les modèles
36
+     * @var string
37
+     */
38
+    protected string $preg_multi;
39
+
40
+    public function __construct(?string $preg = null) {
41
+
42
+        $this->preg_multi = ($preg ?: '@<multi>(.*?)</multi>@sS');
43
+    }
44
+
45
+    /**
46
+     * Sanitizer une collection d'occurences de multi : on sanitize chaque texte de langue séparemment
47
+     *
48
+     * @param array $collection
49
+     * @param string $sanitize_callback
50
+     * @return array
51
+     */
52
+    protected function sanitizer_collection(array $collection, string $sanitize_callback): array {
53
+
54
+        foreach ($collection as &$multi) {
55
+            $changed = false;
56
+            foreach ($multi['trads'] as $lang => $trad) {
57
+                $t = $sanitize_callback($trad);
58
+                if ($t !== $trad) {
59
+                    $changed = true;
60
+                    $multi['trads'][$lang] = $t;
61
+                }
62
+            }
63
+            if ($changed) {
64
+                $texte = $this->agglomerer_trads($multi['trads']);
65
+                $multi['raw'] = str_replace($multi['texte'], $texte, $multi['raw']);
66
+                $multi['texte'] = $texte;
67
+            }
68
+        }
69
+        return $collection;
70
+    }
71
+
72
+
73
+    /**
74
+     * Convertit le contenu d'une balise `<multi>` en un tableau
75
+     *
76
+     * Exemple de blocs.
77
+     * - `texte par défaut [fr] en français [en] en anglais`
78
+     * - `[fr] en français [en] en anglais`
79
+     *
80
+     * @param string $bloc
81
+     *     Le contenu intérieur d'un bloc multi
82
+     * @return array [code de langue => texte]
83
+     *     Peut retourner un code de langue vide, lorsqu'un texte par défaut est indiqué.
84
+     **/
85
+    protected function extraire_trads($bloc) {
86
+        $trads = [];
87
+
88
+        if (strlen($bloc)) {
89
+            $langs = $this->collecteur($bloc, ']', '[', '@[\[]([a-z]{2,3}(_[a-z]{2,3})?(_[a-z]{2,3})?)[\]]@siS');
90
+            $lang = '';
91
+            $pos_prev = 0;
92
+            foreach ($langs as $l) {
93
+                $pos = $l['pos'];
94
+                if ($lang or $pos > $pos_prev) {
95
+                    $trads[$lang] = substr($bloc, $pos_prev, $pos - $pos_prev);
96
+                }
97
+                $lang = $l['match'][1];
98
+                $pos_prev = $pos + $l['length'];
99
+            }
100
+            $trads[$lang] = substr($bloc, $pos_prev);
101
+        }
102
+
103
+        return $trads;
104
+    }
105
+
106
+    /**
107
+     * Recoller ensemble les trads pour reconstituer le texte dans la balise <multi>...</multi>
108
+     * @param $trads
109
+     * @return string
110
+     */
111
+    protected function agglomerer_trads($trads) {
112
+        $texte = '';
113
+        foreach ($trads as $lang => $trad) {
114
+            if ($texte or $lang) {
115
+                $texte .= "[$lang]";
116
+            }
117
+            $texte .= $trad;
118
+        }
119
+        return $texte;
120
+    }
121
+
122
+    /**
123
+     * @param string $texte
124
+     * @param array $options
125
+     *   bool $collecter_liens
126
+     * @return array
127
+     */
128
+    public function collecter(string $texte, array $options = []): array {
129
+        if (!$texte) {
130
+            return [];
131
+        }
132
+
133
+        // collecter les matchs de la preg
134
+        $multis = $this->collecteur($texte, '', '<multi', $this->preg_multi, empty($options['detecter_presence']) ? 0 : 1);
135
+
136
+        // si on veut seulement detecter la présence, on peut retourner tel quel
137
+        if (empty($options['detecter_presence'])) {
138
+            foreach ($multis as $k => &$multi) {
139
+                $multi['texte'] = $multi['match'][1];
140
+                // extraire les trads du texte
141
+                $multi['trads'] = $this->extraire_trads($multi['texte']);
142
+            }
143
+        }
144
+
145
+        return $multis;
146
+    }
147
+
148
+    /**
149
+     * Traiter les multis d'un texte
150
+     *
151
+     * @uses approcher_langue()
152
+     * @uses lang_typo()
153
+     * @uses code_echappement()
154
+     * @uses echappe_retour()
155
+     *
156
+     * @param string $texte
157
+     * @param array $options
158
+     *   ?string $lang
159
+     *   ?string $lang_defaut
160
+     *   ?bool echappe_span
161
+     *   ?bool appliquer_typo
162
+     * @return string
163
+     */
164
+    public function traiter(string $texte, array $options) {
165
+        if ($texte) {
166
+
167
+            $multis = $this->collecter($texte);
168
+            if (!empty($multis)) {
169
+                $lang = $options['lang'] ?? $GLOBALS['spip_lang'];
170
+                $lang_defaut = $options['lang_defaut'] ?? _LANGUE_PAR_DEFAUT;
171
+                $echappe_span = $options['echappe_span'] ?? false;
172
+                $appliquer_typo = $options['appliquer_typo'] ?? true;
173
+
174
+                if (!function_exists('approcher_langue')) {
175
+                    include_spip('inc/lang');
176
+                }
177
+                if (!function_exists('code_echappement')) {
178
+                    include_spip('inc/texte_mini');
179
+                }
180
+
181
+                $offset_pos = 0;
182
+                foreach ($multis as $m) {
183
+
184
+                    // chercher la version de la langue courante
185
+                    $trads = $m['trads'];
186
+                    if (empty($trads)) {
187
+                        $trad = '';
188
+                    }
189
+                    elseif ($l = approcher_langue($trads, $lang)) {
190
+                        $trad = $trads[$l];
191
+                    } else {
192
+                        if ($lang_defaut == 'aucune') {
193
+                            $trad = '';
194
+                        } else {
195
+                            // langue absente, prendre le fr ou une langue précisée (meme comportement que inc/traduire.php)
196
+                            // ou la premiere dispo
197
+                            if (!$l = approcher_langue($trads, $options['lang_defaut'])) {
198
+                                $l = array_keys($trads);
199
+                                $l = reset($l);
200
+                            }
201
+
202
+                            // mais typographier le texte selon les regles de celle-ci
203
+                            // Attention aux blocs multi sur plusieurs lignes
204
+                            if ($appliquer_typo) {
205
+                                $trad = $trads[$l];
206
+                                $typographie = charger_fonction(lang_typo($l), 'typographie');
207
+                                $trad = $typographie($trad);
208
+
209
+                                // Tester si on echappe en span ou en div
210
+                                // il ne faut pas echapper en div si propre produit un seul paragraphe
211
+                                include_spip('inc/texte');
212
+                                $trad_propre = preg_replace(',(^<p[^>]*>|</p>$),Uims', '', propre($trad));
213
+                                $mode = preg_match(',</?(' . _BALISES_BLOCS . ')[>[:space:]],iS', $trad_propre) ? 'div' : 'span';
214
+                                if ($mode === 'div') {
215
+                                    $trad = rtrim($trad) . "\n\n";
216
+                                }
217
+                                $trad = code_echappement($trad, 'multi', false, $mode);
218
+                                $trad = str_replace("'", '"', inserer_attribut($trad, 'lang', $l));
219
+                                if (lang_dir($l) !== lang_dir($lang)) {
220
+                                    $trad = str_replace("'", '"', inserer_attribut($trad, 'dir', lang_dir($l)));
221
+                                }
222
+                                if (!$echappe_span) {
223
+                                    $trad = echappe_retour($trad, 'multi');
224
+                                }
225
+                            }
226
+                        }
227
+                    }
228
+
229
+                    $texte = substr_replace($texte, $trad, $m['pos'] + $offset_pos, $m['length']);
230
+                    $offset_pos += strlen($trad) - $m['length'];
231
+                }
232
+            }
233
+        }
234
+
235
+        return $texte;
236
+    }
237 237
 
238 238
 }
Please login to merge, or discard this patch.