示例#1
1
function do_minify($content)
{
    global $config;
    $c = File::open(__DIR__ . DS . 'states' . DS . 'config.txt')->unserialize();
    $url = $config->protocol . $config->host;
    // Minify HTML
    if (isset($c['html_minifier'])) {
        $config->html_minifier = true;
        Config::set('html_minifier', $config->html_minifier);
        $content = Converter::detractSkeleton($content);
    }
    // Minify URL
    if (isset($c['url_minifier']) && Text::check($content)->has('="' . $url)) {
        $content = str_replace(array('="' . $url . '"', '="' . $url . '/', '="' . $url . '?', '="' . $url . '#'), array('="/"', '="/', '="?', '="#'), $content);
    }
    // Minify Embedded CSS
    if (isset($c['css_minifier'])) {
        $content = preg_replace_callback('#<style(>| .*?>)([\\s\\S]*?)<\\/style>#i', function ($matches) use($config, $c, $url) {
            $css = Converter::detractShell($matches[2]);
            if (isset($c['url_minifier'])) {
                $css = preg_replace('#(?<=[\\s:])(src|url)\\(' . preg_quote($url, '/') . '#', '$1(', $css);
            }
            return '<style' . $matches[1] . $css . '</style>';
        }, $content);
    }
    // Minify Embedded JavaScript
    if (isset($c['js_minifier'])) {
        $content = preg_replace_callback('#<script(>| .*?>)([\\s\\S]*?)<\\/script>#i', function ($matches) {
            $js = Converter::detractSword($matches[2]);
            return '<script' . $matches[1] . $js . '</script>';
        }, $content);
    }
    return $content;
}
示例#2
0
function do_minify($content)
{
    global $config;
    $c_minify = $config->states->{'plugin_' . md5(File::B(__DIR__))};
    $url = $config->protocol . $config->host;
    // Minify HTML
    if (isset($c_minify->html_minify)) {
        Config::set('html_minify', true);
        $content = Converter::detractSkeleton($content);
    }
    // Minify URL
    if (isset($c_minify->url_minify) && Text::check($content)->has('="' . $url)) {
        Config::set('url_minify', true);
        $content = str_replace(array('="' . $url . '"', '="' . $url . '/', '="' . $url . '?', '="' . $url . '#'), array('="/"', '="/', '="?', '="#'), $content);
    }
    // Minify Embedded CSS
    if (isset($c_minify->css_minify)) {
        Config::set('css_minify', true);
        $content = preg_replace_callback('#<style(>| .*?>)([\\s\\S]*?)<\\/style>#i', function ($matches) use($config, $c_minify, $url) {
            $css = Converter::detractShell($matches[2]);
            if (isset($c_minify->url_minify)) {
                $css = preg_replace('#(?<=[\\s:])(src|url)\\(' . preg_quote($url, '/') . '#', '$1(', $css);
            }
            return '<style' . $matches[1] . $css . '</style>';
        }, $content);
    }
    // Minify Embedded JavaScript
    if (isset($c_minify->js_minify)) {
        Config::set('js_minify', true);
        $content = preg_replace_callback('#<script(>| .*?>)([\\s\\S]*?)<\\/script>#i', function ($matches) {
            $js = Converter::detractSword($matches[2]);
            return '<script' . $matches[1] . $js . '</script>';
        }, $content);
    }
    return $content;
}
示例#3
0
 public static function merge($path, $name = null, $addon = "", $call = null)
 {
     $path = is_string($path) && strpos($path, ';') !== false ? explode(';', $path) : (array) $path;
     $the_path = ASSET . DS . File::path($name);
     $the_path_log = SYSTEM . DS . 'log' . DS . 'asset.' . str_replace(array(ASSET . DS, DS), array("", '__'), $the_path) . '.log';
     $is_valid = true;
     if (!file_exists($the_path_log)) {
         $is_valid = false;
     } else {
         $the_path_time = explode("\n", file_get_contents($the_path_log));
         if (count($the_path_time) !== count($path)) {
             $is_valid = false;
         } else {
             foreach ($the_path_time as $i => $time) {
                 $p = self::path($path[$i]);
                 if (!file_exists($p) || (int) filemtime($p) !== (int) $time) {
                     $is_valid = false;
                     break;
                 }
             }
         }
     }
     $time = "";
     $content = "";
     $e = File::E($name);
     if (!file_exists($the_path) || !$is_valid) {
         if (Text::check($e)->in(array('gif', 'jpeg', 'jpg', 'png'))) {
             foreach ($path as $p) {
                 if (!self::ignored($p)) {
                     $p = self::path($p);
                     if (file_exists($p)) {
                         $time .= filemtime($p) . "\n";
                     }
                 }
             }
             File::write(trim($time))->saveTo($the_path_log);
             Image::take($path)->merge()->saveTo($the_path);
         } else {
             foreach ($path as $p) {
                 if (!self::ignored($p)) {
                     $p = self::path($p);
                     if (file_exists($p)) {
                         $time .= filemtime($p) . "\n";
                         $c = file_get_contents($p);
                         if (strpos(File::B($p), '.min.') === false) {
                             if (strpos(File::B($the_path), '.min.css') !== false) {
                                 $content .= Converter::detractShell($c) . "\n";
                             } else {
                                 if (strpos(File::B($the_path), '.min.js') !== false) {
                                     $content .= Converter::detractSword($c) . "\n";
                                 } else {
                                     $content .= $c . "\n\n";
                                 }
                             }
                         } else {
                             $content .= $c . "\n\n";
                         }
                     }
                 }
             }
             File::write(trim($time))->saveTo($the_path_log);
             File::write(trim($content))->saveTo($the_path);
         }
     }
     if (is_null($call)) {
         $call = Mecha::alter($e, array('css' => 'stylesheet', 'js' => 'javascript', 'gif' => 'image', 'jpeg' => 'image', 'jpg' => 'image', 'png' => 'image'));
     }
     return call_user_func_array('self::' . $call, array($the_path, $addon));
 }
示例#4
0
 /**
  * ====================================================================
  *  HTML MINIFIER
  * ====================================================================
  *
  * -- CODE: -----------------------------------------------------------
  *
  *    Converter::detractSkeleton(file_get_contents('test.html'));
  *
  * --------------------------------------------------------------------
  *
  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  *  Parameter | Type   | Description
  *  --------- | ------ | ----------------------------------------------
  *  $input    | string | The HTML string to be compressed
  *  --------- | ------ | ----------------------------------------------
  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  *
  */
 public static function detractSkeleton($input)
 {
     if (trim($input) === "") {
         return $input;
     }
     // Remove extra white-space(s) between HTML attribute(s)
     $input = preg_replace_callback('#<([^\\/\\s<>!]+)(?:\\s+([^<>]*?)\\s*|\\s*)(\\/?)>#s', function ($matches) {
         return '<' . $matches[1] . preg_replace('#([^\\s=]+)(\\=([\'"]?)(.*?)\\3)?(\\s+|$)#s', ' $1$2', $matches[2]) . $matches[3] . '>';
     }, $input);
     // Minify inline CSS declaration(s)
     if (strpos($input, ' style=') !== false) {
         $input = preg_replace_callback('#<([^<]+?)\\s+style=([\'"])(.*?)\\2(?=[\\/\\s>])#s', function ($matches) {
             return '<' . $matches[1] . ' style=' . $matches[2] . Converter::detractShell($matches[3]) . $matches[2];
         }, $input);
     }
     return preg_replace(array('#\\s*(<\\!--(?=\\[if\\s).*?-->)\\s*|\\s*<\\!--.*?-->\\s*#s', '#(<(?:img|input)(?:\\/?>|\\s[^<>]*?\\/?>))\\s+(?=\\<[^\\/])#s', '#(<\\!--.*?-->)|(>)(?:[\\n\\r]|\\s{2,})|(?:[\\n\\r]|\\s{2,})(<)|(>)(?:[\\n\\r]|\\s{2,})(<)#s', '#(<\\!--.*?-->)|(<(?:img|input)(?:\\/?>|\\s[^<>]*?\\/?>))\\s+(?!\\<\\/)#s', '#(<\\!--.*?-->)|(<[^\\/\\s<>]+(?:>|\\s[^<>]*?>))\\s+(?=\\<[^\\/])#s', '#(<\\!--.*?-->)|(<\\/[^\\/\\s<>]+?>)\\s+(?=\\<\\/)#s', '#(<\\!--.*?-->)|(<([^\\/\\s<>]+)(?:>|\\s[^<>]*?>))\\s+(<\\/\\3>)#s', '#(<\\!--.*?-->)|(<[^\\/\\s<>]+(?:>|\\s[^<>]*?>))\\s+(?!\\<)#s', '#(<\\!--.*?-->)|(?!\\>)\\s+(<\\/[^\\/\\s<>]+?>)#s', '#(<\\!--.*?-->)|(?!\\>)\\s+(?=\\<[^\\/])#s', '#(<\\!--.*?-->)|(<\\/[^\\/\\s<>]+?>)\\s+(?!\\<)#s', '#(<\\!--.*?-->)|(\\/>)\\s+(?!\\<)#', '#(?<=&nbsp;)(&nbsp;){2}#', '#(?<=\\>)&nbsp;(?!\\s|&nbsp;|\\<\\/)#', '#(?<=--\\>)(?:\\s|&nbsp;)+(?=\\<)#'), array('$1', '$1&nbsp;', '$1$2$3$4$5', '$1$2&nbsp;', '$1$2', '$1$2', '$1$2$4', '$1$2', '$1$2', '$1$2 ', '$1$2 ', '$1$2 ', ' $1', ' ', ""), trim($input));
 }
示例#5
0
 public static function merge($path, $name = null, $addon = "", $call = null)
 {
     $the_path = strpos($name, ROOT) === 0 ? File::path($name) : ASSET . DS . File::path($name);
     $the_log = LOG . DS . 'asset.' . str_replace(array(ASSET . DS, DS), array("", '__'), $the_path) . '.log';
     $is_valid = true;
     if (!file_exists($the_log)) {
         $is_valid = false;
     } else {
         $the_time = explode("\n", file_get_contents($the_log));
         if (count($the_time) !== count($path)) {
             $is_valid = false;
         } else {
             foreach ($the_time as $i => $unix) {
                 $p = self::path($path[$i]);
                 if (!file_exists($p) || (int) filemtime($p) !== (int) $unix) {
                     $is_valid = false;
                     break;
                 }
             }
         }
     }
     $unix = "";
     $content = "";
     $e = File::E($name);
     if (!file_exists($the_path) || !$is_valid) {
         if (Text::check($e)->in(array('gif', 'jpeg', 'jpg', 'png'))) {
             foreach ($path as &$p) {
                 $p = Filter::colon('asset:source', $p);
                 if (!self::ignored($p) && ($p = Filter::colon('asset:path', self::path($p, false)))) {
                     $unix .= filemtime($p) . "\n";
                 }
             }
             File::write(trim($unix))->saveTo($the_log);
             Image::take($path)->merge()->saveTo($the_path);
         } else {
             foreach ($path as $p) {
                 $p = Filter::colon('asset:source', $p);
                 if (!self::ignored($p) && ($p = Filter::colon('asset:path', self::path($p, false)))) {
                     $unix .= filemtime($p) . "\n";
                     $c = Filter::apply('asset:input', file_get_contents($p) . "\n", $p);
                     if (strpos(File::B($p), '.min.') === false) {
                         if (strpos(File::B($the_path), '.min.css') !== false) {
                             $c = Converter::detractShell($c);
                         } else {
                             if (strpos(File::B($the_path), '.min.js') !== false) {
                                 $c = Converter::detractSword($c);
                             } else {
                                 $c = $c . "\n";
                             }
                         }
                         $content .= Filter::apply('asset:output', $c, $p);
                     } else {
                         $content .= Filter::apply('asset:output', $c . "\n", $p);
                     }
                 }
             }
             File::write(trim($unix))->saveTo($the_log);
             File::write(trim($content))->saveTo($the_path);
         }
     }
     if (is_null($call)) {
         $call = Mecha::alter($e, array('css' => 'stylesheet', 'js' => 'javascript', 'gif' => 'image', 'jpeg' => 'image', 'jpg' => 'image', 'png' => 'image'));
     }
     return call_user_func('self::' . $call, $the_path, $addon);
 }