forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
113 lines (103 loc) · 3.78 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* 代码显示样式风格 可多用户不同风格
*
* @package CodeStyle
* @author hongweipeng
* @version 0.8.0
* @link https://www.hongweipeng.com
*/
class CodeStyle_Plugin implements Typecho_Plugin_Interface {
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate() {
Typecho_Plugin::factory('Widget_Archive')->header = array(__CLASS__, 'header');
Typecho_Plugin::factory('Widget_Archive')->footer = array(__CLASS__, 'footer');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate(){}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form){
//设置代码风格样式
$styles = array_map('basename', glob(dirname(__FILE__) . '/markdown/styles/*.css'));
$styles = array_combine($styles, $styles);
$name = new Typecho_Widget_Helper_Form_Element_Select('code_style', $styles, 'segmentfault.css', _t('选择你的代码风格'));
$form->addInput($name->addRule('enum', _t('必须选择配色样式'), $styles));
$showLineNumber = new Typecho_Widget_Helper_Form_Element_Checkbox('showln', array('showln' => _t('显示行号')), array('showln'), _t('是否在代码左侧显示行号'));
$form->addInput($showLineNumber);
/*$jq_import = new Typecho_Widget_Helper_Form_Element_Radio('jq_import', array(
0 => _t('不引入'),
1 => _t('引入')
), 1, _t('是否引入jQuery'), _t('此插件需要jQuery,如已有选择不引入避免引入多余jQuery'));
$form->addInput($jq_import->addRule('enum', _t('必须选择一个模式'), array(0, 1)));*/
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function render() {
}
/**
*为header添加css文件
*@return void
*/
public static function header() {
$style = Helper::options()->plugin('CodeStyle')->code_style;
$cssUrl = Helper::options()->pluginUrl . '/CodeStyle/markdown/styles/' . $style;
echo '<link rel="stylesheet" type="text/css" href="' . $cssUrl . '" />';
if (Helper::options()->plugin('CodeStyle')->showln) {
echo '<link rel="stylesheet" type="text/css" href="' . Helper::options()->pluginUrl. '/CodeStyle/markdown/highlightjs-line.css" />';
}
}
/**
*为footer添加js文件
*@return void
*/
public static function footer() {
$jsUrl = Helper::options()->pluginUrl . '/CodeStyle/markdown/highlight.pack.js';
$lineUrl = Helper::options()->pluginUrl . '/CodeStyle/markdown/highlightjs-line-numbers.min.js';
$showIn = Helper::options()->plugin('CodeStyle')->showln;
echo <<<HTML
<script type="text/javascript" src="{$jsUrl}"></script>
<script type="text/javascript">
hljs.initHighlightingOnLoad();
</script>
HTML;
if ($showIn) {
echo <<<HTML
<script type="text/javascript" src="{$lineUrl}"></script>
<script type="text/javascript">
hljs.initLineNumbersOnLoad();
</script>
HTML;
}
}
}