#73910: Teaser length should be a maximum as the description says, not a minimum.

5.x
Steven Wittens 2006-12-12 08:35:30 +00:00
parent 4eda8b315d
commit a0ca5362c0
1 changed files with 27 additions and 10 deletions

View File

@ -173,19 +173,36 @@ function node_teaser($body, $format = NULL) {
return $body;
}
// In some cases, no delimiter has been specified (e.g. when posting using
// the Blogger API). In this case, we try to split at paragraph boundaries.
// When even the first paragraph is too long, we try to split at the end of
// the next sentence.
$breakpoints = array('</p>' => 4, '<br />' => 0, '<br>' => 0, "\n" => 0, '. ' => 1, '! ' => 1, '? ' => 1, '。' => 3, '؟ ' => 1);
foreach ($breakpoints as $point => $charnum) {
if ($length = strpos($body, $point, $size)) {
return substr($body, 0, $length + $charnum);
// The teaser may not be longer than maximum length specified. Initial slice.
$teaser = truncate_utf8($body, $size);
$position = 0;
// Cache the reverse of the teaser.
$reversed = strrev($teaser);
// In some cases, no delimiter has been specified. In this case, we try to
// split at paragraph boundaries.
$breakpoints = array('</p>' => 0, '<br />' => 6, '<br>' => 4, "\n" => 1);
// We use strpos on the reversed needle and haystack for speed.
foreach ($breakpoints as $point => $offset) {
$length = strpos($reversed, strrev($point));
if ($length !== FALSE) {
$position = - $length - $offset;
return ($position == 0) ? $teaser : substr($teaser, 0, $position);
}
}
// If all else fails, we simply truncate the string.
return truncate_utf8($body, $size);
// When even the first paragraph is too long, we try to split at the end of
// the last full sentence.
$breakpoints = array('. ' => 1, '! ' => 1, '? ' => 1, '。' => 0, '؟ ' => 1);
$min_length = strlen($reversed);
foreach ($breakpoints as $point => $offset) {
$length = strpos($reversed, strrev($point));
if ($length !== FALSE) {
$min_length = min($length, $min_length);
$position = 0 - $length - $offset;
}
}
return ($position == 0) ? $teaser : substr($teaser, 0, $position);
}
/**