/** * {@inheritdoc} */ protected function _initCompiler() { if ($this->_compiler) { return $this->_compiler; } $this->_compiler = new \lessc(); if (class_exists('\\lessc_formatter_lessjs')) { $formatter = new \lessc_formatter_lessjs(); // configurate css view $formatter->openSingle = ' { '; $formatter->closeSingle = "}\n"; $formatter->close = "}\n"; $formatter->indentChar = ' '; $formatter->disableSingle = true; $formatter->breakSelectors = true; $formatter->assignSeparator = ': '; $formatter->selectorSeparator = ', '; $this->_compiler->setFormatter($formatter); } $this->_compiler->setPreserveComments(false); // Set paths $importPaths = (array) $this->_options->get('import_paths', []); foreach ($importPaths as $fullPath => $relPath) { $this->setImportPath($fullPath, $relPath); } // Set paths $this->_compiler->setVariables((array) $this->_options->get('global_vars', [])); return $this->_compiler; }
function CompileOptionsLess($inputFile) { global $dynamo_tpl; require_once get_template_directory() . '/dynamo_framework/lib/lessc.inc.php'; $less = new lessc(); $less->setPreserveComments(true); $url = "'" . get_template_directory_uri() . "'"; $body_bg_image = "'" . get_option($dynamo_tpl->name . '_body_bg_image') . "'"; $page404_bg_image = "'" . get_option($dynamo_tpl->name . '_page404_bg_image') . "'"; $subheader_area_bgimage = "'" . get_option($dynamo_tpl->name . '_subheader_area_bgimage') . "'"; $branding_logo_image = "'" . get_option($dynamo_tpl->name . '_branding_logo_image') . "'"; $expander_bgimage = "'" . get_option($dynamo_tpl->name . '_expander_bgimage') . "'"; $footer_bg_image = "'" . get_option($dynamo_tpl->name . '_footer_bg_image') . "'"; $expander_bgimage = "'" . get_option($dynamo_tpl->name . '_expander_bgimage') . "'"; $footerbgtype = 'n'; if (get_option($dynamo_tpl->name . '_footer_pattern', 'none') != 'none') { $footerbgtype = 'p'; } if (get_option($dynamo_tpl->name . '_footer_bg_image') != '') { $footerbgtype = 'i'; } $expanderbgtype = 'n'; if (get_option($dynamo_tpl->name . '_expander_pattern', 'none') != 'none') { $expanderbgtype = 'p'; } if (get_option($dynamo_tpl->name . '_expander_bgimage') != '') { $expanderbgtype = 'i'; } $less->setVariables(array("url" => $url, "fontsize_body" => get_option($dynamo_tpl->name . '_fontsize_body', '13px'), "fontsize_h1" => get_option($dynamo_tpl->name . '_fontsize_h1', '40px'), "fontsize_h2" => get_option($dynamo_tpl->name . '_fontsize_h2', '30px'), "fontsize_h3" => get_option($dynamo_tpl->name . '_fontsize_h3', '18px'), "fontsize_h4" => get_option($dynamo_tpl->name . '_fontsize_h4', '16px'), "fontsize_h5" => get_option($dynamo_tpl->name . '_fontsize_h5', '14px'), "fontsize_h6" => get_option($dynamo_tpl->name . '_fontsize_h6', '12px'), "maincontent_accent_color" => get_option($dynamo_tpl->name . '_maincontent_accent_color', '#3296dc'), "maincontent_secondary_accent_color" => get_option($dynamo_tpl->name . '_maincontent_secondary_accent_color', '#000000'), "page_wrap_state" => get_option($dynamo_tpl->name . '_page_wrap_state', 'streched'), "page_bgcolor" => get_option($dynamo_tpl->name . '_page_bgcolor', '#ffffff'), "page_pattern" => get_option($dynamo_tpl->name . '_page_pattern', 'none'), "body_bg_image_state" => get_option($dynamo_tpl->name . '_body_bg_image_state', 'N'), "body_bg_image" => $body_bg_image, "body_bgcolor" => get_option($dynamo_tpl->name . '_body_bgcolor', '#ffffff'), "body_pattern" => get_option($dynamo_tpl->name . '_body_pattern', 'none'), "paspartu_state" => get_option($dynamo_tpl->name . '_paspartu_state', 'N'), "paspartu_bg_color" => get_option($dynamo_tpl->name . '_paspartu_bg_color', '#ffffff'), "paspartu_width" => get_option($dynamo_tpl->name . '_paspartu_width', '30') . 'px', "top_bar_bg_color" => get_option($dynamo_tpl->name . '_top_bar_bg_color', '#000000'), "top_bar_text_color" => get_option($dynamo_tpl->name . '_top_bar_text_color', '#ffffff'), "top_bar_link_color" => get_option($dynamo_tpl->name . '_top_bar_link_color', '#3296dc'), "top_bar_hlink_color" => get_option($dynamo_tpl->name . '_top_bar_hlink_color', '#f2f2f2'), "top_bar_icon_color" => get_option($dynamo_tpl->name . '_top_bar_icon_color', '#ffffff'), "branding_logo_type" => get_option($dynamo_tpl->name . '_branding_logo_type'), "branding_logo_image" => $branding_logo_image, "branding_logo_image_width" => get_option($dynamo_tpl->name . '_branding_logo_image_width', '160') . 'px', "branding_logo_image_height" => get_option($dynamo_tpl->name . '_branding_logo_image_height', '50') . 'px', "branding_logo_top_margin" => get_option($dynamo_tpl->name . '_branding_logo_top_margin', '30') . 'px', "branding_logo_bottom_margin" => get_option($dynamo_tpl->name . '_branding_logo_bottom_margin', '10') . 'px', "sticky_logo_top_margin" => get_option($dynamo_tpl->name . '_sticky_logo_top_margin', '10') . 'px', "menu_top_bg_color" => get_option($dynamo_tpl->name . '_menu_top_bg_color', 'transparent'), "top_mainmenu_link_color" => get_option($dynamo_tpl->name . '_top_mainmenu_link_color', '#222222'), "top_mainmenu_hlink_color" => get_option($dynamo_tpl->name . '_top_mainmenu_hlink_color', '#3296dc'), "sticky_header_bgcolor" => get_option($dynamo_tpl->name . '_sticky_header_bgcolor', 'rgba(255,255,255,0.95)'), "sticky_mainmenu_link_color" => get_option($dynamo_tpl->name . '_sticky_mainmenu_link_color', '#222222'), "sticky_mainmenu_hlink_color" => get_option($dynamo_tpl->name . '_sticky_mainmenu_hlink_color', '#3296dc'), "aside_logo_image_width" => get_option($dynamo_tpl->name . '_aside_logo_image_width', '101') . 'px', "aside_logo_image_height" => get_option($dynamo_tpl->name . '_aside_logo_image_height', '35') . 'px', "aside_mainmenu_bg_color" => get_option($dynamo_tpl->name . '_aside_mainmenu_bg_color', '#ffffff'), "aside_mainmenu_link_color" => get_option($dynamo_tpl->name . '_aside_mainmenu_link_color', '#222222'), "aside_mainmenu_hlink_color" => get_option($dynamo_tpl->name . '_aside_mainmenu_hlink_color', '#3296dc'), "submenu_bgcolor" => get_option($dynamo_tpl->name . '_submenu_bgcolor', '#ffffff'), "submenu_topbordercolor" => get_option($dynamo_tpl->name . '_submenu_topbordercolor', '#3296dc'), "submenu_link_color" => get_option($dynamo_tpl->name . '_submenu_link_color', '#AFB4B9'), "submenu_hlink_color" => get_option($dynamo_tpl->name . '_submenu_hlink_color', '#AFB4B9'), "submenu_hbg_color" => get_option($dynamo_tpl->name . '_submenu_hbg_color', '#F6F6F6'), "overlay_mainmenu_bg_color" => get_option($dynamo_tpl->name . '_overlay_mainmenu_bg_color', '#ffffff'), "overlay_mainmenu_link_color" => get_option($dynamo_tpl->name . '_overlay_mainmenu_link_color', '#222222'), "overlay_mainmenu_hlink_color" => get_option($dynamo_tpl->name . '_overlay_mainmenu_hlink_color', '#3296dc'), "overlay_mainmenu_bg_color" => get_option($dynamo_tpl->name . '_overlay_mainmenu_bg_color', '#ffffff'), "overlay_mainmenu_link_color" => get_option($dynamo_tpl->name . '_overlay_mainmenu_link_color', '#222222'), "overlay_mainmenu_hlink_color" => get_option($dynamo_tpl->name . '_overlay_mainmenu_hlink_color', '#3296dc'), "overlapping_header_bgcolor" => get_option($dynamo_tpl->name . '_overlapping_header_bgcolor', '#ffffff'), "overlapping_mainmenu_link_color_light" => get_option($dynamo_tpl->name . '_overlapping_mainmenu_link_color_light', '#ffffff'), "overlapping_mainmenu_hlink_color_light" => get_option($dynamo_tpl->name . '_overlapping_mainmenu_hlink_color_light', '#3296dc'), "overlapping_mainmenu_link_color_dark" => get_option($dynamo_tpl->name . '_overlapping_mainmenu_link_color_dark', '#222222'), "overlapping_mainmenu_hlink_color_dark" => get_option($dynamo_tpl->name . '_overlapping_mainmenu_hlink_color_dark', '#3296dc'), "subheader_bgcolor" => get_option($dynamo_tpl->name . '_subheader_bgcolor', '#F7F8FA'), "subheader_pattern" => get_option($dynamo_tpl->name . '_subheader_pattern', 'none'), "subheader_area_bgimage" => $subheader_area_bgimage, "subheader_text_color" => get_option($dynamo_tpl->name . '_subheader_text_color', '#ffffff'), "expander_bgcolor" => get_option($dynamo_tpl->name . '_expander_bgcolor', '#222222'), "expander_bgimage" => $expander_bgimage, "expander_pattern" => get_option($dynamo_tpl->name . '_expander_pattern', 'none'), "expander_text_color" => get_option($dynamo_tpl->name . '_eexpander_text_color', '#ffffff'), "expander_link_color" => get_option($dynamo_tpl->name . '_expander_link_color', '#3296dc'), "expander_hlink_color" => get_option($dynamo_tpl->name . '_expander_hlink_color', '#f6f6f6'), "expanderbgtype" => $expanderbgtype, "maincontent_text_color" => get_option($dynamo_tpl->name . '_maincontent_text_color', '#7A7A7A'), "maincontent_headers_color" => get_option($dynamo_tpl->name . '_maincontent_headers_color', '#7A7A7A'), "maincontent_link_color" => get_option($dynamo_tpl->name . '_maincontent_link_color', '#3296dc'), "maincontent_hlink_color" => get_option($dynamo_tpl->name . '_maincontent_hlink_color', '#76797C'), "footer_bg_color" => get_option($dynamo_tpl->name . '_footer_bg_color', '#232D37'), "footer_bg_image" => $footer_bg_image, "footer_pattern" => get_option($dynamo_tpl->name . '_footer_pattern', 'none'), "footer_text_color" => get_option($dynamo_tpl->name . '_footer_text_color', '#BCC1C5'), "footer_header_color" => get_option($dynamo_tpl->name . '_footer_header_color', '#ffffff'), "footer_link_color" => get_option($dynamo_tpl->name . '_footer_link_color', '#ffffff'), "footer_hlink_color" => get_option($dynamo_tpl->name . '_footer_hlink_color', '#3296dc'), "footerbgtype" => $footerbgtype, "copyrightbgcolor" => get_option($dynamo_tpl->name . '_copyright_bg_color', 'rgba(0,0,0,.2)'), "copyrightbordercolor" => get_option($dynamo_tpl->name . '_copyright_border_color', 'rgba(0,0,0,0)'), "copyrighttextcolor" => get_option($dynamo_tpl->name . '_copyright_text_color', '#A2A2A2'), "copyrightlinkcolor" => get_option($dynamo_tpl->name . '_copyright_link_color', '#A2A2A2'), "copyrighthlinkcolor" => get_option($dynamo_tpl->name . '_copyright_hlink_color', '#fff'), "page404_bg_image" => $page404_bg_image, "page404_bg_image_state" => get_option($dynamo_tpl->name . '_404_bg_image_state', 'N'))); $less->compileFile(get_template_directory() . '/css/less/' . $inputFile, get_template_directory() . '/css/dynamic.css'); }
public static function compile($source, $path, $todir, $importdirs) { // call Less to compile $parser = new lessc(); $parser->setImportDir(array_keys($importdirs)); $parser->setPreserveComments(true); $output = $parser->compile($source); // update url $arr = preg_split(CANVASLess::$rsplitbegin . CANVASLess::$kfilepath . CANVASLess::$rsplitend, $output, -1, PREG_SPLIT_DELIM_CAPTURE); $output = ''; $file = $relpath = ''; $isfile = false; foreach ($arr as $s) { if ($isfile) { $isfile = false; $file = $s; $relpath = CANVASLess::relativePath($todir, dirname($file)); $output .= "\n#" . CANVASLess::$kfilepath . "{content: \"{$file}\";}\n"; } else { $output .= ($file ? CANVASPath::updateUrl($s, $relpath) : $s) . "\n\n"; $isfile = true; } } return $output; }
public static function compile($source, $importdirs) { // call Less to compile $parser = new lessc(); $parser->setImportDir(array_keys($importdirs)); $parser->setPreserveComments(true); $output = $parser->compile($source); return $output; }
/** * Compiles a less css file. The the compiler will create a css file output. * * @param array $config Array of less compile configuration */ public function compile($config = array()) { $config = new KConfig($config); $config->append(array('parse_urls' => true, 'compress' => true, 'import' => array(), 'force' => false, 'output' => null, 'input' => null)); $less = new lessc(); $less->setPreserveComments(!$config->compress); if ($config->compress) { $less->setFormatter('compressed'); } $config['import'] = $config['import']; $less->setImportDir($config['import']); $cache_file = JPATH_CACHE . '/less-' . md5($config->input); if (file_exists($cache_file)) { $cache = unserialize(file_get_contents($cache_file)); } else { $cache = $config->input; } $force = $config->force; //if output doesn't exsit then force compile if (!is_readable($config->output)) { $force = true; } //check if any of the import folder have changed or //if yes then re-compile if (is_array($cache)) { foreach ($config['import'] as $path) { if (is_readable($path) && filemtime($path) > $cache['updated']) { $force = true; break; } } } try { $new_cache = $less->cachedCompile($cache, $force); } catch (Exception $e) { print $e->getMessage(); return; } if (!is_array($cache) || $new_cache['updated'] > $cache['updated']) { if ($config->parse_urls) { $new_cache['compiled'] = $this->_parseUrls($new_cache['compiled'], $config->import); } //store the cache file_put_contents($cache_file, serialize($new_cache)); //store the compiled file //create a directory if if (!file_exists(dirname($config->output))) { mkdir(dirname($config->output), 0755); } file_put_contents($config->output, $new_cache['compiled']); } }
public function filterLoad(AssetInterface $asset) { $lc = new \lessc(); if ($dir = $asset->getSourceDirectory()) { $lc->importDir = $dir; } foreach ($this->loadPaths as $loadPath) { $lc->addImportDir($loadPath); } if ($this->formatter) { $lc->setFormatter($this->formatter); } if (null !== $this->preserveComments) { $lc->setPreserveComments($this->preserveComments); } $asset->setContent($lc->parse($asset->getContent(), $this->presets)); }
public function filterLoad(AssetInterface $asset) { $root = $asset->getSourceRoot(); $path = $asset->getSourcePath(); $lc = new \lessc(); if ($root && $path) { $lc->importDir = dirname($root . '/' . $path); } foreach ($this->loadPaths as $loadPath) { $lc->addImportDir($loadPath); } if ($this->formatter) { $lc->setFormatter($this->formatter); } if (null !== $this->preserveComments) { $lc->setPreserveComments($this->preserveComments); } $asset->setContent($lc->parse($asset->getContent(), $this->presets)); }
/** * Build the Less file * * @param string $dest The destination CSS file * @param bool $force If set to true, will build whereas the cache status * @param array $variables Less variables to set before compiling the Less file */ public function build($dest, $force = false, $variables = array()) { if (!is_dir(dirname($dest))) { mkdir(dirname($dest), 0755, true); } $compiler = new \lessc(); $lastCompilationFile = App::cache()->getCacheFilePath($this->getLastCompilationInfoFilename()); if (!$force && is_file($lastCompilationFile)) { $cache = (include $lastCompilationFile); } else { $cache = $this->source; } $compiler->setFormatter('compressed'); $compiler->setPreserveComments(false); $compilation = $compiler->cachedCompile($cache, $force); if (!is_array($cache) || $compilation['updated'] > $cache['updated']) { file_put_contents($dest, '/*** ' . date('Y-m-d H:i:s') . ' ***/' . PHP_EOL . $compilation['compiled']); $event = new Event('built-less', array('source' => $this->source, 'dest' => $dest)); $event->trigger(); // Save the compilation information unset($compilation['compiled']); $this->saveLastCompilationInfo($compilation); } }
/** * @return lessc */ protected function _initProcessor() { // lazy load if (!class_exists('lessc')) { require_once dirname(__FILE__) . '/class.less.leafo.php'; } $less = new lessc(); if ($this->_isDebug()) { $formatter = new lessc_formatter_lessjs(); // configurate css view $formatter->openSingle = " { "; $formatter->closeSingle = "}\n"; $formatter->close = "}\n"; $formatter->indentChar = " "; $formatter->disableSingle = true; $formatter->breakSelectors = true; $formatter->assignSeparator = ": "; $formatter->selectorSeparator = ", "; } else { // compress mode $formatter = new lessc_formatter_compressed(); $formatter->closeSingle = "}\n"; $formatter->close = "}\n"; } // set formatter $less->setFormatter($formatter); $less->setPreserveComments(false); // add paths for imports $less->addImportDir($this->_tpl->lessFull); $less->addImportDir(JPATH_ROOT); // from php $less->setVariables(array('css' => str_replace($this->_tpl->baseurl, '', $this->_tpl->css), 'less' => str_replace($this->_tpl->baseurl, '', $this->_tpl->less), 'images' => str_replace($this->_tpl->baseurl, '', $this->_tpl->img), 'debug' => (int) $this->_isDebug())); // add custom functions $less->registerFunction('data-uri', array($this, 'lib_dataUri')); return $less; }
function autoCompileLess() { // include lessc.inc require_once get_template_directory() . '/inc/less/lessc.inc.php'; // input and output location $inputFile = get_template_directory() . '/assets/css/styles.less'; $outputFile = get_template_directory() . '/assets/css/styles.css'; // load the cache $cacheFile = $inputFile . ".cache"; if (file_exists($cacheFile)) { $cache = unserialize(file_get_contents($cacheFile)); } else { $cache = $inputFile; } $less = new lessc(); // create a new cache object, and compile $less->setPreserveComments(true); $newCache = $less->cachedCompile($cache); // output a LESS file, and cache file only if it has been modified since last compile if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) { file_put_contents($cacheFile, serialize($newCache)); file_put_contents($outputFile, $newCache['compiled']); } }
function ro_theme_autoCompileLess($inputFile, $outputFile) { require_once ABS_PATH_FR . '/inc/lessc.inc.php'; global $tb_options; $less = new lessc(); $less->setFormatter("classic"); $less->setPreserveComments(true); /*Styling Options*/ $tb_primary_color = $tb_options['tb_primary_color']; $tb_secondary_color = $tb_options['tb_secondary_color']; $variables = array("tb_primary_color" => $tb_primary_color, "tb_secondary_color" => $tb_secondary_color); $less->setVariables($variables); $cacheFile = $inputFile . ".cache"; if (file_exists($cacheFile)) { $cache = unserialize(file_get_contents($cacheFile)); } else { $cache = $inputFile; } $newCache = $less->cachedCompile($inputFile); if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) { file_put_contents($cacheFile, serialize($newCache)); file_put_contents($outputFile, $newCache['compiled']); } }
/** * Compile less to css. Creates a cache-file of the last compiled less-file. * * This code is originally from the manual of lessphp. * * @param string $inputFile the filename of the less-file. * @param string $outputFile the filename of the css-file to be created. * @param array $config with configuration details. * * @return void */ function autoCompileLess($inputFile, $outputFile, $config) { $cacheFile = $inputFile . ".cache"; if (file_exists($cacheFile)) { $cache = unserialize(file_get_contents($cacheFile)); } else { $cache = $inputFile; } $less = new lessc(); // Add custom less functions if (isset($config['functions'])) { foreach ($config['functions'] as $key => $val) { $less->registerFunction($key, $val); } } // Add import dirs if (isset($config['imports'])) { foreach ($config['imports'] as $val) { $less->addImportDir($val); } } // Set output formatter if (isset($config['formatter'])) { $less->setFormatter($config['formatter']); } // Preserve comments if (isset($config['comments'])) { $less->setPreserveComments($config['comments']); } // Compile a new cache $newCache = $less->cachedCompile($cache); if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) { file_put_contents($cacheFile, serialize($newCache)); file_put_contents($outputFile, $newCache['compiled']); } }
public function beforesave() { $change = true; if (!empty($_POST['bootstrap'])) { $change = $this->cfg['bootstrap'] != $_POST['bootstrap']; } if ($change) { include_once JPATH_SITE . '/components/com_acctexp/lib/lessphp/lessc.inc.php'; $less = new lessc(); $less->setImportDir(array(JPATH_SITE . '/media/com_acctexp/less/')); //$less->setFormatter("compressed"); $less->setPreserveComments(true); if (!isset($_POST['bootstrap'])) { $this->cfg['bootstrap'] = true; } else { $this->cfg['bootstrap'] = $_POST['bootstrap']; } if ($this->cfg['bootstrap']) { $less->compileFile(JPATH_SITE . "/media/com_acctexp/less/template.etacarinae.less", JPATH_SITE . '/media/com_acctexp/css/template.etacarinae.css'); } else { $less->compileFile(JPATH_SITE . "/media/com_acctexp/less/template.etacarinae-reuse-bootstrap.less", JPATH_SITE . '/media/com_acctexp/css/template.etacarinae.css'); } } }
/** * Lessify the stylesheet and return the href of the compiled file * * @param string $src Source URL of the file to be parsed * @param string $handle An identifier for the file used to create the file name in the cache * @return string URL of the compiled stylesheet */ public function parse_stylesheet($src, $handle) { // we only want to handle .less files if (!preg_match('/\\.less(\\.php)?$/', preg_replace('/\\?.*$/', '', $src))) { return $src; } // get file path from $src if (!strstr($src, '?')) { $src .= '?'; } // prevent non-existent index warning when using list() & explode() // Match the URL schemes between WP_CONTENT_URL and $src, // so the str_replace further down will work $src_scheme = parse_url($src, PHP_URL_SCHEME); $wp_content_url_scheme = parse_url(WP_CONTENT_URL, PHP_URL_SCHEME); if ($src_scheme != $wp_content_url_scheme) { $src = set_url_scheme($src, $wp_content_url_scheme); } list($less_path, $query_string) = explode('?', str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $src)); // output css file name $css_path = trailingslashit($this->get_cache_dir()) . "{$handle}.css"; // automatically regenerate files if source's modified time has changed or vars have changed try { // initialise the parser $less = new lessc(); // load the cache $cache_path = "{$css_path}.cache"; if (file_exists($cache_path)) { $cache = unserialize(file_get_contents($cache_path)); } // vars to pass into the compiler - default @themeurl var for image urls etc... $this->vars['themeurl'] = '~"' . get_stylesheet_directory_uri() . '"'; $this->vars['lessurl'] = '~"' . dirname($src) . '"'; $this->vars = apply_filters('less_vars', $this->vars, $handle); // If the cache or root path in it are invalid then regenerate if (empty($cache) || empty($cache['less']['root']) || !file_exists($cache['less']['root'])) { $cache = array('vars' => $this->vars, 'less' => $less_path); } // less config $less->setFormatter(apply_filters('less_compression', $this->compression)); $less->setPreserveComments(apply_filters('less_preserve_comments', $this->preserve_comments)); $less->setVariables($this->vars); // add directories to scan for imports $import_dirs = apply_filters('less_import_dirs', $this->import_dirs); if (!empty($import_dirs)) { foreach ((array) $import_dirs as $dir) { $less->addImportDir($dir); } } // register and unregister functions foreach ($this->registered_functions as $name => $callable) { $less->registerFunction($name, $callable); } foreach ($this->unregistered_functions as $name) { $less->unregisterFunction($name); } // allow devs to mess around with the less object configuration do_action_ref_array('lessc', array(&$less)); // $less->cachedCompile only checks for changed file modification times // if using the theme customiser (changed variables not files) then force a compile if ($this->vars !== $cache['vars']) { $force = true; } else { $force = false; } $less_cache = $less->cachedCompile($cache['less'], apply_filters('less_force_compile', $force)); if (empty($cache) || empty($cache['less']['updated']) || $less_cache['updated'] > $cache['less']['updated'] || $this->vars !== $cache['vars']) { file_put_contents($cache_path, serialize(array('vars' => $this->vars, 'less' => $less_cache))); file_put_contents($css_path, $less_cache['compiled']); } } catch (exception $ex) { wp_die($ex->getMessage()); } // restore query string it had if any $url = trailingslashit($this->get_cache_dir(false)) . "{$handle}.css" . (!empty($query_string) ? "?{$query_string}" : ''); // restore original url scheme $url = set_url_scheme($url, $src_scheme); return add_query_arg('ver', $less_cache['updated'], $url); }
public function lessen(&$errors) { // Convert LESS files include_once JPATH_SITE . '/components/com_acctexp/lib/lessphp/lessc.inc.php'; $less = new lessc(); $less->setImportDir(array(JPATH_SITE . '/media/com_acctexp/less/')); $less->setPreserveComments(true); $v = new JVersion(); if ($v->isCompatible('3.0')) { $less->compileFile(JPATH_SITE . "/media/com_acctexp/less/admin-j3.less", JPATH_SITE . '/media/com_acctexp/css/admin.css'); } else { $less->compileFile(JPATH_SITE . "/media/com_acctexp/less/admin.less", JPATH_SITE . '/media/com_acctexp/css/admin.css'); } }
/** * Lessify the stylesheet and return the href of the compiled file * * @param string $src Source URL of the file to be parsed * @param string $handle An identifier for the file used to create the file name in the cache * @return string URL of the compiled stylesheet */ public function parse_stylesheet($src, $handle) { // we only want to handle .less files if (!preg_match('/\\.less(\\.php)?$/', preg_replace('/\\?.*$/', '', $src))) { return $src; } // get file path from $src if (!strstr($src, '?')) { $src .= '?'; } // prevent non-existent index warning when using list() & explode() // Match the URL schemes between WP_CONTENT_URL and $src, // so the str_replace further down will work $src_scheme = parse_url($src, PHP_URL_SCHEME); $wp_content_url_scheme = parse_url(WP_CONTENT_URL, PHP_URL_SCHEME); if ($src_scheme != $wp_content_url_scheme) { $src = set_url_scheme($src, $wp_content_url_scheme); } list($less_path, $query_string) = explode('?', str_replace(WP_CONTENT_URL, WP_CONTENT_DIR, $src)); $cache = $this->get_cached_file_data($handle); // vars to pass into the compiler - default @themeurl var for image urls etc... $this->vars['themeurl'] = '~"' . get_stylesheet_directory_uri() . '"'; $this->vars['lessurl'] = '~"' . dirname($src) . '"'; $this->vars = apply_filters('less_vars', $this->vars, $handle); // The overall "version" of the LESS file is all it's vars, src etc. $less_version = md5(serialize(array($this->vars, $src))); /** * Give the ability to disable always compiling the LESS with lessc() * and instead just use the $vars and $version of the LESS file to * dictate whether the LESS should be (re)generated. * * This means we don't need to run everything through the lessc() compiler * on every page load. The tradeoff is making a change in a LESS file will not * necessarily cause a (re)generation, one would need to bump the $ver param * on wp_enqueue_script() to cause that. */ if (!get_option('wp_less_always_compile_less', true)) { if (!empty($cache['version']) && $cache['version'] === $less_version) { // restore query string it had if any $url = $cache['url'] . (!empty($query_string) ? "?{$query_string}" : ''); $url = set_url_scheme($url, $src_scheme); return add_query_arg('ver', $less_version, $url); } } // automatically regenerate files if source's modified time has changed or vars have changed try { // initialise the parser if (!class_exists('lessc')) { return $url; //wp_die( 'the lessphp library is missing, aborting, run composer update' ); } // If the cache or root path in it are invalid then regenerate if (empty($cache) || empty($cache['less']['root']) || !file_exists($cache['less']['root'])) { $cache = array('vars' => $this->vars, 'less' => $less_path); } if (empty($cache['url'])) { $cache['url'] = trailingslashit($this->get_cache_dir(false)) . "{$handle}.css"; } // less config $less = new lessc(); $less->setFormatter(apply_filters('less_compression', $this->compression)); $less->setPreserveComments(apply_filters('less_preserve_comments', $this->preserve_comments)); $less->setVariables($this->vars); // add directories to scan for imports $import_dirs = apply_filters('less_import_dirs', $this->import_dirs); if (!empty($import_dirs)) { foreach ((array) $import_dirs as $dir) { $less->addImportDir($dir); } } // register and unregister functions foreach ($this->registered_functions as $name => $callable) { $less->registerFunction($name, $callable); } foreach ($this->unregistered_functions as $name) { $less->unregisterFunction($name); } // allow devs to mess around with the less object configuration do_action_ref_array('lessc', array(&$less)); // $less->cachedCompile only checks for changed file modification times // if using the theme customiser (changed variables not files) then force a compile if ($this->vars !== $cache['vars']) { $force = true; } else { $force = false; } $force = apply_filters('less_force_compile', $force); $less_cache = $less->cachedCompile($cache['less'], $force); // if they have the same values but differing order, they wont match //sort( $cache['less'] ); //sort( $less_cache ); if (empty($cache) || empty($cache['less']['updated']) || md5($less_cache['compiled']) !== md5($cache['less']['compiled']) || $this->vars !== $cache['vars']) { // output css file name $css_path = trailingslashit($this->get_cache_dir()) . "{$handle}.css"; $cache = array('vars' => $this->vars, 'url' => trailingslashit($this->get_cache_dir(false)) . "{$handle}.css", 'version' => $less_version, 'less' => null); /** * If the option to not have LESS always compiled is set, * then we dont store the whole less_cache in the options table as it's * not needed because we only do a comparison based off $vars and $src * (which includes the $ver param). * * This saves space on the options table for high performance environments. */ if (get_option('wp_less_always_compile_less', true)) { $cache['less'] = $less_cache; } $payload = '<strong>Rebuilt stylesheet with handle: "' . $handle . '"</strong><br>'; if ($this->vars != $cache['vars']) { $payload .= '<em>Variables changed</em>'; $difference = array_merge(array_diff_assoc($cache['vars'], $this->vars), array_diff_assoc($this->vars, $cache['vars'])); $payload .= '<pre>' . print_r($difference, true) . '</pre>'; } else { if (empty($cache) || empty($cache['less']['updated'])) { $payload .= '<em>Empty cache or empty last update time</em>'; } else { if ($less_cache['updated'] > $cache['less']['updated']) { $payload .= '<em>Update times different</em>'; } else { $payload .= '<em><strong>Unknown! Contact the developers poste haste!!!!!!!</em><strong></em>'; } } } $payload .= '<br>src: <code>"' . $src . '"</code> css path: <code>"' . $css_path . '"</code> and cache path: <code>"' . $cache_path . '"</code> and scheme <code>"' . $src_scheme . '"</code>'; $this->add_message(array('time' => time(), 'payload' => $payload)); $this->save_parsed_css($css_path, $less_cache['compiled']); $this->update_cached_file_data($handle, $cache); } } catch (exception $ex) { $this->add_message(array('time' => time(), 'payload' => '<strong>Lessphp failure</strong> ' . $ex->GetMessage())); wp_die(wp_strip_all_tags($ex->getMessage())); } // restore query string it had if any $url = $cache['url'] . (!empty($query_string) ? "?{$query_string}" : ''); // restore original url scheme $url = set_url_scheme($url, $src_scheme); if (get_option('wp_less_always_compile_less', true)) { return add_query_arg('ver', $less_cache['updated'], $url); } return add_query_arg('ver', $less_version, $url); }
/** * Get instance of less compiler * * @return lessc */ private function getCompiler() { $less = new lessc(); // do not spend CPU time letting less doing minify $less->setPreserveComments(true); return $less; }
/** * Initialize less compiler * @param $formatterName * @param $z_textColor * @param $z_themeColor * @param $z_themeColor2 * @param $z_themeColorBtn * @param $z_themeColorHeader * @param $z_path * @return lessc */ protected function initializeLessCompiler($formatterName, $z_textColor, $z_themeColor, $z_themeColor2, $z_themeColorBtn, $z_themeColorHeader, $z_path) { $lessCompiler = new lessc(); $lessCompiler->setPreserveComments(false); $lessCompiler->setFormatter($formatterName); $lessCompiler->setImportDir($this->getLessFilesPath()); $lessCompiler->setVariables(array("z_textColor" => $z_textColor, "z_themeColor" => $z_themeColor, "z_themeColor2" => $z_themeColor2, "z_themeColorBtn" => $z_themeColorBtn, "z_themeColorHeader" => $z_themeColorHeader, "z_path" => $z_path)); return $lessCompiler; }
com_install(); break; case 'initsettings': $aecConfig = new aecConfig(); $aecConfig->initParams(); echo 'SPLINES RETICULATED.'; break; case 'parsertest': $top = new templateOverrideParser(); break; case 'lessen': include_once JPATH_SITE . '/components/com_acctexp/lib/lessphp/lessc.inc.php'; $less = new lessc(); $less->setImportDir(array(JPATH_SITE . '/media/com_acctexp/less/')); //$less->setFormatter("compressed"); $less->setPreserveComments(true); $v = new JVersion(); if ($v->isCompatible('3.0')) { $less->compileFile(JPATH_SITE . "/media/com_acctexp/less/admin-j3.less", JPATH_SITE . '/media/com_acctexp/css/admin.css'); } else { $less->compileFile(JPATH_SITE . "/media/com_acctexp/less/admin.less", JPATH_SITE . '/media/com_acctexp/css/admin.css'); } break; default: $class = 'aecAdmin' . ucfirst($entity); /** @var aecAdminEntity $class */ $class = new $class($id, $entity); $class->call($task); break; } function addGroup($type, $id, $groupid)
/** * Checks if .less file has been updated and stores it in cache for quick comparison. * * This function is taken and modified from documentation of lessphp * * @param String $inputFile * @param String $outputFile */ function autoCompileLess($inputFile, $outputFile) { // load config file $config = JFactory::getConfig(); //path to temp folder $tmpPath = $config->get('tmp_path'); //load chached file $cacheFile = $tmpPath . DIRECTORY_SEPARATOR . $this->app->getTemplate() . "_" . basename($inputFile) . ".cache"; if (file_exists($cacheFile)) { $tmpCache = unserialize(file_get_contents($cacheFile)); if ($tmpCache['root'] === $inputFile) { $cache = $tmpCache; } else { $cache = $inputFile; unlink($cacheFile); } } else { $cache = $inputFile; } //instantiate less compiler $less = new lessc(); //set less options //option: force recompilation regardless of change $force = (bool) $this->params->get('less_force', 0); //option: preserve comments if ($this->params->get('less_comments', 0)) { $less->setPreserveComments(true); } //option: compression if ($this->params->get('less_compress', 0)) { $less->setFormatter("compressed"); } else { $less->setFormatter("classic"); } //compile cache file $newCache = $less->cachedCompile($cache, $force); if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) { file_put_contents($cacheFile, serialize($newCache)); file_put_contents($outputFile, $newCache['compiled']); } }
protected function autoCompileLess($inputFile) { global $okt; $outputFile = OKT_PUBLIC_PATH . '/cache/' . md5($inputFile) . '.css'; $cacheFile = $outputFile . '.cache'; if (file_exists($cacheFile)) { $cache = unserialize(file_get_contents($cacheFile)); } else { $cache = $inputFile; } try { $less = new lessc(); $less->setPreserveComments(true); $less->setImportDir(array(isset($okt->theme->path) ? $okt->theme->path . '/css/' : null, OKT_PUBLIC_PATH . '/css/less/')); $less->setVariables($okt->theme->getLessVariables()); $newCache = $less->cachedCompile($cache); } catch (Exception $ex) { $okt->error->set('lessphp fatal error: ' . $ex->getMessage()); } if (!is_array($cache) || $newCache['updated'] > $cache['updated']) { file_put_contents($cacheFile, serialize($newCache)); file_put_contents($outputFile, $newCache['compiled']); } return str_replace(OKT_PUBLIC_PATH, $okt->config->app_path . OKT_PUBLIC_DIR, $outputFile); }
/** * Get the CSS for the given style and preset * * @param $style * @param $preset * @return string */ function create_css($style, $preset) { $paths = $this->get_widget_paths(); $style_file = false; // Find the file - exit if it can't be found. foreach ($paths as $path) { if (file_exists($path . '/' . $this->origin_id . '/styles/' . $style . '.less')) { $style_file = $path . '/' . $this->origin_id . '/styles/' . $style . '.less'; break; } } if (empty($style_file)) { return ''; } if (!class_exists('lessc')) { include plugin_dir_path(__FILE__) . 'lib/lessc.inc.php'; } foreach ($this->get_widget_folders() as $folder => $folder_url) { $filename = rtrim($folder, '/') . '/' . $this->origin_id . '/styles/' . $style . '.less'; if (file_exists($filename)) { $less = file_get_contents($filename); break; } } // Add in the mixins $less = str_replace('@import "../../../less/mixins";', "\n\n" . file_get_contents(plugin_dir_path(__FILE__) . 'less/mixins.less'), $less); // Apply the preset variables to the LESS file $presets = $this->get_style_presets($style); if (!empty($presets[$preset]) && is_array($presets[$preset])) { foreach ($presets[$preset] as $k => $v) { $less = preg_replace('/@' . preg_quote($k) . ':(.*);/', '@' . $k . ': ' . $v . ';', $less); } } // Scope the CSS with the wrapper we'll be adding $less = '.origin-widget.origin-widget-' . $this->origin_id . '-' . $style . '-' . $preset . ' {' . $less . '}'; $lc = new lessc(); $lc->setPreserveComments(false); $lc->registerFunction('lumlighten', 'origin_widgets_less_lumlighten'); $lc->registerFunction('lumdarken', 'origin_widgets_less_lumdarken'); $lc->registerFunction('texture', 'origin_widgets_less_texture'); $lc->registerFunction('widgetimage', 'origin_widgets_less_widgetimage'); // Create the CSS return $lc->compile($less); }
/** * Uses phpless to parse LESS in our CSS * * most of this function is error handling to show a nice useful error when * LESS compilation fails * * @param string $css * @return string */ function css_parseless($css) { global $conf; $less = new lessc(); $less->importDir[] = DOKU_INC; $less->setPreserveComments(!$conf['compress']); if (defined('DOKU_UNITTEST')) { $less->importDir[] = TMP_DIR; } try { return $less->compile($css); } catch (Exception $e) { // get exception message $msg = str_replace(array("\n", "\r", "'"), array(), $e->getMessage()); // try to use line number to find affected file if (preg_match('/line: (\\d+)$/', $msg, $m)) { $msg = substr($msg, 0, -1 * strlen($m[0])); //remove useless linenumber $lno = $m[1]; // walk upwards to last include $lines = explode("\n", $css); for ($i = $lno - 1; $i >= 0; $i--) { if (preg_match('/\\/(\\* XXXXXXXXX )(.*?)( XXXXXXXXX \\*)\\//', $lines[$i], $m)) { // we found it, add info to message $msg .= ' in ' . $m[2] . ' at line ' . ($lno - $i); break; } } } // something went wrong $error = 'A fatal error occured during compilation of the CSS files. ' . 'If you recently installed a new plugin or template it ' . 'might be broken and you should try disabling it again. [' . $msg . ']'; echo ".dokuwiki:before {\n content: '{$error}';\n background-color: red;\n display: block;\n background-color: #fcc;\n border-color: #ebb;\n color: #000;\n padding: 0.5em;\n }"; exit; } }
/** * Processing less file. * * @param $path * @param string $cacheFolder * @return null|string * @throws \Exception */ public function lessProcess($path, $cacheFolder = 'styles') { $output = null; $ext = pathinfo($path, PATHINFO_EXTENSION); if (!$ext) { $path .= '.less'; } $systemPath = $path; $segments = explode('/', $path); $lessFolder = ROOT . DS . 'webroot' . DS . 'less'; list($plugin, $path) = $this->_View->pluginSplit($path, false); $webRoot = ROOT . DS . 'webroot' . DS; $isPlugin = Plugin::loaded($plugin); $this->_imageFull = WWW_ROOT . DS . 'img' . DS; // Set data for plugin less. if ($plugin && $isPlugin) { $pluginPath = Plugin::path($plugin); $lessFolder = $pluginPath . 'webroot' . DS . 'less'; $webRootPlg = $pluginPath . 'webroot' . DS; $this->_type = 'plugin:' . $plugin; $this->_imageFull = $webRootPlg . 'img' . DS; } $lessFile = $lessFolder . DS . $path; // Set data for module less. if (strpos($segments[0], ':')) { list($type, $name) = explode(':', $segments[0]); if ($type == 'modules') { $modulePath = ROOT . DS . 'modules' . DS . $name . DS . 'assets' . DS; $lessFile = $modulePath . 'less' . DS . $segments[1]; $this->_type = 'modules:' . $name; $this->_imageFull = $modulePath . 'img' . DS; } } if (file_exists($lessFile)) { $less = new \lessc(); $fileHash = md5(serialize(md5_file($lessFile))); $cssFileName = $fileHash . '.css'; $less->registerFunction('url', array($this, 'lessPhpUrl')); $less->addImportDir(Plugin::path('Union/Core') . 'webroot' . DS . 'less' . DS); if (!DEBUG) { $formatter = new \lessc_formatter_compressed(); $less->setFormatter($formatter); $less->setPreserveComments(false); $cssFileName = $fileHash . '.min.css'; } $updateFile = false; try { $cssFile = $webRoot . 'cache' . DS . $cacheFolder . DS . $cssFileName; if (file_exists($cssFile)) { $cacheRes = fopen($cssFile, 'r'); $firstLine = fgets($cacheRes); fclose($cacheRes); if (!preg_match("#{$fileHash}#i", $firstLine)) { $updateFile = true; } } else { $updateFile = true; } $output = '/cache/' . $cacheFolder . '/' . $cssFileName; if ($this->_lessCompile) { $updateFile = true; } if ($updateFile) { $css = $less->compileFile($lessFile); $css = $this->_getLessFileComment($fileHash, $systemPath) . $css; $file = new File($cssFile, true); $file->write($css); $file->close(); } } catch (Exception $error) { die('<strong>' . __d('union_dev', 'Less Error:') . '</strong><br/><pre>' . $error->getMessage() . '</pre>'); } } return $output; }
$scss = new scssc(); // Set the import path and formatting type $scss->setImportPaths(dirname($file) . "/"); $scss->setFormatter('scss_formatter_compressed'); // scss_formatter, scss_formatter_nested, scss_formatter_compressed try { $scssContent = $scss->compile('@import "' . $fileName . '"'); $fh = fopen(substr($file, 0, -4) . "css", 'w'); fwrite($fh, $scssContent); fclose($fh); } catch (Exception $e) { echo ";top.ICEcoder.message('Couldn\\'t compile your Sass, error info below:\\n\\n" . $e->getMessage() . "');"; } } // LESS Compiling if we have LESSPHP plugin installed if (strtolower($fileExt) == "less" && file_exists(dirname(__FILE__) . "/../plugins/lessphp/lessc.inc.php")) { // Load the LESSPHP lib and start a new instance require dirname(__FILE__) . "/../plugins/lessphp/lessc.inc.php"; $less = new lessc(); // Set the formatting type and if we want to preserve comments $less->setFormatter('lessjs'); // lessjs (same style used in LESS for JS), compressed (no whitespace) or classic (LESSPHP's original formatting) $less->setPreserveComments(false); // true or false try { $less->checkedCompile($file, substr($file, 0, -4) . "css"); // Note: Only recompiles if changed } catch (Exception $e) { echo ";top.ICEcoder.message('Couldn\\'t compile your LESS, error info below:\\n\\n" . $e->getMessage() . "');"; } }
/** * Returns LESS compiler set up for use with MediaWiki * * @param Config $config * @throws MWException * @since 1.22 * @return lessc */ public static function getLessCompiler(Config $config) { // When called from the installer, it is possible that a required PHP extension // is missing (at least for now; see bug 47564). If this is the case, throw an // exception (caught by the installer) to prevent a fatal error later on. if (!class_exists('lessc')) { throw new MWException('MediaWiki requires the lessphp compiler'); } if (!function_exists('ctype_digit')) { throw new MWException('lessc requires the Ctype extension'); } $less = new lessc(); $less->setPreserveComments(true); $less->setVariables(self::getLessVars($config)); $less->setImportDir($config->get('ResourceLoaderLESSImportPaths')); foreach ($config->get('ResourceLoaderLESSFunctions') as $name => $func) { $less->registerFunction($name, $func); } return $less; }
public function compress() { $this->lessc->setPreserveComments(false); $this->lessc->setFormatter('compressed'); return $this; }
public function filterLoad(AssetInterface $asset) { $lc = new \lessc(); if ($dir = $asset->getSourceDirectory()) { $lc->importDir = $dir; } foreach ($this->loadPaths as $loadPath) { $lc->addImportDir($loadPath); } foreach ($this->customFunctions as $name => $callable) { $lc->registerFunction($name, $callable); } if ($this->formatter) { $lc->setFormatter($this->formatter); } if (null !== $this->preserveComments) { $lc->setPreserveComments($this->preserveComments); } if (method_exists($lc, 'setOptions') && count($this->options) > 0) { $lc->setOptions($this->options); } $asset->setContent($lc->parse($asset->getContent(), $this->presets)); }
protected static function compileCSS($sourceFileWithPath, $cssFileWithPath, $sourceFileType, $vars) { global $REX; if (!file_exists($sourceFileWithPath)) { return; } // get content of source file $sourceFileContent = file_get_contents($sourceFileWithPath); // strip comments out $sourceFileContent = self::stripCSSComments($sourceFileContent); // get file path $path = pathinfo($sourceFileWithPath); // compile source file to css try { if ($sourceFileType == 'scss') { // EP for scss string compilation $compiledCSS = rex_register_extension_point('SEO42_COMPILE_SCSS', $sourceFileContent, array('vars' => $vars)); if ($sourceFileContent == $compiledCSS) { // include compiler if (!class_exists('scssc')) { require_once $REX['INCLUDE_PATH'] . '/addons/seo42/classes/class.scssc.inc.php'; } $formatter = new scss_formatter(); $formatter->indentChar = "\t"; $formatter->close = "}" . PHP_EOL; $formatter->assignSeparator = ": "; $scss = new scssc(); $scss->setFormatter($formatter); $compiledCSS = $scss->compile($sourceFileContent); } } else { // EP for less string compilation $compiledCSS = rex_register_extension_point('SEO42_COMPILE_LESS', $sourceFileContent, array('vars' => $vars, 'path' => $path['dirname'])); if ($sourceFileContent == $compiledCSS) { // include compiler if (!class_exists('lessc')) { require_once $REX['INCLUDE_PATH'] . '/addons/seo42/classes/class.lessc.inc.php'; } $formatter = new lessc_formatter_classic(); $formatter->indentChar = "\t"; $formatter->close = "}" . PHP_EOL; $formatter->assignSeparator = ": "; $less = new lessc(); $less->setImportDir($path['dirname']); $less->setFormatter($formatter); $less->setPreserveComments(true); $less->setVariables($vars); $compiledCSS = $less->compile($sourceFileContent); } } } catch (Exception $e) { echo '" />'; // close tag as we are probably in an open link tag in head section of website echo '<p style="margin: 5px;"><code>'; echo '<strong>' . strtoupper($sourceFileType) . ' Compile Error:</strong><br/>'; echo $e->getMessage(); echo '</code></p>'; exit; } // write css $fileHandle = fopen($cssFileWithPath, 'w'); fwrite($fileHandle, $compiledCSS); fclose($fileHandle); }