contentEx = __CLASS__ . '::parse'; Contents::pluginHandle()->excerptEx = __CLASS__ . '::parse'; Comments::pluginHandle()->contentEx = __CLASS__ . '::parse'; Archive::pluginHandle()->header = __CLASS__ . '::header'; Archive::pluginHandle()->footer = __CLASS__ . '::footer'; } /** * 禁用插件方法,如果禁用失败,直接抛出异常 */ public static function deactivate() { } /** * 获取插件配置面板 * * @param Form $form 配置面板 */ public static function config(Form $form) { $compatibilityMode = new Form\Element\Radio('compatibilityMode', [ 0 => _t('不启用'), 1 => _t('启用') ], 0, _t('兼容模式'), _t('兼容模式一般用于对以前没有使用Markdown语法解析的文章')); $form->addInput($compatibilityMode->addRule('enum', _t('必须选择一个模式'), [0, 1])); $styles = array_map('basename', glob(dirname(__FILE__) . '/res/styles/*.css')); $styles = array_combine($styles, $styles); $style = new Form\Element\Select('style', $styles, 'default.css', _t('代码配色样式')); $form->addInput($style->addRule('enum', _t('必须选择配色样式'), $styles)); } /** * 个人用户的配置面板 * * @param Form $form */ public static function personalConfig(Form $form) { } /** * 输出头部css */ public static function header($header, Archive $archive) { if ($archive->is('single')) { $cssUrl = Helper::options()->pluginUrl . '/HighlightJs/res/styles/' . Helper::options()->plugin('HighlightJs')->style; echo ''; } } /** * 输出尾部js */ public static function footer(Archive $archive) { if ($archive->is('single')) { $jsUrl = Helper::options()->pluginUrl . '/HighlightJs/res/highlight.pack.js'; echo ''; echo ''; } } /** * 插件实现方法 * * @access public * @return string */ public static function parse($text, $widget, $lastResult): string { $text = empty($lastResult) ? $text : $lastResult; if (!Helper::options()->plugin('HighlightJs')->compatibilityMode) { return $text; } if ($widget instanceof Archive || $widget instanceof Comments) { $isMarkdown = $widget instanceof Comments ? Helper::options()->commentsMarkdown : $widget->isMarkdown; return preg_replace_callback("/<(code|pre)(\s*[^>]*)>(.*?)<\/\\1>/is", function ($matches) use ($isMarkdown) { if ('code' == $matches[1] && !$isMarkdown) { $language = $matches[2]; if (!empty($language)) { if (preg_match("/^\s*(class|lang|language)=\"(?:lang-|language-)?([_a-z0-9-]+)\"$/i", $language, $out)) { $language = ' class="' . trim($out[2]) . '"'; } elseif (preg_match("/\s*([_a-z0-9]+)/i", $language, $out)) { $language = ' class="language-' . trim($out[1]) . '"'; } } return "
" . htmlspecialchars(trim($matches[3])) . "
"; } return $matches[0]; }, $text); } else { return $text; } } }