This commit is contained in:
21
AbbrSlug/LICENSE
Normal file
21
AbbrSlug/LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 Hapi
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
118
AbbrSlug/Plugin.php
Normal file
118
AbbrSlug/Plugin.php
Normal file
@@ -0,0 +1,118 @@
|
||||
<?php
|
||||
/**
|
||||
* 自动生成缩略名
|
||||
*
|
||||
* @package AbbrSlug
|
||||
* @author 羽叶
|
||||
* @version 1.0.0
|
||||
* @link https://chenyeah.com
|
||||
*/
|
||||
class AbbrSlug_Plugin implements Typecho_Plugin_Interface
|
||||
{
|
||||
/**
|
||||
* 激活插件方法,如果激活失败,直接抛出异常
|
||||
*
|
||||
* @access public
|
||||
* @return void
|
||||
* @throws Typecho_Plugin_Exception
|
||||
*/
|
||||
public static function activate()
|
||||
{
|
||||
Typecho_Plugin::factory('Widget_Contents_Post_Edit')->write = array('AbbrSlug_Plugin', 'render');
|
||||
Typecho_Plugin::factory('Widget_Contents_Page_Edit')->write = array('AbbrSlug_Plugin', 'render');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用插件方法,如果禁用失败,直接抛出异常
|
||||
*
|
||||
* @static
|
||||
* @access public
|
||||
* @return void
|
||||
* @throws Typecho_Plugin_Exception
|
||||
*/
|
||||
public static function deactivate(){}
|
||||
|
||||
/**
|
||||
* 获取插件配置面板
|
||||
*/
|
||||
public static function config(Typecho_Widget_Helper_Form $form){
|
||||
/** 生成模式 */
|
||||
$alg = new Typecho_Widget_Helper_Form_Element_Radio(
|
||||
'alg',
|
||||
array(
|
||||
'crc32' => _t('crc32'),
|
||||
'crc16' => _t('crc16')
|
||||
),
|
||||
'crc32',
|
||||
_t('算法'),
|
||||
_t('目前支持crc16和crc32,其中crc32是默认的')
|
||||
);
|
||||
$form->addInput($alg);
|
||||
|
||||
$rep = new Typecho_Widget_Helper_Form_Element_Radio(
|
||||
'rep',
|
||||
array(
|
||||
'hex' => _t('16进制'),
|
||||
'dec' => _t('10进制')
|
||||
),
|
||||
'hex',
|
||||
_t('进制显示'),
|
||||
_t('其中16进制是默认的')
|
||||
);
|
||||
|
||||
|
||||
$form->addInput($rep);
|
||||
}
|
||||
|
||||
/**
|
||||
* 个人用户的配置面板
|
||||
*/
|
||||
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
|
||||
|
||||
/**
|
||||
* 插件实现方法
|
||||
*
|
||||
* @access public
|
||||
* @param array $contents 文章输入信息
|
||||
* @return void
|
||||
*/
|
||||
public static function render($contents)
|
||||
{
|
||||
if (empty($contents['slug'])) {
|
||||
$settings = Helper::options()->plugin('AbbrSlug');
|
||||
$title = $contents['title'];
|
||||
|
||||
if($settings->alg=='crc16'){
|
||||
/*
|
||||
* crc16 算法实现
|
||||
*/
|
||||
function crc16($string,$crc=0)
|
||||
{
|
||||
for ( $x=0; $x<strlen( $string ); $x++ ) {
|
||||
$crc = $crc ^ ord( $string[$x] );
|
||||
for ($y = 0; $y < 8; $y++) {
|
||||
if ( ($crc & 0x0001) == 0x0001 ) $crc = ( ($crc >> 1 ) ^ 0xA001 );
|
||||
else $crc = $crc >> 1;
|
||||
}
|
||||
}
|
||||
return $crc;
|
||||
}
|
||||
|
||||
if($settings->rep=='dec'){
|
||||
$result = crc16($title);
|
||||
}else{
|
||||
$result = dechex(crc16($title));
|
||||
}
|
||||
}else{
|
||||
if($settings->rep=='dec'){
|
||||
$result = crc32($title);
|
||||
}else{
|
||||
$result = dechex(crc32($title));
|
||||
}
|
||||
}
|
||||
$contents['slug'] = $result;
|
||||
}
|
||||
return $contents;
|
||||
}
|
||||
}
|
||||
48
AbbrSlug/README.md
Normal file
48
AbbrSlug/README.md
Normal file
@@ -0,0 +1,48 @@
|
||||
# AbbrSlug
|
||||
|
||||
对标 `hexo-abbrlink` 插件
|
||||
|
||||
以下参照了 `hexo-abbrlink` 的文档
|
||||
|
||||
## 主题 Mirages 不生效问题
|
||||
|
||||
将 21 22 行 代码替换如下
|
||||
```
|
||||
// Typecho_Plugin::factory('Widget_Contents_Post_Edit')->write = array('AbbrSlug_Plugin', 'render');
|
||||
// Typecho_Plugin::factory('Widget_Contents_Page_Edit')->write = array('AbbrSlug_Plugin', 'render');
|
||||
Typecho_Plugin::factory('Mirages_Plugin')->writePost = array('AbbrSlug_Plugin', 'render');
|
||||
Typecho_Plugin::factory('Mirages_Plugin')->writePage = array('AbbrSlug_Plugin', 'render');
|
||||
```
|
||||
|
||||
再将插件 禁用 重启即可
|
||||
|
||||
## 设置:
|
||||
|
||||
```
|
||||
alg -- 算法 (目前支持 crc16 和 crc32, 默认crc2)
|
||||
rep -- 进制显示 (sulg 会显示10进制和16进制)
|
||||
```
|
||||
|
||||
## 例子
|
||||
|
||||
> 生成的 slug 会像下面显示:
|
||||
|
||||
crc16 & hex
|
||||
https://tmp.com/posts/66c8.html
|
||||
|
||||
crc16 & dec
|
||||
https://tmp.com/posts/65535.html
|
||||
crc32 & hex
|
||||
https://tmp.com/posts/8ddf18fb.html
|
||||
|
||||
crc32 & dec
|
||||
https://tmp.com/posts/1690090958.html
|
||||
|
||||
## 限制
|
||||
|
||||
`crc16` 算法最大支持 65535 个文章 (个人博客来说已经完全够了)
|
||||
|
||||
## 感谢
|
||||
|
||||
[NoahDragon](https://github.com/NoahDragon)
|
||||
[Rozbo](https://github.com/Rozbo)
|
||||
Reference in New Issue
Block a user