/** * Post processes CSS. * * This method post processes all of the CSS before it is served for this theme. * This is done so that things such as image URL's can be swapped in and to * run any specific CSS post process method the theme has requested. * This allows themes to use CSS settings. * * @param string $css The CSS to process. * @return string The processed CSS. */ public function post_process($css) { // now resolve all image locations if (preg_match_all('/\\[\\[pix:([a-z0-9_]+\\|)?([^\\]]+)\\]\\]/', $css, $matches, PREG_SET_ORDER)) { $replaced = array(); foreach ($matches as $match) { if (isset($replaced[$match[0]])) { continue; } $replaced[$match[0]] = true; $imagename = $match[2]; $component = rtrim($match[1], '|'); $imageurl = $this->pix_url($imagename, $component)->out(false); // we do not need full url because the image.php is always in the same dir $imageurl = preg_replace('|^http.?://[^/]+|', '', $imageurl); $css = str_replace($match[0], $imageurl, $css); } } // Now resolve all font locations. if (preg_match_all('/\\[\\[font:([a-z0-9_]+\\|)?([^\\]]+)\\]\\]/', $css, $matches, PREG_SET_ORDER)) { $replaced = array(); foreach ($matches as $match) { if (isset($replaced[$match[0]])) { continue; } $replaced[$match[0]] = true; $fontname = $match[2]; $component = rtrim($match[1], '|'); $fonturl = $this->font_url($fontname, $component)->out(false); // We do not need full url because the font.php is always in the same dir. $fonturl = preg_replace('|^http.?://[^/]+|', '', $fonturl); $css = str_replace($match[0], $fonturl, $css); } } // Post processing using an object representation of CSS. $hastreeprocessor = !empty($this->csstreepostprocessor) && function_exists($this->csstreepostprocessor); $needsparsing = $hastreeprocessor || !empty($this->rtlmode); if ($needsparsing) { $parser = new core_cssparser($css); $csstree = $parser->parse(); unset($parser); if ($this->rtlmode) { $this->rtlize($csstree); } if ($hastreeprocessor) { $fn = $this->csstreepostprocessor; $fn($csstree, $this); } $css = $csstree->render(); unset($csstree); } // now resolve all theme settings or do any other postprocessing $csspostprocess = $this->csspostprocess; if (function_exists($csspostprocess)) { $css = $csspostprocess($css, $this); } return $css; }
/** * Post processes CSS. * * This method post processes all of the CSS before it is served for this theme. * This is done so that things such as image URL's can be swapped in and to * run any specific CSS post process method the theme has requested. * This allows themes to use CSS settings. * * @param string $css The CSS to process. * @return string The processed CSS. */ public function post_process($css) { // now resolve all image locations if (preg_match_all('/\\[\\[pix:([a-z0-9_]+\\|)?([^\\]]+)\\]\\]/', $css, $matches, PREG_SET_ORDER)) { $replaced = array(); foreach ($matches as $match) { if (isset($replaced[$match[0]])) { continue; } $replaced[$match[0]] = true; $imagename = $match[2]; $component = rtrim($match[1], '|'); $imageurl = $this->pix_url($imagename, $component)->out(false); // we do not need full url because the image.php is always in the same dir $imageurl = preg_replace('|^http.?://[^/]+|', '', $imageurl); $css = str_replace($match[0], $imageurl, $css); } } // Now resolve all font locations. if (preg_match_all('/\\[\\[font:([a-z0-9_]+\\|)?([^\\]]+)\\]\\]/', $css, $matches, PREG_SET_ORDER)) { $replaced = array(); foreach ($matches as $match) { if (isset($replaced[$match[0]])) { continue; } $replaced[$match[0]] = true; $fontname = $match[2]; $component = rtrim($match[1], '|'); $fonturl = $this->font_url($fontname, $component)->out(false); // We do not need full url because the font.php is always in the same dir. $fonturl = preg_replace('|^http.?://[^/]+|', '', $fonturl); $css = str_replace($match[0], $fonturl, $css); } } // Now resolve all theme settings or do any other postprocessing. // This needs to be done before calling core parser, since the parser strips [[settings]] tags. $csspostprocess = $this->csspostprocess; if (function_exists($csspostprocess)) { $css = $csspostprocess($css, $this); } // Post processing using an object representation of CSS. $treeprocessor = $this->get_css_tree_post_processor(); $needsparsing = !empty($treeprocessor) || !empty($this->rtlmode); if ($needsparsing) { // We might need more memory/time to do this, so let's play safe. raise_memory_limit(MEMORY_EXTRA); core_php_time_limit::raise(300); $parser = new core_cssparser($css); $csstree = $parser->parse(); unset($parser); if ($this->rtlmode) { $this->rtlize($csstree); } if ($treeprocessor) { $treeprocessor($csstree, $this); } $css = $csstree->render(); unset($csstree); } return $css; }