forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Plugin.php
220 lines (202 loc) · 7.59 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/**
* 文章目录树
*
* @package MenuTree
* @author hongweipeng
* @version 0.6.1
* @link https://www.hongweipeng.com
*/
class MenuTree_Plugin implements Typecho_Plugin_Interface {
/**
* 索引ID
*/
public static $id = 1;
/**
* 目录树
*/
public static $tree = array();
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate() {
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array(__CLASS__, 'parse');
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){
$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() {
}
/**
* 解析
*
* @access public
* @param array $matches 解析值
* @return string
*/
public static function parseCallback( $match ) {
$parent = &self::$tree;
$html = $match[0];
$n = $match[1];
$menu = array(
'num' => $n,
'title' => trim( strip_tags( $html ) ),
'id' => 'menu_index_' . self::$id,
'sub' => array()
);
$current = array();
if( $parent ) {
$current = &$parent[ count( $parent ) - 1 ];
}
// 根
if( ! $parent || ( isset( $current['num'] ) && $n <= $current['num'] ) ) {
$parent[] = $menu;
} else {
while( is_array( $current[ 'sub' ] ) ) {
// 父子关系
if( $current['num'] == $n - 1 ) {
$current[ 'sub' ][] = $menu;
break;
}
// 后代关系,并存在子菜单
elseif( $current['num'] < $n && $current[ 'sub' ] ) {
$current = &$current['sub'][ count( $current['sub'] ) - 1 ];
}
// 后代关系,不存在子菜单
else {
for( $i = 0; $i < $n - $current['num']; $i++ ) {
$current['sub'][] = array(
'num' => $current['num'] + 1,
'sub' => array()
);
$current = &$current['sub'][0];
}
$current['sub'][] = $menu;
break;
}
}
}
self::$id++;
return "<span id=\"{$menu['id']}\" name=\"{$menu['id']}\"></span>" . $html;
}
/**
* 构建目录树,生成索引
*
* @access public
* @return string
*/
public static function buildMenuHtml( $tree, $include = true ) {
$menuHtml = '';
foreach( $tree as $menu ) {
if( ! isset( $menu['id'] ) && $menu['sub'] ) {
$menuHtml .= self::buildMenuHtml( $menu['sub'], false );
} elseif( $menu['sub'] ) {
$menuHtml .= "<li><a data-scroll href=\"#{$menu['id']}\" title=\"{$menu['title']}\">{$menu['title']}</a>" . self::buildMenuHtml( $menu['sub'] ) . "</li>";
} else {
$menuHtml .= "<li><a data-scroll href=\"#{$menu['id']}\" title=\"{$menu['title']}\">{$menu['title']}</a></li>";
}
}
if( $include ) {
$menuHtml = '<ul>' . $menuHtml . '</ul>';
}
return $menuHtml;
}
/**
* 判断是否是内容页,避免主页加载插件
*/
public static function is_content() {
static $is_content = null;
if($is_content === null) {
$widget = Typecho_Widget::widget('Widget_Archive');
$is_content = !($widget->is('index') || $widget->is('search') || $widget->is('date') || $widget->is('category') || $widget->is('author'));
}
return $is_content;
}
/**
* 插件实现方法
*
* @access public
* @return string
*/
public static function parse( $html, $widget, $lastResult ) {
$html = empty( $lastResult ) ? $html : $lastResult;
if (!self::is_content()) {
return $html;
}
$html = preg_replace_callback( '/<h([1-6])[^>]*>.*?<\/h\1>/s', array( 'MenuTree_Plugin', 'parseCallback' ), $html );
return $html;
}
/**
*为header添加css文件
*@return void
*/
public static function header() {
if (!self::is_content()) {
return;
}
$cssUrl = Helper::options()->pluginUrl . '/MenuTree/menutree.css';
echo '<link rel="stylesheet" type="text/css" href="' . $cssUrl . '" />';
}
/**
*为footer添加js文件
*@return void
*/
public static function footer() {
if (!self::is_content()) {
return;
}
if (Helper::options()->plugin('MenuTree')->jq_import) {
echo '<script src="//cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>';
}
$html = '<div class="in-page-preview-buttons in-page-preview-buttons-full-reader"><svg data-toggle="dropdown" class="dropdown-toggle icon-list" version="1.1" id="tree_nav" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="104.5 -245.5 51 51" style="enable-background:new 104.5 -245.5 50 50;" xml:space="preserve"><path d="M130.895-218.312c-0.922,0-1.842-0.317-2.59-0.951l-11.01-9.352c-1.684-1.43-1.889-3.955-0.459-5.638c1.43-1.684,3.953-1.89,5.639-0.459l8.42,7.152l8.42-7.152c1.686-1.43,4.211-1.225,5.639,0.459c1.43,1.684,1.225,4.208-0.459,5.638l-11.01,9.352C132.738-218.628,131.816-218.312,130.895-218.312z M133.486-206.289l11.008-9.352c1.684-1.43,1.889-3.955,0.459-5.638c-1.43-1.682-3.955-1.89-5.639-0.458l-8.418,7.152l-8.422-7.152c-1.686-1.431-4.209-1.225-5.639,0.459c-1.43,1.684-1.225,4.208,0.461,5.638l11.012,9.352c0.746,0.634,1.668,0.951,2.588,0.951C131.818-205.337,132.74-205.654,133.486-206.289z"/></svg><div class="dropdown-menu theme pull-right theme-white"id="toc-list"><h3>内容目录</h3><hr><div class="table-of-contents"><div class="toc"><ul><li>'. self::buildMenuHtml( self::$tree ) .'</div></div></div></li></ul></div>';
$js = Helper::options()->pluginUrl . '/MenuTree/dropdown.js';
echo <<<HTML
<script src="{$js}"></script>
<script type="text/javascript">
jQuery('body').append('$html');
</script>
HTML;
self::$id = 1;
self::$tree = array();
}
}