Superteaser
17 October 2005
Updated 12 June 2006
Superteaser generates a teaser based on a body of text. A teaser is simply an excerpt (of specified length) which starts at the beginning of the text. The trick is to cut the text in such a way that it is near the desired length while remaining readable (i.e., words or HTML tags are not cut off at the end).
Problem
Often it's not good enough to cut at the first paragraph break after the desired length (as Drupal does out of the box). Since we'd like to break at a paragraph, but might be willing to break at a sentence, and would prefer either of these to breaking between words (or mid-word), we need a way to (1) rank breakpoints and (2) weigh the value of breaking at a desirable location against having a teaser very close to our desired length.
Solution
We rank breakpoints by how much they're worth going out of our way to find. For example, breaking at the end of a paragraph might be worth missing our target length by 50 characters whereas breaking between words might only be worth 8 characters. Superteaser tries each of the breakpoints and chooses the best result according to how you have weighted the various break points.
Other Benefits
In addition to breaking text at an intelligent place, Superteaser:
- computes teaser length by counting only characters which are actually visible, ignoring HTML tags
- won't cut text mid-HTML tag
- closes any tags which are left open after the text is cut
- allows specification of whether the teaser should be shorter or longer than the optimal length, enabling the generation of teasers which are guaranteed not to exceed a certain size (e.g., for fitting on a single line)
- allows configuration of the explicit break string ("<!--break-->" by default)
Comparison
Here you can compare the performance of Superteaser with Drupal's built-in teaser generation. Look below the form to see the results. Note that Superteaser will close any HTML tags that are left open (with some intentional exceptions).