public function compile(Filesystem $files, $source, $dest) { $scss = $files->get($source); $this->compiler->setImportPaths(array_merge([$this->getPath($source)], $this->importPaths)); $css = $this->compiler->compile($scss); $files->put($dest, $css); }
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)); }
/** * 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; }
/** * 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()))); }
/** * @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); } }
/** * 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; }
/** * Compile .scss file * * @param string $in Input path (.scss) * @param string $out Output path (.css) * * @return array */ protected function compile($in, $out) { $start = microtime(true); $css = $this->scss->compile(file_get_contents($in), $in); $elapsed = round(microtime(true) - $start, 4); $v = Compiler::$VERSION; $t = @date('r'); $css = "/* compiled by scssphp {$v} on {$t} ({$elapsed}s) */\n\n" . $css; $etag = md5($css); file_put_contents($out, $css); file_put_contents($this->metadataName($out), serialize(array('etag' => $etag, 'imports' => $this->scss->getParsedFiles()))); return array($css, $etag); }
/** * @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 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]); }
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()]; }
/** * @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); }
/** * {@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; }
public function filterLoad(AssetInterface $asset) { $sc = new Compiler(); if ($this->compass) { new \scss_compass($sc); } if ($dir = $asset->getSourceDirectory()) { $sc->addImportPath($dir); } foreach ($this->importPaths as $path) { $sc->addImportPath($path); } foreach ($this->customFunctions as $name => $callable) { $sc->registerFunction($name, $callable); } if ($this->formatter) { $sc->setFormatter($this->formatter); } if (!empty($this->variables)) { $sc->setVariables($this->variables); } $asset->setContent($sc->compile($asset->getContent())); }
/** * 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); }
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; } }
<?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));
public function compile($scss) { $compiler = new Compiler(); return $compiler->compile($scss); }
/** * 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)); } }
/** * 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); } }
echo $_POST['codingSpace']; } ?> </textarea> <button type="submit" name="submit" value="Draw CSS!" tabindex="2"></button> <?php use Leafo\ScssPhp\Compiler; if (isset($_POST['codingSpace'])) { $cS = $_POST['codingSpace']; require "compiler/scssphp/scss.inc.php"; $compressing = "Leafo\\ScssPhp\\Formatter\\Expanded"; $scss = new Compiler(); ini_set('display_errors', '0'); $scss->setFormatter($compressing); try { $outputH = $scss->compile($cS); echo "<pre id='outputSpace' name='outputSpace'>" . $outputH . "</pre>"; } catch (Exception $e) { echo "<div id='outputSpace' name='outputSpace'>" . $e->getMessage() . "</div>"; } } ?> </form> <hr /> Made with help from everyone :). Author: Sachin Kanungo | Fork or post issues: <a href="https://github.com/sachya/SCSS-Playground">Github-SCSS Playground</a> <script type="text/javascript" src="scripts/behave.js"></script> <script type="text/javascript" src="scripts/keyboard.js"></script> <script type="text/javascript"> var editor = new Behave({
<!-- Google Analytics Script <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX']); /* ID */ _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> --> <style> <?php echo $scss->compile('@import "test.scss";'); ?> </style> </head> <body> <!-- Logo --> <a href="http://hrn.io" title="Home"> <div id="HeaderFixedContainer"> <div id="HeaderLogoContainer" data-elemid="1"> <img class="HRNLogo" src="img/menu/hrn-logo.svg" alt="HRN"> <img class="HRNLogo" src="img/menu/hrn-logo.svg" alt="HRN"> <img class="HRNLogo" src="img/menu/hrn-logo.svg" alt="HRN"> </div>
/** * 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()); }