From d75331d8b8816f0806ca324b550483cbadaf656f Mon Sep 17 00:00:00 2001 From: BigCoke233 Date: Tue, 14 Jul 2020 14:15:59 +0800 Subject: =?UTF-8?q?:zap:=20Add:=20=E5=8A=A0=E5=85=A5=20HTML=20=E5=8E=8B?= =?UTF-8?q?=E7=BC=A9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/Utils.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) (limited to 'libs/Utils.php') diff --git a/libs/Utils.php b/libs/Utils.php index 541e46d..b78aa06 100644 --- a/libs/Utils.php +++ b/libs/Utils.php @@ -157,17 +157,17 @@ class Utils /** * 缩略图 */ - public static function postBanner($post){ + public static function postBanner($post, $random){ if($post->fields->banner && $post->fields->banner=!''): $banner = $post->fields->banner; else: - if($post->options->randomBanner==''){ + if($random==''){ $banner = '/usr/themes/Miracles/images/postbg/'; $banner .= srand(mb_strlen($post->title)); $banner .= rand(1,15).'.jpg'; } else{ - $banner_url = explode(',',$post->options->randomBanner); + $banner_url = explode(',',$random); $banner = $banner_url[mt_rand(0,count($banner_url)-1)].'\"'; } endif; @@ -195,4 +195,57 @@ class Utils echo $banner; } + + /** + * 压缩 HTML + * thanks https://www.jiangxu.site/concerning-how-to-typecho-integrated-html-compression.html + */ + public static function compressHtml($html_source) { + $chunks = preg_split('/(.*?|.*?<\/nocompress>|||)/msi', $html_source, -1, PREG_SPLIT_DELIM_CAPTURE); + $compress = ''; + foreach ($chunks as $c) { + if (strtolower(substr($c, 0, 19)) == '') { + $c = substr($c, 19, strlen($c) - 19 - 20); + $compress .= $c; + continue; + } else if (strtolower(substr($c, 0, 12)) == '') { + $c = substr($c, 12, strlen($c) - 12 - 13); + $compress .= $c; + continue; + } else if (strtolower(substr($c, 0, 4)) == ' $char) { + if ($char == '"' && $chars[$key - 1] != '\\' && !$is_apos) { + $is_quot = !$is_quot; + } else if ($char == '\'' && $chars[$key - 1] != '\\' && !$is_quot) { + $is_apos = !$is_apos; + } else if ($char == '/' && $chars[$key + 1] == '/' && !$is_quot && !$is_apos) { + $tmp = substr($tmp, 0, $key); + break; + } + } + } + $c .= $tmp; + } + } + $c = preg_replace('/[\\n\\r\\t]+/', ' ', $c); + $c = preg_replace('/\\s{2,}/', ' ', $c); + $c = preg_replace('/>\\s <', $c); + $c = preg_replace('/\\/\\*.*?\\*\\//i', '', $c); + $c = preg_replace('//', '', $c); + $compress .= $c; + } + return $compress; + } } \ No newline at end of file -- cgit v1.2.3