From e2c9713d7ccf06a9c6acd1fff9f52657cc8fea9f Mon Sep 17 00:00:00 2001
From: flow <f.weber@digi-info.de>
Date: Fri, 18 Feb 2011 17:33:30 +0100
Subject: [PATCH] d7 port.

---
 contrib/image_caption_filter.info   |    4 +-
 contrib/image_caption_filter.module |  110 ++++++++++++++++++++---------------
 image_caption.info                  |    4 +-
 3 files changed, 65 insertions(+), 53 deletions(-)

diff --git contrib/image_caption_filter.info contrib/image_caption_filter.info
index ed94890..989087e 100755
--- contrib/image_caption_filter.info
+++ contrib/image_caption_filter.info
@@ -1,5 +1,5 @@
 ; $Id$
 name = Image caption filter
 description = Create captions on images using the title attribute.
-core = 6.x
-version = 6.x-1.x-dev
+core = 7.x
+files[] = image_caption_filter.module
\ No newline at end of file
diff --git contrib/image_caption_filter.module contrib/image_caption_filter.module
index 20bf7c5..8d96a91 100755
--- contrib/image_caption_filter.module
+++ contrib/image_caption_filter.module
@@ -20,65 +20,79 @@
 * @param arg array that holds the current path as would be returned from arg() function
 * @return help text for the path
 */
-function image_caption_filter_help($path, $arg) {
-  $output = '';
-  switch ($path) {
-    case "admin/help#image_caption_filter":
-      $output = '<p>' .  t("Adds captions to images") . '</p>';
-      break;
-  }
-  return $output;
-} //function image_caption_filter_help
-
-//filter hook implementation
-function image_caption_filter_filter($op, $delta = 0, $format = -1, $text = '') {
-  switch ($op) {
-    case 'list':
-      return array(0 => t('Image caption filter'));
-
-    case 'description':
-      return t('Adds captions to images via a filter');
-
-    case 'prepare':
-      // Nothing to prepare
-      return $text;
+function _image_caption_filter_tips($filter, $format, $long = FALSE) {
+  return '<p>' . t("Adds captions to images") . '</p>';
+}
 
-    case "process":
-      //Look for <img> tags and run the doImgTitles function on the <img> tag
-      $text = preg_replace_callback('|(<img.*?>)|s', 'doImgTitles', $text);
-      return $text;
+/**
+ * Implements hook_filter_info().
+ */
+function image_caption_filter_filter_info() {
+  $filters['filter_image_caption'] = array(
+    'title' => t('Image caption filter'),
+    'description' => t('Adds captions to images via a filter'),
+    'process callback' => '_filter_image_caption_filter_process',
+    'tips callback' => '_image_caption_filter_tips',
+  );
+ 
+  return $filters;
+}
 
-    default:
-      return $text;
-  }
-} //function image_caption_filter_filter
+function _filter_image_caption_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
+  $text = preg_replace_callback('|(<img.*?>)|s', 'doImgTitles', $text);
 
+  return $text;
+}
 
 //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 title out of the <img> tag.
+  preg_match('/title=\"(.+?)\"/i', $imgText, $matches);
+  $title = isset($matches[1]) ? $matches[1] :  '';
 
-  //Get the width out of the <img> tag
-  preg_match ('/width=\"(.+?)\"/i', $imgText, $matches);
-  $width = $matches[1];
+  // Get the width out of the <img> tag.
+  preg_match('/width=\"(.+?)\"/i', $imgText, $matches);
+  $width = isset($matches[1]) ? $matches[1] :  '';
 
-  //Get class out of the <img> tag
-  preg_match ('/class=\"(.+?)\"/i', $imgText, $matches);
-  $class = $matches[1];
+  // Get class out of the <img> tag.
+  preg_match('/class=\"(.+?)\"/i', $imgText, $matches);
+  $class = isset($matches[1]) ? $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;
+  if (in_array($class, variable_get('image_caption_class', array('image-left', 'image-right', 'standalone-image'))) && ($title)) {
+    $imgText = preg_replace('/class=\"(.+?)\"/i', '', $imgText);
+    
+    $caption = array(
+      'img' => array(
+        '#type' => 'markup',
+        '#markup' => $imgText,
+      ),
+      'caption' => array(
+        '#theme' => 'container',
+        '#attributes' => array(
+          'class' => 'caption',
+        ),
+        '#children' => $title,
+      ),
+    );
+    
+    $element = array(
+      'image_caption' => array(
+        '#theme' => 'container',
+        '#attributes' => array(
+          'class' => $class
+        ),
+        '#children' => render($caption),
+      ),
+    );
+    
+    if (!empty($width)) {
+      $element['image_caption']['#attributes']['style'] = 'width: ' . $width . 'px;';
+    }
+
+    return render($element);
   }
   return $imgText;
-} //function doImgTitles
-
-?>
+}
diff --git image_caption.info image_caption.info
index 2253580..5cf009b 100644
--- image_caption.info
+++ image_caption.info
@@ -1,6 +1,4 @@
 ;$Id;
 name = "Image Caption"
 description = "Provides a caption for images using jquery."
-core = "6.x"
-php = "4.7"
-version = "6.x-1.0"
\ No newline at end of file
+core = "7.x"
\ No newline at end of file
-- 
1.7.3.4

