Index: inc/public/lib.tpl.context.php
===================================================================
--- inc/public/lib.tpl.context.php	(revision 937)
+++ inc/public/lib.tpl.context.php	(revision 1107)
@@ -174,26 +174,4 @@
 	}
 	
-	public static function categoryPostParam(&$p)
-	{
-		$not = substr($p['cat_url'],0,1) == '!';
-		if ($not) {
-			$p['cat_url'] = substr($p['cat_url'],1);
-		}
-		
-		$p['cat_url'] = preg_split('/\s*,\s*/',$p['cat_url'],-1,PREG_SPLIT_NO_EMPTY);
-		
-		foreach ($p['cat_url'] as &$v)
-		{
-			if ($not) {
-				$v .= ' ?not';
-			}
-			if ($GLOBALS['_ctx']->exists('categories') && preg_match('/#self/',$v)) {
-				$v = preg_replace('/#self/',$GLOBALS['_ctx']->categories->cat_url,$v);
-			} elseif ($GLOBALS['_ctx']->exists('posts') && preg_match('/#self/',$v)) {
-				$v = preg_replace('/#self/',$GLOBALS['_ctx']->posts->cat_url,$v);
-			}
-		}
-	}
-	
 	# Static methods for pagination
 	public static function PaginationNbPages()
@@ -348,122 +326,66 @@
 			return $str;
 		}
-		
-		return preg_replace(array_keys($GLOBALS['__smilies']),array_values($GLOBALS['__smilies']),$str);
-	}
-
-	# First post image helpers
-	public static function EntryFirstImageHelper($size,$with_category,$class="",$no_tag=false,$content_only=false,$cat_only=false)
-	{
-		global $core, $_ctx;
-		
-		$media = new dcMedia($core);
-		$sizes = implode('|',array_keys($media->thumb_sizes)).'|o';
-		if (!preg_match('/^'.$sizes.'$/',$size)) {
-			$size = 's';
-		}
-		$p_url = $core->blog->settings->system->public_url;
-		$p_site = preg_replace('#^(.+?//.+?)/(.*)$#','$1',$core->blog->url);
-		$p_root = $core->blog->public_path;
-		
-		$pattern = '(?:'.preg_quote($p_site,'/').')?'.preg_quote($p_url,'/');
-		$pattern = sprintf('/<img.+?src="%s(.*?\.(?:jpg|jpeg|gif|png))"[^>]+/msui',$pattern);
-		
-		$src = '';
-		$alt = '';
-		
-		# We first look in post content
-		if (!$cat_only && $_ctx->posts)
-		{
-			$subject = ($content_only ? '' : $_ctx->posts->post_excerpt_xhtml).$_ctx->posts->post_content_xhtml;
-			if (preg_match_all($pattern,$subject,$m) > 0)
-			{
-				foreach ($m[1] as $i => $img) {
-					if (($src = self::ContentFirstImageLookup($p_root,$img,$size)) !== false) {
-						$dirname = str_replace('\\', '/', dirname($img)); 
-						$src = $p_url.($dirname != '/' ? $dirname : '').'/'.$src;
-						if (preg_match('/alt="([^"]+)"/',$m[0][$i],$malt)) {
-							$alt = $malt[1];
-						}
-						break;
-					}
+
+		# Process part adapted from SmartyPants engine (J. Gruber et al.) :
+
+		$tokens = self::tokenizeHTML($str);
+		$result = '';
+		$in_pre = 0;  # Keep track of when we're inside <pre> or <code> tags.
+
+		foreach ($tokens as $cur_token) {
+			if ($cur_token[0] == "tag") {
+				# Don't mess with quotes inside tags.
+				$result .= $cur_token[1];
+				if (preg_match('@<(/?)(?:pre|code|kbd|script|math)[\s>]@', $cur_token[1], $matches)) {
+					$in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1;
 				}
-			}
-		}
-		
-		# No src, look in category description if available
-		if (!$src && $with_category && $_ctx->categories)
-		{
-			if (preg_match_all($pattern,$_ctx->categories->cat_desc,$m) > 0)
-			{
-				foreach ($m[1] as $i => $img) {
-					if (($src = self::ContentFirstImageLookup($p_root,$img,$size)) !== false) {
-						$dirname = str_replace('\\', '/', dirname($img)); 
-						$src = $p_url.($dirname != '/' ? $dirname : '').'/'.$src;
-						if (preg_match('/alt="([^"]+)"/',$m[0][$i],$malt)) {
-							$alt = $malt[1];
-						}
-						break;
-					}
+			} else {
+				$t = $cur_token[1];
+				if (!$in_pre) {
+					$t = preg_replace(array_keys($GLOBALS['__smilies']),array_values($GLOBALS['__smilies']),$t);
 				}
-			};
-		}
-		
-		if ($src) {
-			if ($no_tag) {
-				return $src;
-			} else {
-				return '<img alt="'.$alt.'" src="'.$src.'" class="'.$class.'" />';
-			}
-		}
-	}
-	
-	private static function ContentFirstImageLookup($root,$img,$size)
-	{
-		global $core;
-		
-		# Get base name and extension
-		$info = path::info($img);
-		$base = $info['base'];
-		
-		$media = new dcMedia($core);
-		$sizes = implode('|',array_keys($media->thumb_sizes));
-		if (preg_match('/^\.(.+)_('.$sizes.')$/',$base,$m)) {
-			$base = $m[1];
-		}
-		
-		$res = false;
-		if ($size != 'o' && file_exists($root.'/'.$info['dirname'].'/.'.$base.'_'.$size.'.jpg'))
-		{
-			$res = '.'.$base.'_'.$size.'.jpg';
-		}
-		else
-		{
-			$f = $root.'/'.$info['dirname'].'/'.$base;
-			if (file_exists($f.'.'.$info['extension'])) {
-				$res = $base.'.'.$info['extension'];
-			} elseif (file_exists($f.'.jpg')) {
-				$res = $base.'.jpg';
-			} elseif (file_exists($f.'.jpeg')) {
-				$res = $base.'.jpeg';
-			} elseif (file_exists($f.'.png')) {
-				$res = $base.'.png';
-			} elseif (file_exists($f.'.gif')) {
-				$res = $base.'.gif';
-			} elseif (file_exists($f.'.JPG')) {
-				$res = $base.'.JPG';
-			} elseif (file_exists($f.'.JPEG')) {
-				$res = $base.'.JPEG';
-			} elseif (file_exists($f.'.PNG')) {
-				$res = $base.'.PNG';
-			} elseif (file_exists($f.'.GIF')) {
-				$res = $base.'.GIF';
-			}
-		}
-		
-		if ($res) {
-			return $res;
-		}
-		return false;
-	}
+				$result .= $t;
+			}
+		}
+
+		return $result;
+	}
+
+	private static function tokenizeHTML($str)
+	{
+		# Function from SmartyPants engine (J. Gruber et al.)
+		#
+		#   Parameter:  String containing HTML markup.
+		#   Returns:    An array of the tokens comprising the input
+		#               string. Each token is either a tag (possibly with nested,
+		#               tags contained therein, such as <a href="<MTFoo>">, or a
+		#               run of text between tags. Each element of the array is a
+		#               two-element array; the first is either 'tag' or 'text';
+		#               the second is the actual value.
+		#
+		#
+		#   Regular expression derived from the _tokenize() subroutine in 
+		#   Brad Choate's MTRegex plugin.
+		#   <http://www.bradchoate.com/past/mtregex.php>
+		#
+		$index = 0;
+		$tokens = array();
+
+		$match = '(?s:<!(?:--.*?--\s*)+>)|'.	# comment
+				 '(?s:<\?.*?\?>)|'.				# processing instruction
+												# regular tags
+				 '(?:<[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)'; 
+
+		$parts = preg_split("{($match)}", $str, -1, PREG_SPLIT_DELIM_CAPTURE);
+
+		foreach ($parts as $part) {
+			if (++$index % 2 && $part != '') 
+				$tokens[] = array('text', $part);
+			else
+				$tokens[] = array('tag', $part);
+		}
+		return $tokens;
+	}
+
 }
 ?>
