Changeset 58785 in spip-zone
- Timestamp:
- Feb 23, 2012, 8:09:38 AM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
_plugins_/projection/inc/projection.php
r58781 r58785 114 114 ## le texte, c'est l'essentiel mais il ne figure pas dans l'entete 115 115 unset($data['texte']); 116 116 ## le chapo et le PS sont du texte, 117 ## ils peuvent contenir des raccourcis <docX> 118 foreach(explode(' ', 'chapo ps') as $i) 119 if (isset($data[$i])) 120 $data[$i] = projection_texte($data[$i], $data); 121 122 # 123 # Envoyer la representation YAML + content 124 # 117 125 $rep = "##### projection de l'article $obj[id_article]\n" 118 126 . "--- # metadata\n" … … 120 128 . "--- # content\n"; 121 129 122 $rep .= $obj['texte'];130 $rep .= projection_texte($obj['texte'], $data); 123 131 124 132 return $rep; … … 142 150 function projection_doc($doc) { 143 151 $doc = array_filter($doc); 152 $conf['url_de_base'] = 'http://rezo.pagekite.net/spip2.1/'; 153 154 #'<doc http://…………… copy|nocopy> 155 # conf : copy / nocopy / default: copy|nocopy 144 156 145 157 // URL absolue de maniere a pouvoir exporter 146 158 if (isset($doc['fichier']) 147 159 AND !preg_match(',://,', $fichier)) 148 $doc['fichier'] = url_absolue(_DIR_IMG.$doc['fichier'] );160 $doc['fichier'] = url_absolue(_DIR_IMG.$doc['fichier'], $conf['url_de_base']); 149 161 150 162 if ($doc['vu'] == 'non') { … … 169 181 } 170 182 183 # on va nettoyer un peu le texte notamment les liens ! 184 # mais c'est très difficile car dn SPIP tout est fait 185 # pour exporter du HTML 186 ## le code ci-dessous casse pas mal de choses dans le texte 187 function projection_texte($txt, &$data) { 188 include_spip('inc/texte'); 189 $txt = echappe_html($txt, 'P', true); 190 #$txt = expanser_liens($txt /*,$connect */); 191 192 $txt = projection_dereferencer($txt, &$data); 193 194 # $txt = iconv_wordwrap($txt, 80); 195 $txt = echappe_retour($txt, 'P'); 196 return $txt; 197 } 198 199 ## dereferencer les liens et modeles 200 function projection_dereferencer($texte, $data) { 201 $texte = projection_dereferencer_liens($texte, $data); 202 $texte = projection_dereferencer_modeles($texte, $data); 203 return $texte; 204 } 205 206 // cf. http://doc.spip.org/@traiter_modeles 207 function projection_dereferencer_modeles($texte, &$data, $liens=null) { 208 209 // detecter les modeles (rapide) 210 if (strpos($texte,"<")!==false AND 211 preg_match_all('/<[a-z_-]{3,}\s*[0-9|]+/iS', $texte, $matches, PREG_SET_ORDER)) { 212 foreach ($matches as $match) { 213 // Recuperer l'appel complet (y compris un eventuel lien) 214 $a = strpos($texte,$match[0]); 215 preg_match(_RACCOURCI_MODELE_DEBUT, 216 substr($texte, $a), $regs); 217 $regs[]=""; // s'assurer qu'il y a toujours un 5e arg, eventuellement vide 218 list(,$mod, $type, $id, $params, $fin) = $regs; 219 if ($fin AND 220 preg_match('/<a\s[^<>]*>\s*$/i', 221 substr($texte, 0, $a), $r)) { 222 $lien = array( 223 'href' => extraire_attribut($r[0],'href'), 224 'class' => extraire_attribut($r[0],'class'), 225 'mime' => extraire_attribut($r[0],'type') 226 ); 227 $n = strlen($r[0]); 228 $a -= $n; 229 $cherche = $n + strlen($regs[0]); 230 } else { 231 $lien = false; 232 $cherche = strlen($mod); 233 } 234 235 /* 236 // si un tableau de liens a ete passe, reinjecter le contenu d'origine 237 // dans les parametres, plutot que les liens echappes 238 if (!is_null($liens)) 239 $params = str_replace($liens[0], $liens[1], $params); 240 */ 241 242 $modele = projection_dereferencer_modele($regs[0], $type, $id, $params, $lien, $connect); 243 244 /* 245 // en cas d'echec, 246 // si l'objet demande a une url, 247 // creer un petit encadre vers elle 248 if ($modele === false) { 249 if (!$lien) 250 $lien = traiter_lien_implicite("$type$id", '', 'tout', $connect); 251 if ($lien) 252 $modele = '<a href="' 253 .$lien['url'] 254 .'" class="spip_modele' 255 . '">' 256 .sinon($lien['titre'], _T('ecrire:info_sans_titre')) 257 ."</a>"; 258 else { 259 $modele = ""; 260 if (test_espace_prive()) { 261 $modele = entites_html(substr($texte,$a,$cherche)); 262 if (!is_null($liens)) 263 $modele = "<pre>".str_replace($liens[0], $liens[1], $modele)."</pre>"; 264 } 265 } 266 } 267 */ 268 269 // le remplacer dans le texte 270 if ($modele !== false) { 271 #$modele = protege_js_modeles($modele); 272 $rempl = code_echappement($modele, 'P'); 273 $texte = substr($texte, 0, $a) 274 . $rempl 275 . substr($texte, $a+$cherche); 276 } 277 } 278 } 279 280 return $texte; 281 } 282 283 function projection_dereferencer_modele($appel, $type, $id, $params, $lien, $connect) { 284 # $a = func_get_args(); 285 # return var_export($a, true); 286 switch($type) { 287 case 'img': 288 case 'doc': 289 case 'emb': 290 if (is_numeric($id) AND $id>0) { 291 $url = projection_doc($doc = sql_fetsel('fichier', 'spip_documents', 'id_document='.$id)); 292 $appel = preg_replace("/$id/", "| href=".$url." ", $appel, 1); 293 # <media|href=xxxxxx|small> ? 294 } 295 break; 296 } 297 298 return $appel; 299 } 300 301 // cf. http://doc.spip.org/@expanser_liens 302 define('_RACCOURCI_LIEN', "/\[([^][]*?([[]\w*[]][^][]*)*)->(>?)([^]]*)\]/msS"); 303 function projection_dereferencer_liens($texte, &$data) { 304 $sources = $inserts = $regs = array(); 305 if (preg_match_all(_RACCOURCI_LIEN, $texte, $regs, PREG_SET_ORDER)) { 306 $lien = 'projection_lien'; #charger_fonction('lien', 'inc'); 307 foreach ($regs as $k => $reg) { 308 309 $inserts[$k] = '@@SPIP_ECHAPPE_LIEN_' . $k . '@@'; 310 $sources[$k] = $reg[0]; 311 $texte = str_replace($sources[$k], $inserts[$k], $texte); 312 313 list($titre, $bulle, $hlang) = traiter_raccourci_lien_atts($reg[1]); 314 $r = $reg[count($reg)-1]; 315 // la mise en lien automatique est passee par la a tort ! 316 // corrigeons pour eviter d'avoir un <a...> dans un href... 317 if (strncmp($r,'<a',2)==0){ 318 $href = extraire_attribut($r, 'href'); 319 // remplacons dans la source qui peut etre reinjectee dans les arguments 320 // d'un modele 321 $sources[$k] = str_replace($r,$href,$sources[$k]); 322 // et prenons le href comme la vraie url a linker 323 $r = $href; 324 } 325 $regs[$k] = $lien($reg, $r, $titre, '', $bulle, $hlang, '', $connect); 326 } 327 } 328 329 // on passe a traiter_modeles la liste des liens reperes pour lui permettre 330 // de remettre le texte d'origine dans les parametres du modele 331 # $texte = traiter_modeles($texte, false, false, $connect, array($inserts, $sources)); 332 # $texte = corriger_typo($texte); 333 $texte = str_replace($inserts, $regs, $texte); 334 return $texte; 335 } 336 337 function projection_lien($reg, $lien, $texte='', $class='', $title='', $hlang='', $rel='', $connect='') { 338 if ($match = typer_raccourci($lien)) { 339 @list($type,,$id,,$args,,$ancre) = $match; 340 341 // Si une langue est demandee sur un raccourci d'article, chercher 342 // la traduction ; 343 // - [{en}->art2] => traduction anglaise de l'article 2, sinon art 2 344 // - [{}->art2] => traduction en langue courante de l'art 2, sinon art 2 345 if ($hlang 346 AND $type == 'article' 347 AND $id_trad = sql_getfetsel('id_trad', 'spip_articles', "id_article=$id") 348 AND $id_dest = sql_getfetsel('id_article', 'spip_articles', 349 "id_trad=$id_trad AND statut<>'refuse' AND lang=" . sql_quote($hlang)) 350 ) 351 $id = $id_dest; 352 353 # (article, 2) => URL publique de l'article 2 354 $url = generer_url_entite_absolue($id, $type, $args, $ancre, $connect); 355 356 # si le texte est vide aller chercher le titre 357 $lien = calculer_url("$type$id", $texte, 'tout', $connect); 358 359 $titre = strlen($reg[1]) ? $reg[1] : $lien['titre']; 360 361 return "[$titre->$url]"; 362 } 363 364 return $reg[0]; 365 } 366 367 ## optionnellement, wordwrap (ne vaut pas un sentencewrap) 368 ## http://fr2.php.net/manual/fr/function.wordwrap.php#106088 369 /** 370 * Word wrap 371 * 372 * @param string $string 373 * @param integer $width 374 * @param string $break 375 * @param boolean $cut 376 * @param string $charset 377 * @return string 378 */ 379 function iconv_wordwrap($string, $width = 75, $break = "\n", $cut = false, $charset = 'utf-8') 380 { 381 $stringWidth = iconv_strlen($string, $charset); 382 $breakWidth = iconv_strlen($break, $charset); 383 384 if (strlen($string) === 0) { 385 return ''; 386 } elseif ($breakWidth === null) { 387 throw new Zend_Text_Exception('Break string cannot be empty'); 388 } elseif ($width === 0 && $cut) { 389 throw new Zend_Text_Exception('Can\'t force cut when width is zero'); 390 } 391 392 $result = ''; 393 $lastStart = $lastSpace = 0; 394 395 for ($current = 0; $current < $stringWidth; $current++) { 396 $char = iconv_substr($string, $current, 1, $charset); 397 398 if ($breakWidth === 1) { 399 $possibleBreak = $char; 400 } else { 401 $possibleBreak = iconv_substr($string, $current, $breakWidth, $charset); 402 } 403 404 if ($possibleBreak === $break) { 405 $result .= iconv_substr($string, $lastStart, $current - $lastStart + $breakWidth, $charset); 406 $current += $breakWidth - 1; 407 $lastStart = $lastSpace = $current + 1; 408 } elseif ($char === ' ') { 409 if ($current - $lastStart >= $width) { 410 $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break; 411 $lastStart = $current + 1; 412 } 413 414 $lastSpace = $current; 415 } elseif ($current - $lastStart >= $width && $cut && $lastStart >= $lastSpace) { 416 $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset) . $break; 417 $lastStart = $lastSpace = $current; 418 } elseif ($current - $lastStart >= $width && $lastStart < $lastSpace) { 419 $result .= iconv_substr($string, $lastStart, $lastSpace - $lastStart, $charset) . $break; 420 $lastStart = $lastSpace = $lastSpace + 1; 421 } 422 } 423 424 if ($lastStart !== $current) { 425 $result .= iconv_substr($string, $lastStart, $current - $lastStart, $charset); 426 } 427 428 return $result; 429 } 430 431
Note: See TracChangeset
for help on using the changeset viewer.