From 732b6c928d23815c5ecae000e7c2be1f3bc9e8c9 Mon Sep 17 00:00:00 2001
From: Fil <fil@rezo.net>
Date: Tue, 2 Mar 2004 22:39:05 +0000
Subject: [PATCH] =?UTF-8?q?fonction=20couper{}=20plus=20sympa=20pour=20les?=
 =?UTF-8?q?=20longueurs=20tr=C3=A8s=20courtes,=20et=20moins=20risqu=C3=A9e?=
 =?UTF-8?q?=20pour=20les=20tirets,=20tableaux=20etc.=20(=C3=A0=20partir=20?=
 =?UTF-8?q?de=20suggestions=20faites=20par=20david=20larouziere)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 ecrire/inc_texte.php3 | 50 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 40 insertions(+), 10 deletions(-)

diff --git a/ecrire/inc_texte.php3 b/ecrire/inc_texte.php3
index 8b987ba9b2..c477b7d819 100644
--- a/ecrire/inc_texte.php3
+++ b/ecrire/inc_texte.php3
@@ -298,10 +298,19 @@ function echappe_retour($letexte, $les_echap, $source) {
 	return $letexte;
 }
 
-function couper($texte, $long) {
-	$texte2 = substr($texte, 0, $long * 2); /* heuristique pour prendre seulement le necessaire */
-	if (strlen($texte2) < strlen($texte)) $plus_petit = true;
-	$texte = ereg_replace("\[([^\[]*)->([^]]*)\]","\\1", $texte2);
+function couper($texte, $taille=50) {
+	$texte = substr($texte, 0, 400 + 2*$taille); /* eviter de travailler sur 10ko pour extraire 150 caracteres */
+
+	// supprimer les tags
+	$texte = supprimer_tags($texte);
+	$texte = trim(ereg_replace("[\n\r]"," ", $texte));
+	$texte .= "\n";	// marquer la fin
+
+	// travailler en accents charset
+	$texte = filtrer_entites($texte);
+
+	// supprimer les liens
+	$texte = ereg_replace("\[([^\[]*)->([^]]*)\]","\\1", $texte);
 
 	// supprimer les notes
 	$texte = ereg_replace("\[\[([^]]|\][^]])*\]\]", "", $texte);
@@ -309,11 +318,32 @@ function couper($texte, $long) {
 	// supprimer les codes typos
 	$texte = ereg_replace("[{}]", "", $texte);
 
-	$texte2 = substr($texte, 0, $long);
-	$texte2 = ereg_replace("([^[:space:]][[:space:]]+)[^[:space:]]*$", "\\1", $texte2);
-	if ((strlen($texte2) + 3) < strlen($texte)) $plus_petit = true;
-	if ($plus_petit) $texte2 .= '&nbsp;(...)';
-	return $texte2;
+	// supprimer les tableaux
+	$texte = ereg_replace("(^|\n)\|.*\|\n", "", $texte);
+
+	// supprimer les traits, lignes etc
+	$texte = ereg_replace("(^|\n)-[-#\*]*", "", $texte);
+
+	// couper au mot precedent
+	$long = substr($texte, 0, max($taille-4,1));
+	$court = ereg_replace("([^[:space:]][[:space:]]+)[^[:space:]]*$", "\\1", $long);
+	$points = '&nbsp;(...)';
+
+	// trop court ? ne pas faire de (...)
+	if (strlen($court) < max(0.75 * $taille,2)) {
+		$points = '';
+		$long = ereg_replace("&#?[a-z0-9]*;?$", "", substr($texte, 0, $taille));
+		$texte = ereg_replace("([^[:space:]][[:space:]]+)[^[:space:]]*$", "\\1", $long);
+		// encore trop court ? couper au caractere
+		if (strlen($texte) < 0.75 * $taille)
+			$texte = $long;
+	} else
+		$texte = $court;
+
+	if (strpos("\n", $texte))	// la fin est encore la : c'est qu'on n'a pas de texte de suite
+		$points = '';
+
+	return trim($texte).$points;
 }
 
 // prendre <intro>...</intro> sinon couper a la longueur demandee
@@ -328,7 +358,7 @@ function couper_intro($texte, $long) {
 	}
 
 	if ($intro)
-		$intro = $intro.' (...)';
+		$intro = $intro.'&nbsp;(...)';
 	else
 		$intro = couper($texte, $long);
 
-- 
GitLab