Index: modules/filter/filter.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/filter/filter.module,v
retrieving revision 1.160.2.5
diff -u -p -r1.160.2.5 filter.module
--- modules/filter/filter.module	10 Jan 2008 22:14:24 -0000	1.160.2.5
+++ modules/filter/filter.module	20 Jan 2008 15:50:11 -0000
@@ -1210,22 +1210,22 @@ function _filter_autop($text) {
       }
     }
     else if (!$ignore) {
-      $chunk = preg_replace('|\n*$|', '', $chunk) ."\n\n"; // just to make things a little easier, pad the end
-      $chunk = preg_replace('|<br />\s*<br />|', "\n\n", $chunk);
-      $chunk = preg_replace('!(<'. $block .'[^>]*>)!', "\n$1", $chunk); // Space things out a little
-      $chunk = preg_replace('!(</'. $block .'>)!', "$1\n\n", $chunk); // Space things out a little
-      $chunk = preg_replace("/\n\n+/", "\n\n", $chunk); // take care of duplicates
-      $chunk = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $chunk); // make paragraphs, including one at the end
-      $chunk = preg_replace('|<p>\s*</p>\n|', '', $chunk); // under certain strange conditions it could create a P of entirely whitespace
-      $chunk = preg_replace("|<p>(<li.+?)</p>|", "$1", $chunk); // problem with nested lists
-      $chunk = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $chunk);
-      $chunk = str_replace('</blockquote></p>', '</p></blockquote>', $chunk);
-      $chunk = preg_replace('!<p>\s*(</?'. $block .'[^>]*>)!', "$1", $chunk);
-      $chunk = preg_replace('!(</?'. $block .'[^>]*>)\s*</p>!', "$1", $chunk);
-      $chunk = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $chunk); // make line breaks
-      $chunk = preg_replace('!(</?'. $block .'[^>]*>)\s*<br />!', "$1", $chunk);
-      $chunk = preg_replace('!<br />(\s*</?(?:p|li|div|th|pre|td|ul|ol)>)!', '$1', $chunk);
-      $chunk = preg_replace('/&([^#])(?![A-Za-z0-9]{1,8};)/', '&amp;$1', $chunk);
+      $chunk = preg_replace('|\n*$|', '', $chunk) ."\n\n";									// exactly two newlines at the end
+      $chunk = preg_replace('|<br />\s*<br />|', "\n\n", $chunk);							// skip two breaks in a row
+      $chunk = preg_replace('!(<'. $block .'[^>]*>)!', "\n$1", $chunk);						// add a newline before block start
+      $chunk = preg_replace('!(</'. $block .'>)!', "$1\n\n", $chunk);						// add two newlines after block end
+      $chunk = preg_replace("/\n\n+/", "\n\n", $chunk);										// no more than double newline
+      $chunk = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $chunk);			// make paragraphs, including one at the end (*** the '\z' seems unnecessary) 
+      $chunk = preg_replace("|<p>(<li.+?)</p>|", "$1", $chunk);								// do not close list elements in paragraphs
+      $chunk = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $chunk);		// move paragraphs into blockquotes (*** the only place it's case insensitive, why?)
+      $chunk = str_replace('</blockquote></p>', '</p></blockquote>', $chunk);				//
+      $chunk = preg_replace('|<p>\s*</p>\n?|', '', $chunk);									// get rid of all empty paragraphs
+      $chunk = preg_replace('!<p>\s*(</?'. $block .'[^>]*>)!', "$1", $chunk);				// skip paragraph starts just before block open/close
+      $chunk = preg_replace('!(</?'. $block .'[^>]*>)\s*</p>!', "$1", $chunk);				// skip paragraph ends just after block open/close
+      $chunk = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $chunk);						// make a break for each newline not preceeded by a break
+      $chunk = preg_replace('!(</?'. $block .'[^>]*>)\s*<br />!', "$1", $chunk);			// remove breaks just before block end
+      $chunk = preg_replace('!<br />(\s*</?(?:p|li|div|th|pre|td|ul|ol)>)!', '$1', $chunk);	// remove breaks before certain elements ends
+      $chunk = preg_replace('/&([^#])(?![A-Za-z0-9]{1,8};)/', '&amp;$1', $chunk);			// replace ampersands, but not html entities
     }
     $output .= $chunk;
   }
