Index: libraries.api.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/libraries/libraries.api.php,v
retrieving revision 1.1
diff -u -p -r1.1 libraries.api.php
--- libraries.api.php	3 Apr 2010 18:55:10 -0000	1.1
+++ libraries.api.php	16 Jul 2010 10:22:20 -0000
@@ -48,7 +48,11 @@
  *     uncompressed/source variant, those can be defined here. Each key should
  *     describe the variant type, e.g. 'minified' or 'source'. Each value is an
  *     associative array of top-level properties that are entirely overridden by
- *     the variant, most often just 'files'. Variants can be version specific.
+ *     the variant, most often just 'files'. Additionally, each variant can
+ *     contain a 'variant callback' and a 'variant arguments' key, which should
+ *     return TRUE or FALSE, depending on whether the variant is available or
+ *     not. If ommitted, the variant is expected to always be available.
+ *     Variants can be version specific.
  *   - versions: (optional) An associative array of supported library versions.
  *     Naturally, external libraries evolve over time and so do their APIs. In
  *     case a library changes between versions, different 'files' may need to be
@@ -62,6 +66,8 @@
  *     the same notion as the top-level 'files' property. Each specified file
  *     should contain the full path to the file.
  *   Additional top-level properties can be registered as needed.
+ *
+ * @see hook_library()
  */
 function hook_libraries_info() {
   // The following is a full explanation of all properties. See below for more
@@ -122,6 +128,10 @@ function hook_libraries_info() {
             'skin/example.css',
           ),
         ),
+        'variant callback' => 'mymodule_check_variant',
+        'variant arguments' => array(
+          'variant' => 'minified',
+        ),
       ),
     ),
     // Optional, but usually required: Override top-level properties for later
@@ -203,9 +213,9 @@ function hook_libraries_info() {
     'download url' => 'http://tinymce.moxiecode.com/download.php',
     'path' => 'jscripts/tiny_mce',
     'version arguments' => array(
-      // It can be easier to parse the first chars of a minified file instead of
-      // doing a multi-line pattern matching in a source file. See 'lines' and
-      // 'cols' below.
+      // It can be easier to parse the first characters of a minified file
+      // instead of doing a multi-line pattern matching in a source file. See
+      // 'lines' and 'cols' below.
       'file' => 'jscripts/tiny_mce/tiny_mce.js',
       // Best practice: Document the actual version strings for later reference.
       // 2.x: this.majorVersion="2";this.minorVersion="1.3"
Index: libraries.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/libraries/libraries.module,v
retrieving revision 1.4
diff -u -p -r1.4 libraries.module
--- libraries.module	3 Apr 2010 18:55:10 -0000	1.4
+++ libraries.module	16 Jul 2010 10:22:20 -0000
@@ -188,7 +188,7 @@ function libraries_info($library = NULL)
  */
 function libraries_detect($libraries) {
   foreach ($libraries as $name => $library) {
-    libraries_detect_library(&$libraries[$name]);
+    libraries_detect_library($libraries[$name]);
   }
   return $libraries;
 }
@@ -210,18 +210,20 @@ function libraries_detect_library(&$libr
   }
   if (!file_exists($library['library path'])) {
     $library['error'] = t('%library could not be found.', array('%library' => $library['title']));
-    continue;
+    return;
   }
 
-  // Detect library version.
-  // Special handling for named arguments (single array).
-  if (!isset($library['version arguments'][0])) {
-    $library['version arguments'] = array($library['version arguments']);
+  // Since libraries_get_version() is library-agnostic, we need to pass
+  // the full filepath.
+  if ($library['version callback'] == 'libraries_get_version') {
+    $library['version arguments']['file'] = $library['library path'] . '/' . $library['version arguments']['file'];
   }
+
+  // Detect library version.
   $library['version'] = call_user_func_array($library['version callback'], $library['version arguments']);
   if (empty($library['version'])) {
     $library['error'] = t('The version of %library could not be detected.', array('%library' => $library['title']));
-    continue;
+    return;
   }
 
   // Determine to which supported version the installed version maps.
@@ -229,13 +231,13 @@ function libraries_detect_library(&$libr
     ksort($library['versions']);
     $version = 0;
     foreach ($library['versions'] as $supported_version => $version_properties) {
-      if (version_compare($library['installed version'], $supported_version, '>=')) {
+      if (version_compare($library['version'], $supported_version, '>=')) {
         $version = $supported_version;
       }
     }
     if (!$version) {
-      $library['error'] = t('The installed version %version of %library is not supported.', array('%version' => $library['installed version'], '%library' => $library['title']));
-      continue;
+      $library['error'] = t('The installed version %version of %library is not supported.', array('%version' => $library['version'], '%library' => $library['title']));
+      return;
     }
 
     // Apply version specific definitions and overrides.
@@ -243,10 +245,137 @@ function libraries_detect_library(&$libr
     unset($library['versions']);
   }
 
+  // Check each variant if it is installed.
+  if (!empty($library['variants'])) {
+    foreach ($library['variants'] as $variant => $info) {
+      // If no variant callback has been set, assume the variant to be
+      // installed.
+      $library['variants'][$variant]['installed'] = TRUE;
+      if (!empty($info['variant callback'])) {
+        $library['variants'][$variant]['installed'] = call_user_func_array($info['variant callback'], $info['version arguments']);
+        if (empty($library['variants'][$variant]['installed'])) {
+          $library['variants'][$variant]['error'] = t('The %variant variant of %library could not be found.', array('%variant' => $variant, '%library' => $library['title']));
+        }
+      }
+    }
+  }
+
   // If we end up here, the library should be usable.
   $library['installed'] = TRUE;
-  if (!empty($library['path'])) {
-    $library['library path'] .= '/' . $library['path'];
+}
+
+/**
+ * Loads a library.
+ *
+ * @param $library
+ *   The name of the library to load.
+ * @param $variant
+ *   The name of the variant to load.
+ */
+function libraries_load($library, $variant = NULL) {
+  $library = libraries_info($library);
+  libraries_detect_library($library);
+  libraries_load_files($library, $variant);
+}
+
+/**
+ * Loads a library's files.
+ *
+ * @param $library
+ *   The name of the library to load.
+ * @param $variant
+ *   The name of the variant to load.
+ */
+function libraries_load_files($library, $variant = NULL) {
+
+  if (!empty($variant)) {
+    if (!empty($library['variants'][$variant]['installed'])) {
+      $library = array_merge($library, $library['variants'][$variant]);
+    }
+  }
+
+  // Load integration files.
+  if (!empty($library['integration files'])) {
+    foreach ($library['integration files'] as $module => $files) {
+      libraries_load_files(array(
+        'files' => $files,
+        'library path' => drupal_get_path('module', $module),
+      ));
+    }
+  }
+
+  // Load both the JavaScript and the CSS files.
+  // The parameters for drupal_add_js() and drupal_add_css() require special
+  // handling.
+  foreach (array('js', 'css') as $type) {
+    foreach ($library['files'][$type] as $data => $options) {
+      // If the value is not an array, it's a filename and passed as first
+      // (and only) argument.
+      if (!is_array($options)) {
+        $data = $options;
+        $options = NULL;
+      }
+      // In some cases, the first parameter ($data) is an array. Arrays can't be
+      // passed as keys in PHP, so we have to get $data from the value array.
+      if (is_numeric($data)) {
+        $data = $options['data'];
+        unset($options['data']);
+      }
+      // Apply the default weight if the weight isn't explicitly given.
+      if (!isset($options['weight'])) {
+        $options['weight'] = ($type == 'js') ? JS_DEFAULT : CSS_DEFAULT;
+      }
+      call_user_func('drupal_add_' . $type, $data, $options);
+    }
+  }
+
+
+  // Load PHP files.
+  if ($files = $library['files']['php']) {
+    foreach ($files as $file) {
+      $file_path = isset($library['path']) ? $library['library path'] . '/' . $library['path'] . '/' . $file : $library['library path'] . '/' . $file;
+      if (file_exists($file_path)) {
+        require_once($file_path);
+      }
+    }
+  }
+}
+
+/**
+ * Gets the version information from an arbitrary library.
+ *
+ * @param $file
+ *   The filename to parse for the version, relative to the Drupal root
+ *   directory.
+ * @param $pattern
+ *   A string containing a regular expression (PCRE) to match the library
+ *   version. For example: '/@version (\d+)\.(\d+)/'.
+ * @param $lines
+ *   The maximum number of lines to search the pattern in. For example: 20.
+ * @param $cols
+ *   (optional) The maximum number of characters per line to take into account.
+ *   For example: 40. Defaults to unlimited. To be used if the file containing
+ *   the library version is minified/compressed, i.e. reading a single line
+ *   would read the entire library into memory.
+ *
+ * @return
+ *   A string containing the version of the library.
+ *
+ * @see libraries_get_path()
+ */
+function libraries_get_version($file, $pattern, $lines, $cols = NULL) {
+  if (!file_exists($file)) {
+    return;
+  }
+  $file = fopen($file, 'r');
+  $args = !empty($cols) ? array('handle' => $file, 'length' => $cols) : array('handle' => $file);
+  while ($lines && $line = call_user_func_array('fgets', $args)) {
+    if (preg_match($pattern, $line, $version)) {
+      fclose($file);
+      return $version[1];
+    } 
+    $lines--;
   }
+  fclose($file);
 }
 
