/** * @see ILess_Importer::getLastModified */ public function getLastModified($path, ILess_FileInfo $currentFileInfo) { $normalizedPath = ILess_Util::normalizePath($currentFileInfo->currentDirectory . $path); if (isset($this->files[$normalizedPath])) { $path = $normalizedPath; } if (isset($this->lastModified[$path])) { return $this->lastModified[$path]; } return false; }
/** * @see ILess_Node */ public function compile(ILess_Environment $env, $arguments = null, $important = null) { $value = $this->value->compile($env); $rootPath = isset($this->currentFileInfo) && $this->currentFileInfo->rootPath ? $this->currentFileInfo->rootPath : false; if ($rootPath && is_string($value->value) && ILess_Util::isPathRelative($value->value)) { $quoteExists = self::propertyExists($value, 'quote'); if ($quoteExists && empty($value->quote) || !$quoteExists) { $rootPath = preg_replace_callback('/[\\(\\)\'"\\s]/', array($this, 'rootPathReplaceCallback'), $rootPath); } $value->value = $rootPath . $value->value; } $value->value = ILess_Util::normalizePath($value->value, false); return new ILess_Node_Url($value, null); }
/** * Returns the debug information * * @param integer $index The index * @param string $input The input * @param ILess_Environment $env The environment * @return ILess_DebugInfo */ protected function getDebugInfo($index, $input, ILess_Environment $env) { list($lineNumber) = ILess_Util::getLocation($input, $index); return new ILess_DebugInfo($env->currentFileInfo->filename, $lineNumber); }
/** * Inlines a resource and falls back to url() if the ieCompat option is on * and the resource is too large, or if you use the function in the browser. * If the mime is not given then node uses the mime package to determine the correct mime type. * * @param string $mimeType A mime type string * @param string $url The URL of the file to inline. */ public function dataUri(ILess_Node $mimeType, ILess_Node $filePath = null) { if (func_num_args() < 2) { $path = $mimeType->value; $mime = false; // we will detect it later } else { $path = $filePath->value; $mime = $mimeType->value; } $path = ILess_Util::sanitizePath($path); if (ILess_Util::isPathRelative($path)) { if ($this->env->relativeUrls) { $path = $this->env->currentFileInfo->currentDirectory . $path; } else { $path = $this->env->currentFileInfo->entryPath . $path; } $path = ILess_Util::normalizePath($path); } if ($mime === false) { $mime = ILess_Mime::lookup($path); // use base 64 unless it's an ASCII or UTF-8 format $charset = ILess_Mime::charsetsLookup($mime); $useBase64 = !in_array($charset, array('US-ASCII', 'UTF-8')); if ($useBase64) { $mime .= ';base64'; } } else { $useBase64 = preg_match('/;base64$/', $mime); } $buffer = false; if (is_readable($path)) { $buffer = file_get_contents($path); } // IE8 cannot handle a data-uri larger than 32KB. If this is exceeded // and the --ieCompat option is enabled, return a normal url() instead. if ($this->env->ieCompat && $buffer !== false) { $fileSizeInKB = round(strlen($buffer) / 1024); if ($fileSizeInKB >= self::IE8_DATA_URI_MAX_KB) { $url = new ILess_Node_Url($filePath ? $filePath : $mimeType, $this->env->currentFileInfo); return $url->compile($this->env); } } if ($buffer !== false) { $buffer = $useBase64 ? base64_encode($buffer) : rawurlencode($buffer); $path = "'data:" . $mime . ',' . $buffer . "'"; } return new ILess_Node_Url(new ILess_Node_Anonymous($path)); }
/** * Generates unique cache key for given $filename * * @param string $filename * @return string */ protected function generateCacheKey($filename) { return ILess_Util::generateCacheKey($filename); }
/** * Compiles the path * * @param ILess_Environment $env * @return ILess_Node_Url */ public function compilePath(ILess_Environment $env) { $path = $this->path->compile($env); if (!$path instanceof ILess_Node_Url) { $rootPath = $this->currentFileInfo && $this->currentFileInfo->rootPath ? $this->currentFileInfo->rootPath : false; if ($rootPath) { $pathValue = $path->value; // Add the base path if the import is relative if ($pathValue && ILess_Util::isPathRelative($pathValue)) { $path->value = $rootPath . $pathValue; } } $path->value = ILess_Util::normalizePath($path->value); } return $path; }
/** * Returns file editor link. The link format can be customized. * * @param ILess_FileInfo|string $file The current file * @param integer $line * @return string|void * @see setFileEditorUrlFormat */ protected function getFileEditorLink($file, $line = null) { if ($file instanceof ILess_FileInfo) { $path = $file->filename; if ($file->importedFile) { $path = $file->importedFile->getPath(); } if (strpos($path, '__string_to_parse__') === 0) { $path = '[input string]'; } } else { $path = $file; } // when in cli or not accessible via filesystem, don't generate links if (PHP_SAPI == 'cli' || !ILess_Util::isPathAbsolute($path)) { return $path; } return sprintf('<a href="%s" class="file-edit">%s</a>', htmlspecialchars(strtr(self::$fileEditUrlFormat, array('%f' => $path, '%file' => $path, '%line' => $line, '%l' => $line))), $path); }
/** * Generates the CSS * * @param ILess_Environment $env * @return string */ public function generateCSS(ILess_Environment $env) { $output = new ILess_Output_Mapped($this->contentsMap, $this); // catch the output $this->root->generateCSS($env, $output); // prepare sources foreach ($this->contentsMap as $filename => $contents) { // match md5 hash in square brackets _[#HASH#]_ // see ILess_Parser_Core::parseString() if (preg_match('/(\\[__[0-9a-f]{32}__\\])+$/', $filename)) { $filename = substr($filename, 0, -38); } $this->sources[$this->normalizeFilename($filename)] = $contents; } $sourceMapUrl = null; if ($url = $this->getOption('url')) { $sourceMapUrl = $url; } elseif ($path = $this->getOption('filename')) { $sourceMapUrl = $this->normalizeFilename($path); // naming conventions, make it foobar.css.map if (!preg_match('/\\.map$/', $sourceMapUrl)) { $sourceMapUrl = sprintf('%s.map', $sourceMapUrl); } } $sourceMapContent = $this->generateJson(); // write map to a file if ($file = $this->getOption('write_to')) { // FIXME: should this happen here? $this->saveMap($file, $sourceMapContent); } else { $sourceMapUrl = sprintf('data:application/json,%s', ILess_Util::encodeURIComponent($sourceMapContent)); } if ($sourceMapUrl) { $output->add(sprintf('/*# sourceMappingURL=%s */', $sourceMapUrl)); } return $output->toString(); }
/** * Sets current file * * @param string $file The path to a file */ public function setCurrentFile($file) { $file = ILess_Util::normalizePath($file); $dirname = preg_replace('/[^\\/\\\\]*$/', '', $file); $this->currentFileInfo = new ILess_FileInfo(array('currentDirectory' => $dirname, 'filename' => $file, 'rootPath' => $this->currentFileInfo && $this->currentFileInfo->rootPath ? $this->currentFileInfo->rootPath : $this->rootPath, 'entryPath' => $dirname)); }
/** * @covers normalizePath * @dataProvider getDataForNormalizePathTest */ public function testNormalizePath($path, $expected) { $this->assertEquals(ILess_Util::normalizePath($path), $expected); }
/** * Runs the task based on the arguments * * @return true|integer True on success, error code on failure */ public function run() { if (!$this->isValid()) { echo $this->getUsage(); // return error return 1; } elseif ($this->getOption('version')) { echo ILess_Parser::VERSION . PHP_EOL; return; } elseif ($this->getOption('help')) { echo $this->getUsage(); return; } try { $parser = new ILess_Parser($this->prepareOptionsForTheParser()); $toBeParsed = $this->cliArguments['arguments'][0]; // read from stdin if (in_array($toBeParsed, $this->stdAliases)) { $content = file_get_contents('php://stdin'); $parser->parseString($content); } else { if (!ILess_Util::isPathAbsolute($toBeParsed)) { $toBeParsed = sprintf('%s/%s', $this->currentDir, $toBeParsed); } $parser->parseFile($toBeParsed); } $toBeSavedTo = null; if (isset($this->cliArguments['arguments'][1])) { $toBeSavedTo = $this->cliArguments['arguments'][1]; if (!ILess_Util::isPathAbsolute($toBeSavedTo)) { $toBeSavedTo = sprintf('%s/%s', $this->currentDir, $toBeSavedTo); } } $css = $parser->getCSS(); // where to put the css? if ($toBeSavedTo) { // write the result $this->saveCSS($toBeSavedTo, $css, $this->getOption('append')); } else { echo $css; } } catch (Exception $e) { if (!$this->getOption('silent')) { $this->renderException($e); } return $e->getCode(); } return true; }
/** * Constructor * * @param string $path The absolute path or URL * @param string $content The content of the local or remote file * @param integer $lastModified The last modification time */ public function __construct($path, $content, $lastModified) { $this->path = $path; $this->content = ILess_Util::normalizeLineFeeds($content); $this->lastModified = $lastModified; }