Example #1
0
 /**
  * Add line numbers in C-style comments
  *
  * This uses a very basic parser easily fooled by comment tokens inside
  * strings or regexes, but, otherwise, generally clean code will not be 
  * mangled. URI rewriting can also be performed.
  *
  * @param string $content
  * 
  * @param array $options available options:
  * 
  * 'id': (optional) string to identify file. E.g. file name/path
  *
  * 'currentDir': (default null) if given, this is assumed to be the
  * directory of the current CSS file. Using this, minify will rewrite
  * all relative URIs in import/url declarations to correctly point to
  * the desired files, and prepend a comment with debugging information about
  * this process.
  * 
  * @return string 
  */
 public static function minify($content, $options = array())
 {
     $id = isset($options['id']) && $options['id'] ? $options['id'] : '';
     $content = str_replace("\r\n", "\n", $content);
     // Hackily rewrite strings with XPath expressions that are
     // likely to throw off our dumb parser (for Prototype 1.6.1).
     $content = str_replace('"/*"', '"/"+"*"', $content);
     $content = preg_replace('@([\'"])(\\.?//?)\\*@', '$1$2$1+$1*', $content);
     $lines = explode("\n", $content);
     $num_lines = count($lines);
     // determine left padding
     $pad_to = strlen((string) $num_lines);
     // e.g. 103 lines = 3 digits
     $in_comment = FALSE;
     $i = 0;
     $new_lines = array();
     while (NULL !== ($line = array_shift($lines))) {
         if ('' !== $id and 0 == $i % 50) {
             array_push($new_lines, '', "/* {$id} */", '');
         }
         ++$i;
         $new_lines[] = self::_add_note($line, $i, $in_comment, $pad_to);
         $in_comment = self::_eol_in_comment($line, $in_comment);
     }
     $content = implode("\n", $new_lines) . "\n";
     // check for desired URI rewriting
     if (isset($options['current_dir'])) {
         Minify_CSS_Uri_Rewriter::$debug_text = '';
         $content = Minify_CSS_Uri_Rewriter::rewrite($content, $options['current_dir'], isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT'], isset($options['symlinks']) ? $options['symlinks'] : array());
         $content = "/* Minify_CSS_Uri_Rewriter::\$debug_text\n\n" . Minify_CSS_Uri_Rewriter::$debug_text . "*/\n" . $content;
     }
     return $content;
 }
Example #2
0
 public function min()
 {
     $this->_options = array_merge(array('remove_charsets' => true, 'preserve_comments' => true, 'current_dir' => null, 'doc_root' => $_SERVER['DOCUMENT_ROOT'], 'prepend_relative_path' => null, 'symlinks' => array()), $this->_options);
     if ($this->_options['remove_charsets']) {
         $this->_css = preg_replace('/@charset[^;]+;\\s*/', '', $this->_css);
     }
     if (!$this->_options['preserve_comments']) {
         $this->_css = Minify_CSS_Compressor::process($this->_css, $this->_options);
     } else {
         $this->_css = Minify_CSS_Comment_Preserver::process($this->_css, array('Minify_CSS_Compressor', 'process'), array($this->_options));
     }
     if (!$this->_options['current_dir'] && !$this->_options['prepend_relative_path']) {
         return $this->_css;
     }
     if ($this->_options['current_dir']) {
         return Minify_CSS_Uri_Rewriter::rewrite($this->_css, $this->_options['current_dir'], $this->_options['doc_root'], $this->_options['symlinks']);
     } else {
         return Minify_CSS_Uri_Rewriter::prepend($this->_css, $this->_options['prepend_relative_path']);
     }
 }