public function setProductionMode($isProduction) { if ($isProduction) { $this->compiler->setLineNumberStyle(0); $this->compiler->setFormatter(CompressedOutput::class); } }
/** * @param LeafoCompiler $compiler * @return LeafoScssProcessor */ public function setCompiler(LeafoCompiler $compiler) { $compiler->setImportPaths($this->importPaths); $compiler->setFormatter($this->getFormatter()); $compiler->setLineNumberStyle($this->getLineNumberStyle()); $this->compiler = $compiler; return $this; }
/** * Filters an asset just before it's dumped. * * @param AssetInterface $asset */ public function filterDump(AssetInterface $asset) { $this->dispatch(new LoadThemeVariables($variables = new Collection())); $compiler = new Compiler(); if ($dir = $asset->getSourceDirectory()) { $compiler->addImportPath($dir); } $compiler->setVariables($variables->all()); $asset->setContent($this->parser->parse($compiler->compile($asset->getContent()))); }
public function get($cssFile) { $scssPath = PROJECT_ROOT . '/src/scss'; $scssFile = $scssPath . '/' . str_replace('css', 'scss', $cssFile); /** Pass the route if file don't exists, will result in 404 */ if (!file_exists($scssFile)) { return $this->slim->pass(); } $this->slim->response->headers->set('Content-Type', 'text/css'); $this->compiler->addImportPath($scssPath); echo $this->compiler->compile(file_get_contents($scssFile)); }
public function getBlocks() { $parameters = $this->request->route()->parameters(); $scssSource = file_get_contents(public_path() . $this->request->input('themeFolder') . $parameters['theme'] . "/style.scss"); // complile scss $scss = new Compiler(); $scss->setVariables($this->request->input('settings')); $css = $scss->compile($scssSource); $header = file_get_contents(public_path() . $this->request->input('themeFolder') . $parameters['theme'] . "/header.html"); $footer = file_get_contents(public_path() . $this->request->input('themeFolder') . $parameters['theme'] . "/footer.html"); // include css in header $header = str_replace('/* --INCLUDE CSS-- */', $css, $header); return response()->json(['status' => 'success', 'header' => $header, 'footer' => $footer]); }
/** * @param array $sourceFiles * @param string $outputFile * @return array */ protected function compile($sourceFiles, $outputFile) { $parsedFiles = []; $file = fopen($outputFile, 'w'); foreach ($sourceFiles as $sourceFile) { $compiler = new Compiler(); $compiler->addImportPath(dirname($sourceFile)); fwrite($file, $compiler->compile(file_get_contents($sourceFile), $sourceFile) . "\n"); fflush($file); $parsedFiles = array_merge($parsedFiles, $compiler->getParsedFiles()); } fclose($file); return $parsedFiles; }
public function compile() { // go on even if user "stops" the script by closing the browser, closing the terminal etc. ignore_user_abort(true); // set script running time to unlimited set_time_limit(0); $root_dir = $this->root_dir; $scss_compiler = new Compiler(); $scss_compiler->setNumberPrecision(10); $scss_compiler->stripComments = $this->strip_comments; $scss_compiler->addImportPath(function ($path) use($root_dir) { $path = $root_dir . $path . '.scss'; $path_parts = pathinfo($path); $underscore_file = $path_parts['dirname'] . '/_' . $path_parts['basename']; if (file_exists($underscore_file)) { $path = $underscore_file; } if (!file_exists($path)) { return null; } return $path; }); // set the path to your to-be-imported mixins. please note: custom paths are coming up on future releases! //$scss_compiler->setImportPaths($scss_folder); // set css formatting (normal, nested or minimized), @see http://leafo.net/scssphp/docs/#output_formatting $scss_compiler->setFormatter($this->formatter); // get .scss's content, put it into $string_sass $string_sass = ''; if (is_array($this->scss_file)) { foreach ($this->scss_file as $scss_file) { $string_sass .= file_get_contents($scss_file); } } else { $string_sass = file_get_contents($this->scss_file); } // try/catch block to prevent script stopping when scss compiler throws an error try { // compile this SASS code to CSS $string_css = $scss_compiler->compile($string_sass) . "\n"; // $string_css = csscrush_string($string_css, $options = array('minify' => true)); // write CSS into file with the same filename, but .css extension file_put_contents($this->css_file, $string_css); } catch (Exception $e) { // here we could put the exception message, but who cares ... echo $e->getMessage(); exit; } }
/** * Compile the SCSS * @param \Contao\ThemeModel * @param boolean */ public static function compile(\Contao\ThemeModel $objTheme, $blnForce = false) { if (!self::confirmDependencies()) { return; } //Get file key $strKey = self::getKey($objTheme); //Set file path $strCSSPath = 'assets/foundation/css/' . $strKey . '.css'; //Compile the scss if (!file_exists(TL_ROOT . '/' . $strCSSPath) || $blnForce) { //Gather up the SCSS files in the assets/foundation/scss folder //This allows to work with different configs and edit defaults //Without affecting the original source $strBasePath = COMPOSER_DIR_RELATIVE . '/vendor/zurb/foundation/scss'; $strCopyPath = 'assets/foundation/scss/' . $strKey; //Create new folder if not exists and clean it out $objNew = new \Folder($strCopyPath); $objNew->purge(); $objOriginal = new \Folder($strBasePath); $objOriginal->copyTo($strCopyPath); //Apply the config self::applyConfig($objTheme, $strCopyPath); $strFoundationCSS = ''; $strNormalizeCSS = ''; //Create the SCSS compiler if (class_exists('scssc')) { $objCompiler = new \scssc(); $objCompiler->setImportPaths(TL_ROOT . '/' . $strCopyPath); $objCompiler->setFormatter(\Config::get('debugMode') ? 'scss_formatter' : 'scss_formatter_compressed'); } else { $objCompiler = new Compiler(); $objCompiler->setImportPaths(TL_ROOT . '/' . $strCopyPath); $objCompiler->setFormatter(\Config::get('debugMode') ? 'Leafo\\ScssPhp\\Formatter\\Expanded' : 'Leafo\\ScssPhp\\Formatter\\Compressed'); } $strFoundationContent = file_get_contents(TL_ROOT . '/' . $strCopyPath . '/foundation.scss'); $strNormalizeContent = file_get_contents(TL_ROOT . '/' . $strCopyPath . '/normalize.scss'); //Compile $strFoundationCSS = $objCompiler->compile($strFoundationContent); $strNormalizeCSS = $objCompiler->compile($strNormalizeContent); //Write to single CSS file cache $objFile = new \File($strCSSPath); $objFile->write($strNormalizeCSS . "\n" . $strFoundationCSS); $objFile->close(); } return $strCSSPath; }
/** * @return string */ public function getFile() { // get file contents $file_contents = Utilities::getFile($this->file['path']); // Create scss parser $scss = new Compiler(); // set load path for imported files $scss->setImportPaths(dirname($this->file['path'])); // Parse file using direct file path $contents = $scss->compile($file_contents); // fix absolute file paths $contents = str_replace(array('../'), str_replace(ROOT, "", dirname($this->file['path'])) . '/../', $contents); // get parsed files and store in cache $this->cache->save($this->file['hash'] . "parsed_files", $scss->getParsedFiles()); // return css return $contents; }
/** * @param string $name * @param string $scss * @param string $css * @param mixed $style * * @dataProvider provideTests */ public function testTests($name, $scss, $css, $style) { static $init = false; if (!getenv('TEST_SCSS_COMPAT')) { if (!$init) { $init = true; $this->markTestSkipped('Define TEST_SCSS_COMPAT=1 to enable ruby scss compatibility tests'); } return; } $compiler = new Compiler(); $compiler->setFormatter('Leafo\\ScssPhp\\Formatter\\' . ($style ? ucfirst($style) : 'Nested')); $actual = $compiler->compile($scss); $this->assertEquals($css, $actual, $name); // TODO: need to fix this in the formatters //$this->assertEquals(trim($css), trim($actual), $name); }
/** * Get variable * * @api * * @param string $name * @param boolean $shouldThrow * @param \stdClass $env * * @return mixed */ public function get($name, $shouldThrow = true, $env = null) { try { return parent::get($name, $shouldThrow, $env); } catch (\Exception $e) { echo $e->getMessage() . "\n"; return ['string', '', ['']]; } }
/** * {@inheritdoc} */ public function compile($path, $relativePath) { $this->parsedFiles = array(); $scss = new Compiler(); $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\Compressed'); if (!empty($this->importDirs)) { $scss->setImportPaths($this->importDirs); } $content = $this->overrideVariables(); $content .= '@import "' . $relativePath . '";' . "\n"; $css = $scss->compile($content); $parsedFiles = array(); foreach ($scss->getParsedFiles() as $file => $time) { $parsedFiles[] = $file; } $this->parsedFiles = $parsedFiles; return $css; }
/** * @param FileContainer $file * * @return $this * @throws CompilerException */ protected function compileSCSS(FileContainer $file) { try { $this->scss->addImportPath(dirname($file->getInputPath())); $content = $this->scss->compile($file->getInputContent()); return $file->setOutputContent($content); } catch (ParserException $e) { throw new CompilerException($e->getMessage(), 1, $e); } }
/** * @param string $rootScssDirectory * @param TwigThemeConfig $themeConfig * @param string | null $formatter * @param string | null $cacheDir */ public function __construct($rootScssDirectory, TwigThemeConfig $themeConfig, $formatter = 'compressed', $cacheDir = null) { $scssImportPaths = $themeConfig->getScssImportPaths(); $scssCompiler = new Compiler(); $scssCompiler->setImportPaths($scssImportPaths); $this->salt = $formatter . $rootScssDirectory . json_encode($scssImportPaths); switch ($formatter) { case 'expanded': $scssCompiler->setFormatter(\Leafo\ScssPhp\Formatter\Expanded::class); break; case 'compact': $scssCompiler->setFormatter(\Leafo\ScssPhp\Formatter\Compact::class); break; case 'compressed': $scssCompiler->setFormatter(\Leafo\ScssPhp\Formatter\Compressed::class); break; } $this->server = new Server($rootScssDirectory, $cacheDir, $scssCompiler); $this->server->showErrorsAsCSS(true); }
/** * Compile .scss file * * @param string $in Input file (.scss) * @param string $out Output file (.css) optional * * @return string|bool */ public function compileFile($in, $out = null) { if (!is_readable($in)) { throw new \Exception('load error: failed to find ' . $in); } $pi = pathinfo($in); $this->scss->addImportPath($pi['dirname'] . '/'); $compiled = $this->scss->compile(file_get_contents($in), $in); if ($out !== null) { return file_put_contents($out, $compiled); } return $compiled; }
public static function compileFile(string $file, array $paths, string $formatter, bool $lineComments) : array { $location = static::resolveFile($file, $paths); $compiler = new Compiler(); $compiler->setImportPaths(\dirname($location)); $compiler->addImportPath(function (string $path) use($paths) { return static::resolveFile($path, $paths); }); $compiler->setFormatter($formatter); if ($lineComments) { $compiler->setLineNumberStyle(Compiler::LINE_COMMENTS); } $css = $compiler->compile(\file_get_contents($location), $location); return [$css, $compiler->getParsedFiles()]; }
protected function importFile($path, $out) { $this->importedStylesheets[] = $this->removeTlPath($path); // call "original" method return parent::importFile($path, $out); }
public function compileValue($value) { // Makes protected function public. return parent::compileValue($value); }
/** * Callback method called before filters are run * * Overriding to run the file through LESS/SCSS if need be. * Also want to fix any relative paths for images. * * @param string $originalFile * @param string $cacheFile * * @throws CompilationException */ protected function beforeFilter($originalFile, $cacheFile) { if ($this->isLess($originalFile)) { $less = new lessc(); try { $compiledLess = $less->cachedCompile($originalFile); } catch (\Exception $e) { throw new CompilationException('Error in LESS Compiler', 0, $e); } $compiledLess['compiled'] = $this->fixRelativePaths($compiledLess['compiled'], $originalFile); file_put_contents($cacheFile, serialize($compiledLess)); } elseif ($this->isScss($originalFile)) { $scss = new ScssCompiler(); $scss->addImportPath(pathinfo($originalFile, PATHINFO_DIRNAME)); try { $compiled = $scss->compile(file_get_contents($originalFile)); } catch (\Exception $e) { throw new CompilationException('Error in SCSS Compiler', 0, $e); } $content = compact('compiled'); $parsedFiles = $scss->getParsedFiles(); $parsedFiles[] = $originalFile; foreach ($parsedFiles as $file) { $content['files'][$file] = filemtime($file); } $content['compiled'] = $this->fixRelativePaths($content['compiled'], $originalFile); file_put_contents($cacheFile, serialize($content)); } else { $content = file_get_contents($originalFile); file_put_contents($cacheFile, $this->fixRelativePaths($content, $originalFile)); } }
/** * Constructor * * @param string $dir Root directory to .scss files * @param string $cacheDir Cache directory * @param \Leafo\ScssPhp\Compiler|null $scss SCSS compiler instance */ public function __construct($dir, $cacheDir = null, $scss = null) { $this->dir = $dir; if (!isset($cacheDir)) { $cacheDir = $this->join($dir, 'scss_cache'); } $this->cacheDir = $cacheDir; if (!is_dir($this->cacheDir)) { mkdir($this->cacheDir, 0755, true); } if (!isset($scss)) { $scss = new Compiler(); $scss->setImportPaths($this->dir); } $this->scss = $scss; $this->showErrorsAsCSS = false; }
public function getChildren(AssetFactory $factory, $content, $loadPath = null) { $sc = new Compiler(); $sc->addImportPath($loadPath); foreach ($this->importPaths as $path) { $sc->addImportPath($path); } $children = array(); foreach (CssUtils::extractImports($content) as $match) { $file = $sc->findImport($match); if ($file) { $children[] = $child = $factory->createAsset($file, array(), array('root' => $loadPath)); $child->load(); $children = array_merge($children, $this->getChildren($factory, $child->getContent(), $loadPath)); } } return $children; }
public function compile($scss) { $compiler = new Compiler(); return $compiler->compile($scss); }
/** * Compile .scss file * * @param string $filename Input path (.scss) * * @see Server::compile() * @return array meta data result of the compile */ private function compile($filename) { $start = microtime(true); $scss = new Compiler(); // set import path directory the input filename resides // otherwise @import statements will not find the files // and will treat the @import line as css import $scss->setImportPaths(dirname($filename)); $css = $scss->compile(file_get_contents($filename), $filename); $elapsed = round(microtime(true) - $start, 4); $v = Version::VERSION; $ts = date('r', $start); $css = "/* compiled by scssphp {$v} on {$ts} ({$elapsed}s) */\n\n" . $css; $imports = $scss->getParsedFiles(); $updated = 0; foreach ($imports as $mtime) { $updated = max($updated, $mtime); } return array('elapsed' => $elapsed, 'updated' => $updated, 'content' => $css, 'files' => $imports); }
<?php namespace Leafo\ScssPhp; use Leafo\ScssPhp\Server; use Leafo\ScssPhp\Compiler; require "scss.inc.php"; $scss_folder = '../../scss/'; $scss = new Compiler(); $scss->setImportPaths($scss_folder); $filename = $scss_folder . $_GET['filename'] . '.scss'; //the name argument is optional $scss->setLineNumbers(true, $filename); header("Content-type: text/css"); echo $scss->compile(file_get_contents($filename));
/** * Output number * * @param \Leafo\ScssPhp\Compiler $compiler * * @return string */ public function output(Compiler $compiler = null) { $dimension = round($this->dimension, self::$precision); $units = array_filter($this->units, function ($unitSize) { return $unitSize; }); // @todo refactor normalize() if (count($units) > 1 && array_sum($units) === 0) { $dimension = $this->dimension; $units = array(); $this->normalizeUnits($dimension, $units, 'in'); $dimension = round($dimension, self::$precision); $units = array_filter($units, function ($unitSize) { return $unitSize; }); } $unitSize = array_sum($units); if ($compiler && ($unitSize > 1 || $unitSize < 0 || count($units) > 1)) { $compiler->throwError((string) $dimension . $this->unitStr() . " isn't a valid CSS value."); } reset($units); list($unit, ) = each($units); return (string) $dimension . $unit; }
/** * Convert the input data. * * @param string $input The raw content without Front-matter. * * @return string */ public function convert($input) { $scss = new Compiler(); $scss->setFormatter('Leafo\\ScssPhp\\Formatter\\Crunched'); return $scss->compile($input); }
/** * Handle SCSS/LESS files * * @param string $content The file content * @param array $arrFile The file array * * @return string The modified file content */ protected function handleScssLess($content, $arrFile) { if ($arrFile['extension'] == self::SCSS) { $objCompiler = new Compiler(); $objCompiler->setImportPaths(array(TL_ROOT . '/' . dirname($arrFile['name']), TL_ROOT . '/vendor/contao-components/compass/css')); $objCompiler->setFormatter(\Config::get('debugMode') ? 'Leafo\\ScssPhp\\Formatter\\Expanded' : 'Leafo\\ScssPhp\\Formatter\\Compressed'); return $this->fixPaths($objCompiler->compile($content), $arrFile); } else { $strPath = dirname($arrFile['name']); $arrOptions = array('strictMath' => true, 'compress' => !\Config::get('debugMode'), 'import_dirs' => array(TL_ROOT . '/' . $strPath => $strPath)); $objParser = new \Less_Parser(); $objParser->SetOptions($arrOptions); $objParser->parse($content); return $this->fixPaths($objParser->getCss(), $arrFile); } }
<?php use Leafo\ScssPhp\Server; use Leafo\ScssPhp\Compiler; require_once 'PHP/vendor/autoload.php'; $scss = new Compiler(); $scss->setImportPaths('sass/'); $scss->setFormatter("scss_formatter_compressed"); ?> <!doctype html> <html> <head> <meta name="designer" content="Designed by: Adam Dragus - adam@hrneurope.com "> <meta name="developer" content="Developed by: TesseracT - bottyan.tamas@web-developer.hu | Benedek Nagy - trialshock@gmail.com | Balazs Pentek - myrrdhinn@gmail.com"> <meta name="author" content="HRN - Human Resources Network"> <meta name="keywords" content="HR Tech, HRN"> <meta charset="utf-8" /> <meta name="viewport" content="initial-scale=1, maximum-scale=1"> <title>HRN | About</title> <!-- Open Graph data <meta property="og:site_name" content="HRN"/> <meta property="og:title" content="HRN | The Pan European HR Network"/> <meta property="og:description" content="HRN – Unleash Your People!"/> <meta property="og:url" content="http://hrn.io/about"> <meta property="og:type" content="website"/> <meta property="og:image" content="http://hrn.io/img/preview-images/preview-image-1.jpg" /> <meta property="og:image" content="http://hrn.io/img/preview-images/preview-image-3.jpg" /> <meta property="og:image" content="http://hrn.io/img/preview-images/preview-image-2.jpg" /> -->
/** * Returns the URL to the less/css-compiled file. * * @return string */ private function getLessUrl() { $p = $this->framework->getRootDir() . "static/" . $this->module->getPathName() . "/css/"; if (!$this->filesystem->exists($p)) { $this->filesystem->mkdir($p); } $cssfile = $p . "all.css"; $cssurl = $this->rootUrl . "static/" . $this->module->getPathName() . "/css/all.css"; // in case we have it in cache just return the url if ($this->filesystem->exists($cssfile) && !$this->isCacheDue()) { return $cssurl; } // if not go through all the dirs $dirs = $this->framework->getTemplateDirs(); // our output $allLess = ""; // paths with potential scss/less files $paths = array(); // go through all the dirs to find the all.less foreach ($dirs as $dir_) { $file = $dir_ . $this->module->getPathName() . "/_public/" . $this->framework->getTheme()->getCssCompiler() . "/all." . $this->framework->getTheme()->getCssCompiler(); if ($this->filesystem->exists($file)) { $paths[] = $dir_ . $this->module->getPathName() . "/_public/" . $this->framework->getTheme()->getCssCompiler() . "/"; $allLess = $file; break; } } // if there is none just return an empty string if ($allLess == "") { return ""; } try { $c = $this->framework->getTheme()->getCssCompiler(); if ($c == "less" || $c == "") { // the less parser $parser = new \Less_Parser(); $vars = array(); foreach ($this->getTemplateVars() as $k => $v) { if (!is_object($v) && !is_array($v)) { $vars[$k] = '\'' . $v . '\''; } } // are we minifying? \Less_Parser::$options['compress'] = $this->minifyCss; $parser->ModifyVars($vars); // parse our output $parser->parseFile($allLess, $this->rootUrl . 'static/' . $this->module->getPathName()); // and get it as css $css = $parser->getCss(); } else { if ($c == "scss") { $vars = array(); foreach ($this->getTemplateVars() as $k => $v) { if (!is_object($v) && !is_array($v)) { $vars[$k] = '' . $v . ''; } } $scss = new Compiler(); $scssData = file_get_contents($allLess); $scss->setImportPaths($paths); $scss->setVariables($vars); if ($this->minifyCss) { $scss->setFormatter("Leafo\\ScssPhp\\Formatter\\Crunched"); } else { $scss->setFormatter("Leafo\\ScssPhp\\Formatter\\Expanded"); } $css = $scss->compile($scssData); } else { if ($c == "css") { // TODO: Load all css files and just put them together in one file. } } } } catch (\Exception $e) { throw $e; } // remove the file if it exists if ($this->filesystem->exists($cssfile)) { $this->filesystem->remove($cssfile); } // save our output here $this->filesystem->dumpFile($cssfile, $css); // and return the url return $cssurl; }
/** * Compile the SCSS into CSS */ function _common_tinymce_compile_css() { require COMMON_TINYMCE_STYLES_PATH . '/vendor/autoload.php'; $scss = new Compiler(); $scss->setVariables(array('accent-color' => get_theme_mod('common_tinymce_button_color', '#999999'), 'alt-color' => get_theme_mod('common_tinymce_alt_button_color', '#CCCCCC'), 'message-color' => get_theme_mod('common_tinymce_message_color', '#BB0000'), 'warning-color' => get_theme_mod('common_tinymce_warning_color', '#CC0000'))); $css = file_get_contents(COMMON_TINYMCE_STYLES_PATH . '/assets/css/editor-styles.scss'); $css = $scss->compile($css); file_put_contents(COMMON_TINYMCE_STYLES_PATH . '/assets/css/editor-styles.css', $css); do_action('common_tinymce_compile_css', $scss); update_option('common_tinymce_styles_stylesheet', time()); }