diff options
| author | BigCoke233 <hi@guhub.cn> | 2020-07-14 14:15:59 +0800 |
|---|---|---|
| committer | BigCoke233 <hi@guhub.cn> | 2020-07-14 14:15:59 +0800 |
| commit | d75331d8b8816f0806ca324b550483cbadaf656f (patch) | |
| tree | 804ff5f554ac0917a3abde0e9526466da508e0d2 | |
| parent | 5690f8c59778373553a2a4b96b18900cc41d13b4 (diff) | |
:zap: Add: 加入 HTML 压缩功能
| -rw-r--r-- | README.md | 10 | ||||
| -rw-r--r-- | includes/footer.php | 14 | ||||
| -rw-r--r-- | includes/head.php | 3 | ||||
| -rw-r--r-- | index.php | 2 | ||||
| -rw-r--r-- | libs/Utils.php | 59 | ||||
| -rw-r--r-- | themeConfig.php | 12 |
6 files changed, 89 insertions, 11 deletions
@@ -148,6 +148,16 @@ $ git clone https://github.com/BigCoke233/miracles.git - 新增:可以使用 TimThumb 裁剪文章缩略图了(需要在高级设置 themeConfig.php 里打开) - 优化:归档页面 meta 元信息 +**v20200713A** + +- 优化:改进番剧页面(Done by Kengwang) +- 修复:解决随机颜文字可能报错的问题 +- 修复:评论列表缺失 #comments id + +**v20200714A** + +- 修复:删除不必要的判断 + </details> ## 📝 计划 diff --git a/includes/footer.php b/includes/footer.php index 312249e..82d39dc 100644 --- a/includes/footer.php +++ b/includes/footer.php @@ -12,7 +12,7 @@ $faces = explode("&&",$faces); $faces = "<span class=\"anime-face\">".$faces[mt_rand(0,count($faces)-1)]."</span>"; } - if($faces && $this->options->build_time)echo $faces; + if($faces && $this->options->build_time) echo $faces; ?></p> </div> </footer> @@ -26,7 +26,7 @@ if($this->options->customCDN): $custom=$this->options->customCDN; else: $custom=Helper::options()->themeUrl("","Miracles"); endif; Utils::addRequires($js_files,"js",$this->options->CDN,$custom); ?> - <!-- Options --> + <!-- Options --><!--<nocompress>--> <script>var navSlide = <?php if($this->options->navSlide==1):?>false<?php else:?>true<?php endif;?>; var panguLoadAllow = <?php if($this->options->pangu==1):?>false<?php else:?>true<?php endif;?>; var allowNavAero = <?php if($this->options->navAero==1):?>false<?php else:?>true<?php endif;?>; @@ -37,7 +37,7 @@ if($faviconDarkExist=true): echo 'true'; else: echo 'false'; endif; ?>; <?php if($this->options->pjax && $this->options->pjax!=0) :?>var loadPjax = true; beforePjax = function() {NProgress.start();}; - afterPjax = function() {owoLoad();<?php $this->options->pjax_complete(); ?>};<?php endif; ?></script> + afterPjax = function() {owoLoad();<?php $this->options->pjax_complete(); ?>};<?php endif; ?></script><!--</nocompress>--> <!-- Script that must be after--> <?php $js_files=array("miracles.min","cmt.miracles"); @@ -45,15 +45,19 @@ Utils::addRequires($js_files,"js",$this->options->CDN,$custom); ?> <!-- JavaScript --> - <script> + <!--<nocompress>--><script> <?php if($this->options->build_time)echo "startTime(\"".$this->options->build_time."\");" ?> - </script> + </script><!--</nocompress>--> <!-- Send News and Loaders --> <script><?php if($this->is('post') || $this->is('page')): ?>owoLoad();<?php endif; ?><?php $this->options->jsEcho(); ?></script> <!-- Others --> <?php $this->footer(); ?> </body> </html> +<?php if($GLOBALS['miraclesIfCompressHTML']=='on') { + $html_source = ob_get_contents(); //获取 ob 截取内容 + ob_clean(); print Utils::compressHtml($html_source); ob_end_flush(); //完成截取、压缩 HTML +} ?> <!-- Powered by Typecho, diff --git a/includes/head.php b/includes/head.php index 3a6aa92..41f143d 100644 --- a/includes/head.php +++ b/includes/head.php @@ -1,4 +1,5 @@ -<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; ?> +<?php if (!defined('__TYPECHO_ROOT_DIR__')) exit; +if($GLOBALS['miraclesIfCompressHTML']=='on') ob_start(); //ob 截取开始 ?> <!DOCTYPE HTML> <html<?php if($this->options->grayTheme && $this->options->grayTheme=1): ?> class="html-filter-gray"<?php endif; ?>> <head> @@ -22,7 +22,7 @@ $this->need('includes/header.php'); <div class="col-md-6 post-banner-box"> <a href="<?php $this->permalink(); ?>" class="post-link"> <div class="post-banner"> - <img src="<?php Utils::indexTheme('images/loading/'.$this->options->loading_image.'.gif') ?>" data-gisrc="<?php Utils::postBanner($this); ?>"> + <img src="<?php Utils::indexTheme('images/loading/'.$this->options->loading_image.'.gif') ?>" data-gisrc="<?php Utils::postBanner($this, $post->options->randomBanner); ?>"> </div> </a> </div> 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>-->.*?<!--<\/nocompress>-->|<nocompress>.*?<\/nocompress>|<pre.*?\/pre>|<textarea.*?\/textarea>|<script.*?\/script>)/msi', $html_source, -1, PREG_SPLIT_DELIM_CAPTURE); + $compress = ''; + foreach ($chunks as $c) { + if (strtolower(substr($c, 0, 19)) == '<!--<nocompress>-->') { + $c = substr($c, 19, strlen($c) - 19 - 20); + $compress .= $c; + continue; + } else if (strtolower(substr($c, 0, 12)) == '<nocompress>') { + $c = substr($c, 12, strlen($c) - 12 - 13); + $compress .= $c; + continue; + } else if (strtolower(substr($c, 0, 4)) == '<pre' || strtolower(substr($c, 0, 9)) == '<textarea') { + $compress .= $c; + continue; + } else if (strtolower(substr($c, 0, 7)) == '<script' && strpos($c, '//') != false && (strpos($c, "\r") !== false || strpos($c, "\n") !== false)) { + $tmps = preg_split('/(\r|\n)/ms', $c, -1, PREG_SPLIT_NO_EMPTY); + $c = ''; + foreach ($tmps as $tmp) { + if (strpos($tmp, '//') !== false) { + if (substr(trim($tmp), 0, 2) == '//') { + continue; + } + $chars = preg_split('//', $tmp, -1, PREG_SPLIT_NO_EMPTY); + $is_quot = $is_apos = false; + foreach ($chars as $key => $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 diff --git a/themeConfig.php b/themeConfig.php index 89357b0..ba13cfe 100644 --- a/themeConfig.php +++ b/themeConfig.php @@ -7,12 +7,16 @@ * 通常 'on' 表示开启;'off' 则表示关闭 */ +/* 字体 */ + //主题字体 CDN $GLOBALS['miraclesFontCDN'] = array( 'if' => 'on', //是否开启字体 CDN(使用思源宋体/黑体) 'cdn' => 'google_font' //默认 Google Font,可选项:google_font / font_lolinet,或者直接输入字体文件 url ); +/* 文章缩略图 */ + //是否剪裁文章缩略图 //使用 TimThumb 剪裁文章缩略图能达到最好效果,但会消耗服务器资源 $GLOBALS['miraclesIfTimThumb'] = 'off'; @@ -33,4 +37,10 @@ $GLOBALS['miraclesTimThumbAllowList'] = array ( 'imageshack.us', 'tinypic.com', 'jsdelivr.net' -);
\ No newline at end of file +); + +/* 页面压缩 */ + +//是否压缩 HTML 源代码 +//压缩能优化页面传输速度,但可能会给服务器增加一定负担(不过也还好 +$GLOBALS['miraclesIfCompressHTML'] = 'on';
\ No newline at end of file |
