Skip to content

Commit

Permalink
可读性修正
Browse files Browse the repository at this point in the history
  • Loading branch information
jzwalk committed May 14, 2014
1 parent a5c81fd commit e7f090d
Showing 1 changed file with 84 additions and 66 deletions.
150 changes: 84 additions & 66 deletions Plugin.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

/**
* 自动为文章中出现的关键词添加链接
* @package Keywords
Expand All @@ -10,73 +11,90 @@
*/
class Keywords_Plugin implements Typecho_Plugin_Interface
{
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate(){
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('Keywords_Plugin', 'kwparse');
}

/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @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){
$keywords = new Typecho_Widget_Helper_Form_Element_Textarea('keywords',NULL,"",_t('关键词链接'),_t('以“关键词”|(英文半角分隔号)“链接”形式填写,每行一组。如:<br/>google|http://www.google.com'));
$keywords->input->setAttribute('style', 'width:345px;height:150px;');
$form->addInput($keywords);
$tagslink = new Typecho_Widget_Helper_Form_Element_Checkbox('tagslink',array('true'=>'自动替换'),NULL,_t('标签链接'),_t('文中若出现与本站标签相同的关键词则自动添加标签页链接'));
$form->addInput($tagslink);
$limits = new Typecho_Widget_Helper_Form_Element_Text('limits',NULL,"1",_t('链接频率'),_t('文中有多个重复关键词或标签时可限制链接替换次数'));
$limits->input->setAttribute('style', 'width:60px');
$form->addInput($limits->addRule('isInteger', _t('请填写整数数字')));
}
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
Typecho_Plugin::factory('Widget_Abstract_Contents')->contentEx = array('Keywords_Plugin','kwparse');
}

/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @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)
{
$keywords = new Typecho_Widget_Helper_Form_Element_Textarea('keywords',NULL,"",_t('关键词链接'),_t('以“关键词”|(英文半角分隔号)“链接”形式填写,每行一组。如:<br/>google|http://www.google.com'));
$keywords->input->setAttribute('style','width:345px;height:150px;');
$form->addInput($keywords);
$tagslink = new Typecho_Widget_Helper_Form_Element_Checkbox('tagslink',array('true'=>'自动替换'),NULL,_t('标签链接'),_t('文中若出现与本站标签相同的关键词则自动添加标签页链接'));
$form->addInput($tagslink);
$limits = new Typecho_Widget_Helper_Form_Element_Text('limits',NULL,"1",_t('链接频率'),_t('文中有多个重复关键词或标签时可限制链接替换次数'));
$limits->input->setAttribute('style','width:60px');
$form->addInput($limits->addRule('isInteger',_t('请填写整数数字')));
}

/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}

/**
* 关键词与标签替换
*
* @access public
* @param string $content
* @return string
*/
public static function kwparse($content,$widget,$lastResult)
{
$content = empty($lastResult)?$content:$lastResult;

$db = Typecho_Db::get();
$settings = Helper::options()->plugin('Keywords');
$limit = $settings->limits;

/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
$tagselect = $db->select()->from('table.metas')->where('type=?','tag');
$tagdata = $db->fetchAll($tagselect,array($widget->widget('Widget_Abstract_Metas'),'filter'));

public static function kwparse($content,$widget,$lastResult) {
$content = empty($lastResult)?$content:$lastResult;
$settings = Helper::options()->plugin('Keywords');
$limit = $settings->limits;
$db = Typecho_Db::get();
$tagselect = $db->select()->from('table.metas')->where('type = ?', 'tag');
$tagdata = $db->fetchAll($tagselect,array($widget->widget('Widget_Abstract_Metas'),'filter'));
$wlsets = explode("\n", $settings->keywords);
if (!empty($settings->keywords)&&true==strchr($settings->keywords, '|')) {
foreach ($wlsets as $wlset) {
$wlarray = explode("|", $wlset);
$content = preg_replace('/(?!<[^>]*)('.$wlarray[0].')(?![^<]*>)/i','<a href="'.$wlarray[1].'" target="_blank" title="'.$wlarray[0].'">'.$wlarray[0].'</a>',$content,$limit);
$wlsets = explode("\n", $settings->keywords);

if (!empty($settings->keywords)&&true==strchr($settings->keywords,'|')) {
foreach ($wlsets as $wlset) {
$wlarray = explode("|",$wlset);
$content = preg_replace('/(?!<[^>]*)('.$wlarray[0].')(?![^<]*>)/i','<a href="'.$wlarray[1].'" target="_blank" title="'.$wlarray[0].'">'.$wlarray[0].'</a>',$content,$limit);
}
}

if ($tagdata&&$settings->tagslink) {
foreach ($tagdata as $tag) {
$content = preg_replace('/(?!<[^>]*)('.$tag['name'].')(?![^<]*>)/i','<a href="'.$tag['permalink'].'" target="_blank" title="'.$tag['name'].'">'.$tag['name'].'</a>',$content,$limit);
}
}
if ($tagdata&&$settings->tagslink) {
foreach ($tagdata as $tag) {
$content = preg_replace('/(?!<[^>]*)('.$tag['name'].')(?![^<]*>)/i','<a href="'.$tag['permalink'].'" target="_blank" title="'.$tag['name'].'">'.$tag['name'].'</a>',$content,$limit);
}
}
return $content;
}
}

return $content;
}

}

0 comments on commit e7f090d

Please sign in to comment.