예제 #1
0
    /**
     * Takes a CSS string, rewrites all URL's using Scaffold's built-in find_file method
     *
     * @author Anthony Short
     * @param $css
     * @return $css string
     */
    public static function formatting_process()
    {
        # The absolute url to the directory of the current CSS file
        $dirPath = SCAFFOLD_DOCROOT . Scaffold::url_path(Scaffold::$css->path);
        $dir = rtrim(SCAFFOLD_URLPATH, '\\/') . '/' . str_replace(rtrim(SCAFFOLD_DOCROOT, '\\/') . DIRECTORY_SEPARATOR, '', Scaffold::$css->path);
        //$dir = str_replace('\\', '/', SCAFFOLD_URLPATH . str_replace(SCAFFOLD_DOCROOT, '', Scaffold::$css->path));
        # @imports - Thanks to the guys from Minify for the regex :)
        if (preg_match_all('/
			        @import\\s+
			        (?:url\\(\\s*)?      # maybe url(
			        [\'"]?               # maybe quote
			        (.*?)                # 1 = URI
			        [\'"]?               # maybe end quote
			        (?:\\s*\\))?         # maybe )
			        ([a-zA-Z,\\s]*)?     # 2 = media list
			        ;                    # end token
			    /x', Scaffold::$css->string, $found)) {
            foreach ($found[1] as $key => $value) {
                # Should we skip it
                if (self::skip($value)) {
                    continue;
                }
                $media = $found[2][$key] == "" ? '' : ' ' . preg_replace('/\\s+/', '', $found[2][$key]);
                # Absolute path
                $absolute = self::up_directory($dir, substr_count($url, '..' . DIRECTORY_SEPARATOR, 0)) . str_replace('..' . DIRECTORY_SEPARATOR, '', $url);
                $absolute = str_replace('\\', '/', $absolute);
                # Rewrite it
                # Webligo - PHP5.1 compat
                Scaffold::$css->string = str_replace($found[0][$key], '@import \'' . $absolute . '\'' . $media . ';', Scaffold::$css->string);
            }
        }
        # Convert all url()'s to absolute paths if required
        if (preg_match_all('/url\\(\\s*([^\\)\\s]+)\\s*\\)/', Scaffold::$css->__toString(), $found)) {
            foreach ($found[1] as $key => $value) {
                // START - Webligo Developments
                $original = $found[0][$key];
                $url = Scaffold_Utils::unquote($value);
                # Absolute Path
                if (self::skip($url)) {
                    continue;
                }
                # home path
                if ($url[0] == '~' && $url[1] == '/') {
                    $absolute = str_replace('\\', '/', rtrim(SCAFFOLD_URLPATH, '/\\') . '/' . ltrim($url, '~/'));
                    $absolutePath = rtrim(SCAFFOLD_DOCROOT, '/\\') . DIRECTORY_SEPARATOR . ltrim($url, '~/');
                } else {
                    $absolute = str_replace('\\', '/', self::up_directory($dir, substr_count($url, '..' . DIRECTORY_SEPARATOR, 0)) . str_replace('..' . DIRECTORY_SEPARATOR, '', $url));
                    $absolutePath = self::up_directory($dirPath, substr_count($url, '..' . DIRECTORY_SEPARATOR, 0)) . str_replace('..' . DIRECTORY_SEPARATOR, '', $url);
                }
                # If the file doesn't exist
                if (!Scaffold::find_file($absolutePath)) {
                    Scaffold::log("Missing image - {$absolute} / {$absolutePath}", 1);
                }
                # Rewrite it
                Scaffold::$css->string = str_replace($original, 'url(' . $absolute . ')', Scaffold::$css->string);
                # Webligo - PHP5.1 compat
                // END - Webligo Developments
            }
        }
    }
예제 #2
0
 /**
  * Imports css via @import statements
  * 
  * @author Anthony Short
  * @param $css
  */
 public static function server_import($css, $base)
 {
     if (preg_match_all('/\\@include\\s+(?:\'|\\")([^\'\\"]+)(?:\'|\\")\\;/', $css, $matches)) {
         $unique = array_unique($matches[1]);
         $include = str_replace("\\", "/", Scaffold_Utils::unquote($unique[0]));
         # If they haven't supplied an extension, we'll assume its a css file
         if (pathinfo($include, PATHINFO_EXTENSION) == "") {
             $include .= '.css';
         }
         # Make sure it's a CSS file
         if (pathinfo($include, PATHINFO_EXTENSION) != 'css') {
             $css = str_replace($matches[0][0], '', $css);
             Scaffold::log('Invalid @include file - ' . $include);
             self::server_import($css, $base);
         }
         # Find the file
         if ($path = Scaffold::find_file($include, $base)) {
             # Make sure it hasn't already been included
             if (!in_array($path, self::$loaded)) {
                 self::$loaded[] = $path;
                 $contents = file_get_contents($path);
                 # Check the file again for more imports
                 $contents = self::server_import($contents, realpath(dirname($path)) . '/');
                 $css = str_replace($matches[0][0], $contents, $css);
             } else {
                 $css = str_replace($matches[0][0], '', $css);
             }
         } else {
             Scaffold::error('Can\'t find the @include file - <strong>' . $unique[0] . '</strong>');
         }
         $css = self::server_import($css, $base);
     }
     return $css;
 }
예제 #3
0
/**
 * Parses enumerate()'s within the CSS. Enumerate
 * lets you build a selector based on a start and end
 * number. Like columns-1 through columns-12.
 *
 * enumerate('.name-',1,3) { properties }
 *
 * Will produce something like:
 *
 * .name-1, .name-2, .name-3 { properties }
 *
 * @param $string
 * @param $min
 * @param $max
 * @param $sep
 * @return string
 */
function Scaffold_enumerate($string, $min, $max, $sep = ",")
{
    $ret = array();
    $string = Scaffold_Utils::unquote($string);
    for ($i = $min; $i <= $max; $i++) {
        $ret[] = $string . $i;
    }
    return implode($sep, $ret);
}
예제 #4
0
/**
 * Embeds a file in the CSS using Base64
 *
 * @param $file
 * @return string
 */
function Scaffold_embed($file)
{
    $path = Scaffold::find_file(Scaffold_Utils::unquote($file));
    # Couldn't find it
    if ($path === false) {
        return false;
    }
    $data = 'data:image/' . pathinfo($path, PATHINFO_EXTENSION) . ';base64,' . base64_encode(file_get_contents($path));
    return "url(" . $data . ")";
}
예제 #5
0
    /**
     * Takes a CSS string, rewrites all URL's using Scaffold's built-in find_file method
     *
     * @author Anthony Short
     * @param $css
     * @return $css string
     */
    public static function formatting_process()
    {
        # The absolute url to the directory of the current CSS file
        $dir = Scaffold::url_path(Scaffold::$css->path);
        # @imports - Thanks to the guys from Minify for the regex :)
        if (preg_match_all('/
			        @import\\s+
			        (?:url\\(\\s*)?      # maybe url(
			        [\'"]?               # maybe quote
			        (.*?)                # 1 = URI
			        [\'"]?               # maybe end quote
			        (?:\\s*\\))?         # maybe )
			        ([a-zA-Z,\\s]*)?     # 2 = media list
			        ;                    # end token
			    /x', Scaffold::$css, $found)) {
            foreach ($found[1] as $key => $value) {
                # Should we skip it
                if (self::skip($value)) {
                    continue;
                }
                $media = $found[2][$key] == "" ? '' : ' ' . preg_replace('/\\s+/', '', $found[2][$key]);
                # Absolute path
                $absolute = self::up_directory($dir, substr_count($value, '..' . DIRECTORY_SEPARATOR, 0)) . str_replace('..' . DIRECTORY_SEPARATOR, '', $value);
                # Rewrite it
                Scaffold::$css->string = str_replace($found[0][$key], '@import \'' . $absolute . '\'' . $media . ';', Scaffold::$css);
            }
        }
        # Convert all url()'s to absolute paths if required
        if (preg_match_all('/url\\(\\s*([^\\)\\s]+)\\s*\\)/', Scaffold::$css, $found)) {
            foreach ($found[1] as $key => $value) {
                $url = Scaffold_Utils::unquote($value);
                # Absolute Path
                if (self::skip($url)) {
                    continue;
                }
                # Absolute path
                $absolute = self::up_directory($dir, substr_count($url, '..' . DIRECTORY_SEPARATOR, 0)) . str_replace('..' . DIRECTORY_SEPARATOR, '', $url);
                # If the file doesn't exist
                if (!Scaffold::find_file($absolute)) {
                    Scaffold::log("Missing image - {$absolute}", 1);
                }
                # Rewrite it
                Scaffold::$css->string = str_replace($found[0][$key], 'url(' . $absolute . ')', Scaffold::$css);
            }
        }
    }
예제 #6
0
 /**
  * Parses the parameters of the base
  *
  * @author Anthony Short
  * @param $params
  * @return array
  */
 public static function parse_params($mixin_name, $params, $function_args = array())
 {
     $parsed = array();
     # Make sure any commas inside ()'s, such as rgba(255,255,255,0.5) are encoded before exploding
     # so that it doesn't break the rule.
     if (preg_match_all('/\\([^)]*?,[^)]*?\\)/', $params, $matches)) {
         foreach ($matches as $key => $value) {
             $original = $value;
             $new = str_replace(',', '#COMMA#', $value);
             $params = str_replace($original, $new, $params);
         }
     }
     $mixin_params = $params != "" ? explode(',', $params) : array();
     # Loop through each function arg and create the parsed params array
     foreach ($function_args as $key => $value) {
         $v = explode('=', $value);
         # Remove the $ so we can set it as a constants
         $v[0] = str_replace('$', '', $v[0]);
         # If the user didn't include one of the params, we'll check to see if a default is available
         if (count($mixin_params) == 0 || !isset($mixin_params[$key])) {
             # If there is a default value for the param
             if (strstr($value, '=')) {
                 $parsed_value = Constants::replace(Scaffold_Utils::unquote(trim($v[1])));
                 $parsed[trim($v[0])] = (string) $parsed_value;
             } else {
                 throw new Exception("Missing mixin parameter - " . $mixin_name);
             }
         } else {
             $value = (string) Scaffold_Utils::unquote(trim($mixin_params[$key]));
             $parsed[trim($v[0])] = str_replace('#COMMA#', ',', $value);
         }
     }
     return $parsed;
 }
예제 #7
0
 function testUnquote()
 {
     $string = "'http://awesome.com'";
     $this->assertEqual(Scaffold_Utils::unquote($string), 'http://awesome.com');
 }