You are here

function sassy_pre_render in Sassy 7

Same name and namespace in other branches
  1. 7.3 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 57
Handles compiling of .sass / .scss files.

Code

function sassy_pre_render($elements) {
  $devel = variable_get('sassy_devel', FALSE);
  $map = $original = variable_get('sassy_cache', array());
  $files = sassy_pick_files($elements['#items']);

  // We can bail out here if there are no SCSS files anyways.
  if (empty($files['#stylesheets']) || !module_load_include('php', 'sassy', 'phamlp/sass/SassParser')) {

    // Remove the files from the array of stylesheets.
    $elements['#items'] = array_diff_key($elements['#items'], $files['#stylesheets']);
    return $elements;
  }
  foreach ($files['#stylesheets'] as $key => $file) {

    // Create a unique identifier for the file.
    $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($map[$hash]) || !file_exists($path)) {
      if (!isset($includes[$file['syntax']])) {
        $includes[$file['syntax']] = !empty($files['#includes'][$file['syntax']]) ? implode("\n\n", array_map('sassy_load_stylesheet', $files['#includes'][$file['syntax']])) : '';
      }
      $data = sassy_load_stylesheet($file['data']);
      $output = sassy_parse($file, $includes[$file['syntax']] . "\n\n" . $data, $file['syntax']);
      $directory = 'public://sassy';
      $file['data'] = $directory . '/' . drupal_hash_base64($output) . '.css';

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

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

      // Add this file to the cache if it is not set to recompile on every page load.
      $map[$hash] = $file['data'];
    }
  }

  // If $map and $original don't match anymore that means we need to update the
  // CSS cache.
  if ($original !== $map) {

    // Sort CSS items, so that they appear in the correct order.
    variable_set('sassy_cache', $map);
  }
  return $elements;
}