typechod的一些function代码

2024-03-08 17:17:15 浏览数 (4)

代码语言:php复制
function themeConfig($form) {   
    $nolist = new Typecho_Widget_Helper_Form_Element_Text('nolist', NULL, NULL, _t('首页不显示某些特定分类'), _t('仅用在首页,首页不显示某些分类,可填入<b style="color: red;">mid</b>一个或多个数字,英文逗号分隔!!!'));
$form->addInput($nolist);
}

function themeFields($layout) {
    $description = new Typecho_Widget_Helper_Form_Element_Text('description', NULL, NULL, _t('描述'), _t('简单一句话描述'));$description->input->setAttribute('class', 'text w-100');
    $layout->addItem($description);
    $keywords = new Typecho_Widget_Helper_Form_Element_Text('keywords', NULL, NULL, _t('关键词'), _t('多个关键词用英文下逗号隔开'));$keywords->input->setAttribute('class', 'text w-100');
    $layout->addItem($keywords);
}

/**
 * 文章链接新窗口打开
 */

function parseContent($obj){ 
        $obj = preg_replace("/<a href="([^"]*)">/i", "<a href="\1" target="_blank" >", $obj);
        return $obj;
 }
 
 /** HTML压缩功能 */
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;
}

 /** HTML压缩功能调用 */
<?php $html_source = ob_get_contents(); ob_clean(); print compressHtml($html_source); ob_end_flush(); ?>

//后台编辑器添加功能
function themeFields($layout) {
    $img = new Typecho_Widget_Helper_Form_Element_Text('img', NULL, NULL, _t('自定义缩略图'), _t('在这里填入一个图片 URL 地址, 以添加显示本文的缩略图,留空则显示默认缩略图'));
    $img->input->setAttribute('class', 'w-100');
    $layout->addItem($img);
    $yin = new Typecho_Widget_Helper_Form_Element_Text('yin', NULL, NULL, _t('自定义视频'), _t('在这里填入一个视频 URL 地址'));
    $yin->input->setAttribute('class', 'w-100');
    $layout->addItem($yin); 
}

1 人点赞