[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins-dist / textwheel / wheels / spip / spip-paragrapher.php
1 <?php
2
3 /**
4 * Fonctions utiles pour les wheels SPIP sur les paragraphes
5 *
6 * @SPIP\Textwheel\Wheel\SPIP\Fonctions
7 **/
8
9 if (!defined('_ECRIRE_INC_VERSION')) {
10 return;
11 }
12
13 if (!defined('_BALISES_BLOCS')) {
14 define('_BALISES_BLOCS',
15 'address|applet|article|aside|blockquote|button|center|d[ltd]|div|fieldset|fig(ure|caption)|footer|form|h[1-6r]|hgroup|head|header|iframe|li|map|marquee|nav|noscript|object|ol|pre|section|t(able|[rdh]|body|foot|extarea)|ul|script|style'
16 );
17 }
18
19 /**
20 * Callback de detection des liens qui contiennent des blocks :
21 * dans ce cas il faut traiter le <a> comme un quasi block et fermer/ouvrir les <p> autour du <a>
22 *
23 * @param string $t
24 * @return string
25 */
26 function detecter_liens_blocs(&$t) {
27
28 // si une balise bloc est dans le liens, on y a aussi ajoute un <p>, il suffit donc de detecter ce dernier
29 if (strpos($t[2], "<p>") !== false) {
30 return "<STOP P>" . $t[1] . "<p>" . $t[2] . "</p>" . $t[3] . "\n<p>";
31 }
32
33 return $t[0];
34 }
35
36 /**
37 * Callback fermer-para-mano
38 *
39 * On refait le preg, à la main
40 *
41 * @param string $t
42 * @return string
43 */
44 function fermer_para_mano(&$t) {
45 # match: ",<p (.*)<(/?)(stop p|address|applet|article|aside|blockquote|button|center|d[ltd]|div|fieldset|fig(ure|caption)|footer|form|h[1-6r]|hgroup|head|header|iframe|li|map|marquee|nav|noscript|object|ol|pre|section|t(able|[rdh]|body|foot|extarea)|ul|script|style)\b,UimsS"
46 # replace: "\n<p "+trim($1)+"</p>\n<$2$3"
47
48 foreach (array('<p ' => "</p>\n", '<li' => "<br-li/>") as $cut => $close) {
49 if (strpos($t, $cut) !== false) {
50 foreach (explode($cut, $t) as $c => $p) {
51 if ($c == 0) {
52 $t = $p;
53 } else {
54 $pi = strtolower($p);
55 if (preg_match(
56 ",</?(?:stop p|" . _BALISES_BLOCS . ")\b,S",
57 $pi, $r)) {
58 $pos = strpos($pi, $r[0]);
59 $t .= $cut . str_replace("\n", _AUTOBR . "\n",
60 ($close ? rtrim(substr($p, 0, $pos)) : substr($p, 0, $pos))) . $close . substr($p, $pos);
61 } else {
62 $t .= $cut . $p;
63 }
64 }
65 }
66 }
67 }
68
69 if (strpos($t, "<br-li/>") !== false) {
70 $t = str_replace("<br-li/></li>", "</li>", $t); // pour respecter les non-retour lignes avant </li>
71 $t = str_replace("<br-li/><ul>", "<ul>", $t); // pour respecter les non-retour lignes avant <ul>
72 $t = str_replace("<br-li/>", "\n", $t);
73 }
74 if (_AUTOBR) {
75 $t = str_replace(_AUTOBR . "\n" . "<br", "\n<br", $t); #manque /i
76 $reg = ',(<(p|br|li)\b[^>]*>\s*)' . preg_quote(_AUTOBR . "\n", ',') . ",iS";
77 $t = preg_replace($reg, '\1' . "\n", $t);
78 }
79
80 return $t;
81 }