示例#1
0
	function datauri($mimetypeNode, $filePathNode = null ) {

		$filePath = ( $filePathNode ? $filePathNode->value : null );
		$mimetype = $mimetypeNode->value;
		$useBase64 = false;

		$args = 2;
		if( !$filePath ){
			$filePath = $mimetype;
			$args = 1;
		}

		$filePath = str_replace('\\','/',$filePath);
		if( Less_Environment::isPathRelative($filePath) ){

			if( $this->relativeUrls ){
				$temp = $this->currentFileInfo['currentDirectory'];
			} else {
				$temp = $this->currentFileInfo['entryPath'];
			}

			if( !empty($temp) ){
				$filePath = Less_Environment::NormPath(rtrim($temp,'/').'/'.$filePath);
			}

		}


		// detect the mimetype if not given
		if( $args < 2 ){

			/* incomplete
			$mime = require('mime');
			mimetype = mime.lookup(path);

			// use base 64 unless it's an ASCII or UTF-8 format
			var charset = mime.charsets.lookup(mimetype);
			useBase64 = ['US-ASCII', 'UTF-8'].indexOf(charset) < 0;
			if (useBase64) mimetype += ';base64';
			*/

			$mimetype = Less_Mime::lookup($filePath);

			$charset = Less_Mime::charsets_lookup($mimetype);
			$useBase64 = !in_array($charset,array('US-ASCII', 'UTF-8'));
			if ($useBase64) $mimetype .= ';base64';

		}else{
			$useBase64 = preg_match('/;base64$/',$mimetype);
		}


		if( file_exists($filePath) ){
			$buf = @file_get_contents($filePath);
		}else{
			$buf = false;
		}


		// IE8 cannot handle a data-uri larger than 32KB. If this is exceeded
		// and the --ieCompat flag is enabled, return a normal url() instead.
		$DATA_URI_MAX_KB = 32;
		$fileSizeInKB = round( strlen($buf) / 1024 );
		if( $fileSizeInKB >= $DATA_URI_MAX_KB ){
			$url = new Less_Tree_Url( ($filePathNode ? $filePathNode : $mimetypeNode), $this->currentFileInfo);
			return $url->compile($this);
		}

		if( $buf ){
			$buf = $useBase64 ? base64_encode($buf) : rawurlencode($buf);
			$filePath = "'data:" . $mimetype . ',' . $buf . "'";
		}

		return new Less_Tree_Url( new Less_Tree_Anonymous($filePath) );
	}
示例#2
0
 function compile($env)
 {
     $evald = $this->compileForImport($env);
     $uri = $full_path = false;
     //get path & uri
     $evald_path = $evald->getPath();
     if ($evald_path && $env->isPathRelative($evald_path)) {
         foreach (Less_Parser::$import_dirs as $rootpath => $rooturi) {
             $temp = $rootpath . $evald_path;
             if (file_exists($temp)) {
                 $full_path = Less_Environment::NormPath($temp);
                 $uri = Less_Environment::NormPath(dirname($rooturi . $evald_path));
                 break;
             }
         }
     }
     if (!$full_path) {
         $uri = $evald_path;
         $full_path = $evald_path;
     }
     //import once
     $realpath = realpath($full_path);
     if (!isset($evald->options['multiple']) && $realpath && Less_Parser::FileParsed($realpath)) {
         $evald->skip = true;
     }
     $features = $evald->features ? $evald->features->compile($env) : null;
     if ($evald->skip) {
         return array();
     }
     if ($evald->css) {
         $temp = $this->compilePath($env);
         return new Less_Tree_Import($this->compilePath($env), $features, $this->options, $this->index);
     }
     $parser = new Less_Parser($env);
     $evald->root = $parser->parseFile($full_path, $uri, true);
     $ruleset = new Less_Tree_Ruleset(array(), $evald->root->rules);
     $ruleset->evalImports($env);
     return $this->features ? new Less_Tree_Media($ruleset->rules, $this->features->value) : $ruleset->rules;
 }
示例#3
0
	public function SetFileInfo( $filename, $uri_root = ''){

		$this->path = pathinfo($filename, PATHINFO_DIRNAME);
		$this->filename = Less_Environment::NormPath($filename);

		$dirname = preg_replace('/[^\/\\\\]*$/','',$this->filename);

		$currentFileInfo = array();
		$currentFileInfo['currentDirectory'] = $dirname;
		$currentFileInfo['filename'] = $filename;
		$currentFileInfo['rootpath'] = $dirname;
		$currentFileInfo['entryPath'] = $dirname;

		if( empty($uri_root) ){
			$currentFileInfo['uri_root'] = $uri_root;
		}else{
			$currentFileInfo['uri_root'] = rtrim($uri_root,'/').'/';
		}

		$this->env->currentFileInfo = $currentFileInfo;

		self::$import_dirs = array_merge( array( $dirname => $currentFileInfo['uri_root'] ), self::$import_dirs );
	}