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 12 Apr 2010 20:51:47 -0000 @@ -62,6 +62,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 @@ -203,9 +205,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 12 Apr 2010 20:51:48 -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; } @@ -250,3 +250,82 @@ function libraries_detect_library(&$libr } } +/** + * Loads a library. + * + * @param $library + * The name of the library to load. + */ +function libraries_load($library) { + $library = libraries_info($library); + libraries_detect_library($library); + + // 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'] = $weight; + } + call_user_func('drupal_add_' . $type, $data, $options); + } + } + + + // Load PHP files. + if ($files = $library['files']['php']) { + foreach ($files as $file) { + require_once($library['library path'] . $file); + } + } +} + +/** + * Gets the version information from an arbitrary library. + * + * @param $file + * The filename to parse for the version, relative to the library path. For + * example: 'docs/changelog.txt'. + * @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. + */ +function libraries_get_version($file, $pattern, $lines, $cols = NULL) { + if (!file_exists($file)) { + return; + } + $file = fopen($file, 'r'); + while ($lines && $line = fgets($file, $cols)) { + if (preg_match($pattern, $line, $version)) { + fclose($library); + return $version[1]; + } + $lines--; + } + fclose($library); +} +