You are here

function sassy_pre_render in Sassy 7.3

Same name and namespace in other branches
  1. 7 sassy.module \sassy_pre_render()

Builds the SASS cache. Should only be invoked by drupal_render().

Parameters

$elements: A render array containing: '#items': The CSS items as returned by drupal_add_css() and altered by drupal_get_css(). '#group_callback': A function to call to group #items to enable the use of fewer tags by aggregating files and/or using multiple @import statements within a single tag. '#aggregate_callback': A function to call to aggregate the items within the groups arranged by the #group_callback function.

Return value

$elements The modified (pre-rendered) $elements parameter.

1 string reference to 'sassy_pre_render'
sassy_element_info_alter in ./sassy.module
Implementation of hook_element_info_alter().

File

./sassy.module, line 68
Handles compiling of .sass / .scss files.

Code

function sassy_pre_render($elements) {
  $map = $original = variable_get('sassy_cache', array());
  $devel = variable_get('sassy_devel', FALSE);
  foreach ($elements['#items'] as $key => $file) {
    if ($file['type'] == 'file' && in_array(drupal_substr($file['data'], -5), array(
      '.scss',
      '.sass',
    ))) {

      // If the file is set to recompile on every page load then we don't want
      // it to be aggregated.
      $file['recompile'] = isset($file['recompile']) ? $file['recompile'] : FALSE;
      $file['preprocess'] = !empty($file['recompile']) ? FALSE : $file['preprocess'];

      // Create a unique identifier for the file.
      if ($file['recompile'] !== TRUE) {
        $hash = hash('sha256', serialize($file));
        $path = isset($map[$hash]) ? $map[$hash] : NULL;
      }

      // We recompile this file if recompile equals TRUE, array (and thereby the
      // hash value) changed, if the file doesn't exist, or if we are in development
      // mode. NOTE: You can use the 'recompile' array for your CSS files to cache
      // them based on advanced criteria.
      if ($devel || $file['recompile'] === TRUE || !isset($path) || !file_exists($path)) {
        $syntax = drupal_substr($file['data'], -4);
        if (!($output = sassy_parse($file['data'], $syntax, $devel))) {
          unset($elements['#items'][$key]);
          continue;
        }
        $directory = 'public://sassy';
        $path = $directory . '/' . drupal_hash_base64($output) . '.css';

        // Create the CSS file.
        file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
        if (!file_exists($path) && !file_unmanaged_save_data($output, $path, FILE_EXISTS_REPLACE)) {
          unset($elements['#items'][$key]);
          continue;
        }
      }

      // Update the item in the stylesheets array.
      $elements['#items'][$key] = $file;
      $elements['#items'][$key]['data'] = $path;
      if ($file['recompile'] !== TRUE) {

        // Don't cache this item if it is set to recompile on every page load.
        $map[$hash] = $path;
      }
    }
  }

  // If $map and $original don't match anymore that means we need to update the
  // CSS cache.
  if ($original !== $map) {
    variable_set('sassy_cache', $map);
  }
  return $elements;
}