Index: project_issue.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/project_issue/project_issue.module,v
retrieving revision 1.88.2.9
diff -u -p -r1.88.2.9 project_issue.module
--- project_issue.module	29 Oct 2008 00:20:24 -0000	1.88.2.9
+++ project_issue.module	30 Nov 2008 21:25:38 -0000
@@ -1245,31 +1245,31 @@ function project_issue_filter($op, $delt
     case 'prepare':
       return $text;
     case 'process':
-      $regex = '(?<!\w)(\[#(\d+)(-(\d+))?\])(?![^<]*<\/a>|([^<]++|(<(?!code)))*<\/code>|([^<]++|(<(?!pre)))*<\/pre>|\w)';
-      $offset = 0;
-      while(preg_match("/$regex/", $text, $preg_matches, PREG_OFFSET_CAPTURE, $offset)) {
-        $offset++;
-        $match = $preg_matches[1];
-        $nid = $preg_matches[2][0];
-        $comment_number = $preg_matches[4][0];
-        $node = node_load($nid);
-        if (is_object($node) && node_access('view', $node) && $node->type == 'project_issue') {
-          // Pull comment id based on the comment number if we have one.
-          if (!is_null($comment_number) && ($comment_id = db_result(db_query("SELECT pic.cid FROM {project_issue_comments} pic INNER JOIN {comments} c ON pic.cid = c.cid WHERE pic.nid = %d AND pic.comment_number = %d AND c.status = %d", $nid, $comment_number, COMMENT_PUBLISHED)))) {
-            $link = theme('project_issue_issue_link', $node, $comment_id, $comment_number);
-          }
-          else {
-            $link = theme('project_issue_issue_link', $node);
-          }
-          $text = substr_replace($text, $link, $match[1], strlen($match[0]));
-          $offset = max($offset, $match[1] + strlen($link));
-        }
-        else {
-          $offset = max($offset, $match[1] + strlen($match[0]));
+      $regex = '(?:(?<!\w)\[#\d+(?:-\d+)?\](?!\w))|<pre>.*?<\/pre>|<code>.*?<\/code>|<a(?:[^>"\']|"[^"]*"|\'[^\']*\')*>.*?<\/a>';
+      $text = preg_replace_callback("/$regex/", 'project_issue_link_filter_callback', $text);
+      return $text;
+  }
+}
+
+function project_issue_link_filter_callback($matches) {
+  if (preg_match('/^\[#(\d+)(?:-(\d+))?\]$/', $matches[0], $parts)) {
+    $nid = $parts[1];
+    $node = node_load($nid);
+    if (is_object($node) && node_access('view', $node) && $node->type == 'project_issue') {
+      if (isset($parts[2])) {
+        // Pull comment id based on the comment number if we have one.
+        $comment_number = $parts[2];
+        if ($comment_id = db_result(db_query("SELECT pic.cid FROM {project_issue_comments} pic INNER JOIN {comments} c ON pic.cid = c.cid WHERE pic.nid = %d AND pic.comment_number = %d AND c.status = %d", $nid, $comment_number, COMMENT_PUBLISHED))) {
+          return theme('project_issue_issue_link', $node, $comment_id, $comment_number);
         }
       }
-      return $text;
+      // If we got this far there wasn't a valid comment number, so just link
+      // to the node instead.
+      return theme('project_issue_issue_link', $node);
+    }
   }
+  // If we haven't already returned a replacement, return the original text.
+  return $matches[0];
 }
 
 /**
