Index: modules/node.module =================================================================== RCS file: /cvs/drupal/drupal/modules/node.module,v retrieving revision 1.477 diff -u -r1.477 node.module --- modules/node.module 18 Mar 2005 08:14:37 -0000 1.477 +++ modules/node.module 18 Mar 2005 18:46:00 -0000 @@ -154,23 +154,27 @@ } /** - * Automatically generate a teaser for the given body text. + * Automatically generate a teaser for a node body in a given format. */ -function node_teaser($body) { +function node_teaser($body, $format = NULL) { $size = variable_get('teaser_length', 600); // find where the delimiter is in the body $delimiter = strpos($body, ''); - // If the size is zero, and there is no delimiter, we return the entire body. + // If the size is zero, and there is no delimiter, the entire body is the teaser. if ($size == 0 && $delimiter == 0) { return $body; } - // If the body contains PHP code, do not split it up to prevent parse errors. - if (strpos($body, '', $size)) { - return substr($body, 0, $length + 4); - } - - if ($length = strpos($body, '
', $size)) { - return substr($body, 0, $length); - } - - if ($length = strpos($body, '
', $size)) { - return substr($body, 0, $length); - } - - if ($length = strpos($body, "\n", $size)) { - return substr($body, 0, $length); - } - - // When even the first paragraph is too long, try to split at the end of + // When even the first paragraph is too long, we try to split at the end of // the next sentence. - if ($length = strpos($body, '. ', $size)) { - return substr($body, 0, $length + 1); - } - - if ($length = strpos($body, '! ', $size)) { - return substr($body, 0, $length + 1); - } - - if ($length = strpos($body, '? ', $size)) { - return substr($body, 0, $length + 1); - } - - if ($length = strpos($body, '。', $size)) { - return substr($body, 0, $length + 1); - } - - if ($length = strpos($body, '、', $size)) { - return substr($body, 0, $length + 1); - } - - if ($length = strpos($body, '؟ ', $size)) { - return substr($body, 0, $length + 1); + $breakpoints = array('

' => 4, '
' => 0, '
' => 0, "\n" => 0, '. ' => 1, '! ' => 1, '? ' => 1, '。' => 1, '、' => 1, '؟ ' => 1); + foreach ($breakpoints as $point => $charnum) { + if ($length = strpos($body, $point, $size)) { + return substr($body, 0, $length + $charnum); + } } - - // If all else fails, simply truncate the string. + + // If all else fails, we simply truncate the string. return truncate_utf8($body, $size); } - /** * Determine the module that defines the node type of the given node. * @@ -1211,7 +1181,7 @@ // Auto-generate the teaser, but only if it hasn't been set (e.g. by a // module-provided 'teaser' form item). if (!isset($node->teaser)) { - $node->teaser = node_teaser($node->body); + $node->teaser = node_teaser($node->body, $node->format); } if (node_last_changed($node->nid) > $node->changed) { @@ -1496,7 +1466,7 @@ // Extract a teaser, if it hasn't been set (e.g. by a module-provided // 'teaser' form item). if (!isset($node->teaser)) { - $node->teaser = node_teaser($node->body); + $node->teaser = node_teaser($node->body, $node->format); } // Display a preview of the node: