--- freelinking.module.old	2005-09-28 04:15:54.000000000 -0400
+++ freelinking.module	2005-09-29 16:10:06.000000000 -0400
@@ -124,53 +124,7 @@
       break;
 
     case 'process':
-// FIXME ***
-      $allowcamelcase = variable_get('freelinking_camelcase', TRUE);
-      $freelinkingregexp = '/\[\[.+]]/U'; // this finds [[links like this]], un-greedily
-      preg_match_all($freelinkingregexp, $text, $flmatches);
-      if ($allowcamelcase) {
-         $camelcaseregexp = '/\b([[:upper:]][[:lower:]]+){2,}\b/'; // this gets us close, but is not perfect. Example: ThisIsACamelCaseWord won't match (two caps in a row)
-         preg_match_all($camelcaseregexp, $text, $ccmatches);
-         $wikiwords = array_merge($ccmatches[0], $flmatches[0]);
-      }
-      else {
-         $wikiwords = $flmatches[0];
-      }
-      foreach (array_unique($wikiwords) as $wikiword) {
-        if (substr($wikiword, 0, 2) == '[[') { // if it's a freelink, the expressions are different
-          $phrase = substr($wikiword, 2, -2);
-          $freelink = $phrase;
-          $barpos = strpos($phrase, '|');
-          $pattern = '/\[\[' . preg_quote($phrase,'/') . ']]/';
-          if ($barpos) {
-             $freelink = substr($freelink, 0, $barpos);
-             $phrase = substr($phrase, $barpos + 1);
-          }
-          if (preg_match('/^(http|mailto|https|ftp):/', $freelink)) {
-             $replacement = '<a href="' . $freelink . '">' . $phrase . '</a>';
-          }
-          else {
-            $replacement = l($phrase, 'freelinking/' . rawurlencode(htmlspecialchars(rawurlencode($freelink))));
-          }
-        }
-
-        else if ($allowcamelcase) { // it's a CamelCase, expressions are a bit simpler
-          $pattern = '/(?![^<]a.*?>)\b' . $wikiword . '\b/';
-          $phrase = $wikiword; // consistency for the db
-          $freelink = $wikiword; // also for the db
-          $replacement = l($wikiword, 'freelinking/' . urlencode($wikiword));
-        }
-        $text = preg_replace($pattern, $replacement, $text);
-        
-//FIX should only store on node save, not preview.
-        $query = "SELECT phrase, target FROM {freelinking} WHERE phrase = '%s' AND target = '%s'";
-        if ( !db_num_rows(db_query($query, $phrase, $freelink))) { // not in the db
-          $query = "INSERT INTO {freelinking} (phrase, target) VALUES ('%s', '%s')";
-          $result = db_query($query, $phrase, $freelink);
-        } // endif row not found in table
-
-      } // foreach wikiword
-
+      $text = _freelinking_run_filtering($text, FALSE);
       return $text;
       break;
 
@@ -207,6 +161,21 @@
   return t($output);
 }
 
+/* hook_nodeapi implementation
+ *should be able to store new freelinks on saving of node rather than during the filter phase
+ *
+*/
+function freelinking_nodeapi (&$node, $op, $arg) {
+  switch ($op) { 
+    case 'update':
+      _freelinking_run_filtering ($node->body, TRUE);
+      break;
+
+    case 'insert':
+      _freelinking_run_filtering ($node->body, TRUE);
+      break;
+  }
+}
 
 /* 
  * PRIVATE FUNCTIONS BELOW
@@ -257,4 +226,60 @@
   return 'node/add/' . $nodetype . $delmiter . "edit[title]=$thetitle";
 } // endfunction _freelinking_create_new_link
 
+function _freelinking_run_filtering ($text = '', $store = FALSE) { // the actual filtering done here
+// FIXED? ***
+  $allowcamelcase = variable_get('freelinking_camelcase', TRUE);
+  $freelinkingregexp = '/\[\[.+\]\]/U'; // this finds [[links like this]], un-greedily
+  preg_match_all($freelinkingregexp, $text, $flmatches);
+  if ($allowcamelcase) {
+    $camelcaseregexp = '/\b([[:upper:]][[:lower:]]+){2,}\b/'; // this gets us close, but is not perfect. Example: ThisIsACamelCaseWord won't match (two caps in a row)
+    preg_match_all($camelcaseregexp, $text, $ccmatches);
+    $wikiwords = array_merge($flmatches[0], $ccmatches[0]);
+  }
+  else {
+     $wikiwords = $flmatches[0];
+  }
+  foreach (array_unique($wikiwords) as $wikiword) {
+    if (substr($wikiword, 0, 2) == '[[') { // if it's a freelink, the expressions are different
+      $phrase = substr($wikiword, 2, -2);
+      $freelink = $phrase;
+      $barpos = strpos($phrase, '|');
+      $pattern = '/\[\[' . preg_quote($phrase,'/') . ']]/';
+      if ($barpos) {
+         $freelink = substr($freelink, 0, $barpos);
+         $phrase = substr($phrase, $barpos + 1);
+      }
+      if (preg_match('/^(http|mailto|https|ftp):/', $freelink)) {
+         $replacement = '<a href="' . $freelink . '" class="freelink" >' . $phrase . '</a>';
+      }
+      else {
+        $replacement = l($phrase, 'freelinking/' . rawurlencode(htmlspecialchars(rawurlencode($freelink))), array('class' => 'freelink'));
+      }
+
+    }
+    else if ($allowcamelcase) { // it's a CamelCase, expressions are a bit simpler
+      $pattern = '/\b' . $wikiword . '\b(?![^<]*>)/';
+      if (!preg_match($pattern, $text)) {
+        continue;
+      }
+      else {
+      $phrase = $wikiword; // consistency for the db
+      $freelink = $wikiword; // also for the db
+      $replacement = l($wikiword, 'freelinking/' . urlencode($wikiword), array('class' => 'cammel case'));
+      }
+    }
+    $text = preg_replace($pattern, $replacement, $text);
+        
+    if ($store == TRUE) { // save if store is TRUE (only on saving)
+      $query = "SELECT phrase, target FROM {freelinking} WHERE phrase = '%s' AND target = '%s'";
+      if ( !db_num_rows(db_query($query, $phrase, $freelink))) { // not in the db
+        $query = "INSERT INTO {freelinking} (phrase, target) VALUES ('%s', '%s')";
+        $result = db_query($query, $phrase, $freelink);
+      } // endif row not found in table
+    } //endif !$store
+
+  } // foreach wikiword
+  return $text;   
+}
+
 // vim: tw=300 nowrap syn=php
