--- contrib\image_caption_filter.module	Wed Feb 03 11:44:27 2010
+++ contrib\image_caption_filter-new.module	Wed Apr 21 12:41:57 2010
@@ -30,6 +30,37 @@ function image_caption_filter_help($path
   return $output;
 } //function image_caption_filter_help
 
+
+function image_caption_filter_admin() {
+  $form = array();
+
+  $form['image_caption_filter_classes'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Classes to be searched for image captions'),
+    '#default_value' => variable_get('image_caption_filter_classes', 'image-left,image-right'),
+    '#size' => 80,
+    '#description' => t('Enter a comma-separated list of classes. The filter will only operate on images which have one of these CSS classes and have a title attribute and a width set.'),
+    '#required' => TRUE,
+  );
+
+  return system_settings_form($form);
+}
+
+function image_caption_filter_menu() {
+  $items = array();
+
+  $items['admin/settings/image_caption_filter'] = array(
+    'title' => 'Image caption filter',
+    'description' => t('Configure image caption filter settings.'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('image_caption_filter_admin'),
+    'access arguments' => array('administer site configuration'),
+    'type' => MENU_NORMAL_ITEM,
+   );
+
+  return $items;
+}
+
 //filter hook implementation
 function image_caption_filter_filter($op, $delta = 0, $format = -1, $text = '') {
   switch ($op) {
@@ -37,15 +68,15 @@ function image_caption_filter_filter($op
       return array(0 => t('Image caption filter'));
 
     case 'description':
-      return t('Adds captions to images via a filter');
+      return t('Adds captions to images via a filter.');
 
     case 'prepare':
       // Nothing to prepare
       return $text;
 
     case "process":
-      //Look for <img> tags and run the doImgTitles function on the <img> tag
-      $text = preg_replace_callback('|(<img.*?>)|s', 'doImgTitles', $text);
+      //Look for <img> tags and run the do_img_titles function on the <img> tag
+      $text = preg_replace_callback('|(<img.*?>)|s', 'do_img_titles', $text);
       return $text;
 
     default:
@@ -55,30 +86,46 @@ function image_caption_filter_filter($op
 
 
 //helper function to do the actual manipulation
-function doImgTitles($matches) {
-  $imgText = $matches[0];
-
-  //Get the title out of the <img> tag
-  preg_match ('/title=\"(.+?)\"/i', $imgText, $matches);
-  $title = $matches[1];
-
-  //Get the width out of the <img> tag
-  preg_match ('/width=\"(.+?)\"/i', $imgText, $matches);
-  $width = $matches[1];
-
-  //Get class out of the <img> tag
-  preg_match ('/class=\"(.+?)\"/i', $imgText, $matches);
+function do_img_titles($img_tag_matches) {
+  $img_tag = $img_tag_matches[0];
+  
+  //the classes we will operate on
+  $active_classes = variable_get("image_caption_filter_classes", 'image-left,image-right');
+  
+  //clean out whitespace and create array
+  $active_classes_cleaned = str_replace(' ', '', $active_classes);
+  $active_class_array = explode(',', $active_classes_cleaned);
+  
+  //get class out of the <img> tag
+  preg_match ('/class=\"(.+?)\"/i', $img_tag, $matches);
   $class = $matches[1];
-
-  //Only insert the caption and modify the <img> tag if it is has a title attribute and is one of the classes we are interested in
-  if (in_array($class, array('image-left', 'image-right', 'standalone-image')) && ($title)) {
-    $imgText = preg_replace ('/class=\"(.+?)\"/i', '', $imgText);
-    $returnText = "<div class=\"" . $class . "\" style=\"width: " . $width . "px\">"
-                  . $imgText 
-                  . "<div class=\"caption\">" . $title . "</div></div>";
-    return $returnText;
+  
+  //get the title out of the <img> tag
+  preg_match ('/title=\"(.+?)\"/i', $img_tag, $matches);
+  $title = $matches[1];
+  
+  //only modify the <img> tag if it is one of the classes we are interested in and has a title attribute
+  if (in_array($class, $active_class_array) && ($title)) {
+    //search for width specified as an inline style
+    if (preg_match ('/width:\s*(\d+)px/i', $img_tag, $matches) == 1) {
+      $width = $matches[1];
+      //else search for width specified as a width attribute
+    } else if (preg_match ('/width=\"(\d+?)\"/i', $img_tag, $matches) == 1) {
+      $width = $matches[1];
+    } else {
+      //no width specified, don't modify tag
+      return $img_tag;
+    }
+    
+    // remove the class from the image tag
+    $img_tag = preg_replace ('/class=\"(.+?)\"/i', '', $img_tag);
+    $return_text = "<span class=\"" . $class . "\" style=\"width: " . $width . "px\">"
+                    . $img_tag 
+                    . "<span class=\"caption\">" . $title . "</span></span>";
+    return $return_text;
   }
-  return $imgText;
-} //function doImgTitles
+  
+  return $img_tag;
+} //function do_img_titles
 
 ?>
