/** * http://code.spip.net/@traiter_tableau * * @param sring $bloc * @return string */ function traiter_tableau($bloc) { // id "unique" pour les id du tableau $tabid = substr(md5($bloc), 0, 4); // Decouper le tableau en lignes preg_match_all(',([|].*)[|]\\n,UmsS', $bloc, $regs, PREG_PATTERN_ORDER); $lignes = array(); $debut_table = $summary = ''; $l = 0; $numeric = true; // Traiter chaque ligne $reg_line1 = ',^(\\|(' . _RACCOURCI_TH_SPAN . '))+$,sS'; $reg_line_all = ',^(' . _RACCOURCI_TH_SPAN . ')$,sS'; $hc = $hl = array(); foreach ($regs[1] as $ligne) { $l++; // Gestion de la premiere ligne : if ($l == 1) { // - <caption> et summary dans la premiere ligne : // || caption | summary || (|summary est optionnel) if (preg_match(',^\\|\\|([^|]*)(\\|(.*))?$,sS', rtrim($ligne, '|'), $cap)) { $l = 0; if ($caption = trim($cap[1])) { $debut_table .= "<caption>" . $caption . "</caption>\n"; } $summary = ' summary="' . entites_html(trim($cap[3])) . '"'; } else { if (preg_match($reg_line1, $ligne, $thead)) { preg_match_all('/\\|([^|]*)/S', $ligne, $cols); $ligne = ''; $cols = $cols[1]; $colspan = 1; for ($c = count($cols) - 1; $c >= 0; $c--) { $attr = ''; if ($cols[$c] == '<') { $colspan++; } else { if ($colspan > 1) { $attr = " colspan='{$colspan}'"; $colspan = 1; } // inutile de garder le strong qui n'a servi que de marqueur $cols[$c] = str_replace(array('{', '}'), '', $cols[$c]); $ligne = "<th id='id{$tabid}_c{$c}'{$attr}>{$cols[$c]}</th>{$ligne}"; $hc[$c] = "id{$tabid}_c{$c}"; // pour mettre dans les headers des td } } $debut_table .= "<thead><tr class='row_first'>" . $ligne . "</tr></thead>\n"; $l = 0; } } } // Sinon ligne normale if ($l) { // Gerer les listes a puce dans les cellules // on declenche simplement sur \n- car il y a les // -* -# -? -! (qui produisent des - !) if (strpos($ligne, "\n-") !== false) { $ligne = traiter_listes($ligne); } // tout mettre dans un tableau 2d preg_match_all('/\\|([^|]*)/S', $ligne, $cols); // Pas de paragraphes dans les cellules foreach ($cols[1] as &$col) { if (strlen($col = trim($col))) { $col = preg_replace("/\n{2,}/S", "<br /> <br />", $col); if (_AUTOBR) { $col = str_replace("\n", _AUTOBR . "\n", $col); } } } // assembler le tableau $lignes[] = $cols[1]; } } // maintenant qu'on a toutes les cellules // on prepare une liste de rowspan par defaut, a partir // du nombre de colonnes dans la premiere ligne. // Reperer egalement les colonnes numeriques pour les cadrer a droite $rowspans = $numeric = array(); $n = count($lignes[0]); $k = count($lignes); // distinguer les colonnes numeriques a point ou a virgule, // pour les alignements eventuels sur "," ou "." $numeric_class = array('.' => 'point', ',' => 'virgule'); for ($i = 0; $i < $n; $i++) { $align = true; for ($j = 0; $j < $k; $j++) { $rowspans[$j][$i] = 1; if ($align and preg_match('/^[+-]?(?:\\s|\\d)*([.,]?)\\d*$/', trim($lignes[$j][$i]), $r)) { if ($r[1]) { $align = $r[1]; } } else { $align = ''; } } $numeric[$i] = $align ? " class='numeric " . $numeric_class[$align] . "'" : ''; } for ($j = 0; $j < $k; $j++) { if (preg_match($reg_line_all, $lignes[$j][0])) { $hl[$j] = "id{$tabid}_l{$j}"; // pour mettre dans les headers des td } else { unset($hl[0]); } } if (!isset($hl[0])) { $hl = array(); } // toute la colonne ou rien // et on parcourt le tableau a l'envers pour ramasser les // colspan et rowspan en passant $html = ''; for ($l = count($lignes) - 1; $l >= 0; $l--) { $cols = $lignes[$l]; $colspan = 1; $ligne = ''; for ($c = count($cols) - 1; $c >= 0; $c--) { $attr = $numeric[$c]; $cell = trim($cols[$c]); if ($cell == '<') { $colspan++; } elseif ($cell == '^') { $rowspans[$l - 1][$c] += $rowspans[$l][$c]; } else { if ($colspan > 1) { $attr .= " colspan='{$colspan}'"; $colspan = 1; } if (($x = $rowspans[$l][$c]) > 1) { $attr .= " rowspan='{$x}'"; } $b = ($c == 0 and isset($hl[$l])) ? 'th' : 'td'; $h = (isset($hc[$c]) ? $hc[$c] : '') . ' ' . (($b == 'td' and isset($hl[$l])) ? $hl[$l] : ''); if ($h = trim($h)) { $attr .= " headers='{$h}'"; } // inutile de garder le strong qui n'a servi que de marqueur if ($b == 'th') { $attr .= " id='" . $hl[$l] . "'"; $cols[$c] = str_replace(array('{', '}'), '', $cols[$c]); } $ligne = "\n<{$b}" . $attr . '>' . $cols[$c] . "</{$b}>" . $ligne; } } // ligne complete $class = alterner($l + 1, 'odd', 'even'); $html = "<tr class='row_{$class} {$class}'>{$ligne}</tr>\n{$html}"; } return "\n\n<table" . $GLOBALS['class_spip_plus'] . $summary . ">\n" . $debut_table . "<tbody>\n" . $html . "</tbody>\n" . "</table>\n\n"; }
function core_traiter_raccourcis($letexte) { // Appeler les fonctions de pre_traitement $letexte = pipeline('pre_propre', $letexte); // Gerer les notes (ne passe pas dans le pipeline) $notes = charger_fonction('notes', 'inc'); list($letexte, $mes_notes) = $notes($letexte); // // Tableaux // // ne pas oublier les tableaux au debut ou a la fin du texte $letexte = preg_replace(",^\n?[|],S", "\n\n|", $letexte); $letexte = preg_replace(",\n\n+[|],S", "\n\n\n\n|", $letexte); $letexte = preg_replace(",[|](\n\n+|\n?\$),S", "|\n\n\n\n", $letexte); if (preg_match_all(',[^|](\\n[|].*[|]\\n)[^|],UmsS', $letexte, $regs, PREG_SET_ORDER)) { foreach ($regs as $t) { $letexte = str_replace($t[1], traiter_tableau($t[1]), $letexte); } } $letexte = "\n" . trim($letexte); // les listes if (strpos($letexte, "\n-*") !== false or strpos($letexte, "\n-#") !== false) { $letexte = traiter_listes($letexte); } // Proteger les caracteres actifs a l'interieur des tags html if (preg_match_all(_RACCOURCI_BALISE, $letexte, $regs, PREG_SET_ORDER)) { foreach ($regs as $reg) { $insert = strtr($reg[0], _RACCOURCI_PROTEGER, _RACCOURCI_PROTECTEUR); $letexte = str_replace($reg[0], $insert, $letexte); } } // Traitement des alineas list($a, $b) = definir_raccourcis_alineas(); $letexte = preg_replace($a, $b, $letexte); // Introduction des attributs class_spip* et autres raccourcis list($a, $b) = $GLOBALS['spip_raccourcis_typo']; $letexte = preg_replace($a, $b, $letexte); $letexte = preg_replace('@^\\n<br />@S', '', $letexte); // Retablir les caracteres proteges $letexte = strtr($letexte, _RACCOURCI_PROTECTEUR, _RACCOURCI_PROTEGER); // Fermer les paragraphes ; mais ne pas forcement en creer si un seul $letexte = paragrapher($letexte, $GLOBALS['toujours_paragrapher']); // Appeler les fonctions de post-traitement $letexte = pipeline('post_propre', $letexte); if ($mes_notes) { $notes($mes_notes); } return $letexte; }