You are here

function _less_rewrite_paths in Less CSS Preprocessor 7.2

Same name and namespace in other branches
  1. 8 includes/less.process.inc \_less_rewrite_paths()
  2. 7.4 includes/less.process.inc \_less_rewrite_paths()
  3. 7.3 less.module \_less_rewrite_paths()

Copied functionality from drupal_build_css_cache() for our own purposes.

This function processes $contents and rewrites relative paths to be absolute from web root. This is mainly used to ensure that compiled .less files still reference images at their original paths.

1 call to _less_rewrite_paths()
_less_pre_render in ./less.module
Processes .less files

File

./less.module, line 383
Handles compiling of .less files.

Code

function _less_rewrite_paths($input_filepath, $contents) {
  $output = '';

  // Build the base URL of this CSS file: start with the full URL.
  $css_base_url = file_create_url($input_filepath);

  // Move to the parent.
  $css_base_url = substr($css_base_url, 0, strrpos($css_base_url, '/'));

  // Simplify to a relative URL if the stylesheet URL starts with the
  // base URL of the website.
  if (substr($css_base_url, 0, strlen($GLOBALS['base_root'])) == $GLOBALS['base_root']) {
    $css_base_url = substr($css_base_url, strlen($GLOBALS['base_root']));
  }
  _drupal_build_css_path(NULL, $css_base_url . '/');

  // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths.
  $output .= preg_replace_callback('/url\\(\\s*[\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\s*\\)/i', '_drupal_build_css_path', $contents);
  return $output;
}