diff --git a/GHbutton/Plugin.php b/GHbutton/Plugin.php index 763fa3ba..79280af6 100644 --- a/GHbutton/Plugin.php +++ b/GHbutton/Plugin.php @@ -1,20 +1,19 @@ contentEx = array('GHbutton_Plugin','btn_parse'); + Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('GHbutton_Plugin','btn_parse'); } /** * 禁用插件方法,如果禁用失败,直接抛出异常 - * + * * @static * @access public * @return void @@ -36,44 +36,48 @@ public static function deactivate(){} /** * 获取插件配置面板 - * + * * @access public * @param Typecho_Widget_Helper_Form $form 配置面板 * @return void */ public static function config(Typecho_Widget_Helper_Form $form) { - echo "

编辑文章或页面写入如<gb>用户名/项目名</gb>发布即可. 支持标签参数, 详见各选项说明.
示例: <gb user=\"typecho-fans\" type=\"star\" count=\"1\" size=\"1\" width=\"200\">plugins</gb>

"; + echo +'

' +._t('编辑文章或页面写入%s用户名%s项目名%s即可显示按钮状图标, 支持标签内指定各项参数
例:','<gb>','/','</gb>'). +' <gb user="typecho-fans" type="star" count="1" size="1" width="200">plugin</gb> +

'; $btn_user = new Typecho_Widget_Helper_Form_Element_Text('btn_user', - NULL,'',_t('GitHub用户名'),_t('默认调用的username, 可在标签中用参数user="*"覆盖')); - $btn_user->input->setAttribute('class','w-20'); + NULL,'',_t('GitHub用户名称'),_t('缺省调用username, 可在标签内指定参数user="-"覆盖')); + $btn_user->input->setAttribute('class','w-10'); $form->addInput($btn_user); $btn_type = new Typecho_Widget_Helper_Form_Element_Select('btn_type', - array('watch'=>_t('Watch(跟进项目)'),'star'=>_t('Star(收藏项目)'),'fork'=>_t('Fork(拷贝项目)'),'follow'=>_t('Follow(关注作者)'),'download'=>_t('Download(版本发布)')),'fork',_t('GitHub按钮种类'),_t('默认调用的按钮种类, 可在标签中用参数type="watch/star/fork/follow/download"覆盖')); + array('watch'=>_t('Watch(跟进项目)'),'star'=>_t('Star(收藏项目)'),'fork'=>_t('Fork(拷贝项目)'),'follow'=>_t('Follow(关注作者)'),'download'=>_t('Download(下载项目)'),'issue'=>_t('Issue(提交问题)')),'fork',_t('GitHub按钮种类'),_t('缺省按钮, 可用参数type="watch/star/fork/follow/download/issue"覆盖')); $form->addInput($btn_type); - $btn_count = new Typecho_Widget_Helper_Form_Element_Checkbox('btn_count', - array('1'=>_t('显示')),NULL,_t('GitHub按钮数字'),_t('默认是否显示按钮数字, 可在标签中用参数count="0/1"覆盖')); - $form->addInput($btn_count); + $btn_width = new Typecho_Widget_Helper_Form_Element_Text('btn_width', + NULL,'170',_t('iframe调用宽度'),_t('缺省宽度(单位px不用写), 标签内可用参数width="-"覆盖')); + $btn_width->input->setAttribute('style','width:47px'); + $form->addInput($btn_width->addRule('isInteger','请填写整数数字')); $btn_size = new Typecho_Widget_Helper_Form_Element_Checkbox('btn_size', - array('1'=>_t('大尺寸')),NULL,_t('GitHub按钮大小'),_t('默认是否使用大尺寸按钮, 可在标签中用参数size="0/1"覆盖')); + array(1=>_t('大尺寸')),NULL,_t('GitHub按钮大小'),_t('缺省是否使用大按钮, 可在标签内用参数size="0/1"覆盖')); $form->addInput($btn_size); + $btn_count = new Typecho_Widget_Helper_Form_Element_Checkbox('btn_count', + array(1=>_t('显示')),NULL,_t('GitHub按钮计数'),_t('缺省是否显示计数, 可在标签内用参数count="0/1"覆盖')); + $form->addInput($btn_count); + $btn_lang = new Typecho_Widget_Helper_Form_Element_Radio('btn_lang', - array('en'=>_t('英文'),'cn'=>_t('中文')),'en',_t('GitHub按钮语言'),_t('选择按钮文字语言, 可在标签中用参数lang="en/zh"覆盖')); + array('en'=>_t('英文'),'cn'=>_t('中文')),'en',_t('GitHub按钮语言'),_t('缺省按钮文本语言, 可在标签内用参数lang="en/cn"覆盖')); $form->addInput($btn_lang); - - $btn_width = new Typecho_Widget_Helper_Form_Element_Text('btn_width', - NULL,'170',_t('iframe框架宽度'),_t('默认iframe调用宽度, 单位px(不用写), 可在标签中用参数width="*"覆盖')); - $btn_width->input->setAttribute('class','w-10'); - $form->addInput($btn_width->addRule('isInteger','请输入整数数字')); } /** * 个人用户的配置面板 - * + * * @access public * @param Typecho_Widget_Helper_Form $form * @return void @@ -89,10 +93,9 @@ public static function personalConfig(Typecho_Widget_Helper_Form $form){} */ public static function btn_parse($content,$widget,$lastResult) { - $content = empty($lastResult)?$content:$lastResult; + $content = empty($lastResult) ? $content : $lastResult; - //替换gb标签 - if ($widget instanceof Widget_Archive) { + if ($widget instanceof Widget_Archive && false!==stripos($content,'')) { $content = preg_replace_callback('/<(gb)([^>]*)>(.*?)<\/\\1>/si',array('GHbutton_Plugin',"parseCallback"),$content); } @@ -102,58 +105,54 @@ public static function btn_parse($content,$widget,$lastResult) /** * 参数回调解析 * - * @param array $matches + * @param array $matche * @return string */ - public static function parseCallback($matches) + public static function parseCallback($matche) { $options = Helper::options(); $settings = $options->plugin('GHbutton'); - $url = $options->pluginUrl; - $param = trim($matches[2]); - $btn_repo = trim($matches[3]); + $param = trim($matche['2']); + $btn_repo = trim($matche['3']); //获取设置参数 $btn_user = $settings->btn_user; + if (strpos($btn_repo,'/')) { + $pair = explode('/',$btn_repo); + $btn_user = $pair['0']; + $btn_repo = $pair['1']; + } $btn_type = $settings->btn_type; - $btn_count = ($settings->btn_count)?'&count=true':''; - $btn_size = ($settings->btn_size)?'&size=large':''; + $btn_count = $settings->btn_count ? '&count=true' : ''; + $btn_size = $settings->btn_size ? '&size=large' : ''; + $btn_height = $settings->btn_size ? '30' : '20'; $btn_width = $settings->btn_width; - - //判断语言版本 - $html = ($settings->btn_lang=='cn')?$url.'/GHbutton/source/github-btn-cn.html':$url.'/GHbutton/source/github-btn.html'; - - //匹配输出参数 - if (!empty($param)) { - if (preg_match("/user=[\"']([\w-]*)[\"']/i",$param,$out)) { - $btn_user = trim($out[1])==''?$btn_user:trim($out[1]); + $html = $settings->btn_lang=='cn' ? '/GHbutton/source/github-btn-cn.html' : '/GHbutton/source/github-btn.html'; + + //匹配标签参数 + if ($param) { + if (preg_match('/user=["\']([\w-]*)["\']/i',$param,$out)) { + $btn_user = trim($out['1']) ? trim($out['1']) : $btn_user; } - if (preg_match("/type=[\"'](watch|star|fork|follow|download)[\"']/i",$param,$out)) { - $btn_type = trim($out[1])==''?$btn_type:trim($out[1]); + if (preg_match('/type=["\'](watch|star|fork|follow|download|issue)["\']/i',$param,$out)) { + $btn_type = trim($out['1']) ? trim($out['1']) : $btn_type; } - if (preg_match("/count=[\"']1[\"']/i",$param)) { - $btn_count = '&count=true'; + if (preg_match('/count=["\'](0|1)["\']/i',$param,$out)) { + $btn_count = trim($out['1'])=='0' ? '' : '&count=true'; } - if (preg_match("/size=[\"']1[\"']/i",$param)) { - $btn_size = '&size=large'; + if (preg_match('/size=["\'](0|1)["\']/i',$param,$out)) { + $btn_size = trim($out['1'])=='0' ? '' : '&size=large'; + $btn_height = trim($out['1'])=='0' ? '20' : '30'; } - if (preg_match("/lang=[\"']cn[\"']/i",$param)) { - $html= $url.'/GHbutton/source/github-btn-cn.html'; + if (preg_match('/lang=["\'](cn|en)["\']/i',$param,$out)) { + $html = trim($out['1'])=='cn' ? '/GHbutton/source/github-btn-cn.html' : '/GHbutton/source/github-btn.html'; } - if (preg_match("/width=[\"']([\w-]*)[\"']/i",$param,$out)) { - $btn_width = trim($out[1])==''?$btn_width:str_replace('px','',trim($out[1])); + if (preg_match('/width=["\']([\w-]*)["\']/i',$param,$out)) { + $btn_width = trim($out['1']) ? str_replace('px','',trim($out['1'])) : $btn_width; } } - //兼容格式 - if (strpos($btn_repo,'/')) { - $pair = explode('/',$btn_repo); - $btn_user = $pair[0]; - $btn_repo = $pair[1]; - } - - //替换为iframe - $replace = ''; + $replace = ''; return $replace; } diff --git a/GHbutton/README.md b/GHbutton/README.md index 308e8703..a75aead3 100644 --- a/GHbutton/README.md +++ b/GHbutton/README.md @@ -1,4 +1,7 @@ -### GHbutton 1.0.2 for Typecho 0.9 +### Typecho的Github项目按钮插件GHbutton +2017年1月10日更新至**v1.0.3**: +- 新版[github-buttons](https://github.com/mdo/github-buttons) +- 增加issue型按钮支持 -详细使用说明和效果演示见blog发布地址: -####http://www.jzwalk.com/archives/net/github-btn-typecho \ No newline at end of file +####详细说明与效果演示见blog发布地址: + > http://www.yzmb.me/archives/net/github-btn-typecho \ No newline at end of file diff --git a/GHbutton/source/github-btn-cn.html b/GHbutton/source/github-btn-cn.html index 50b16a40..c55c5f42 100644 --- a/GHbutton/source/github-btn-cn.html +++ b/GHbutton/source/github-btn-cn.html @@ -1 +1 @@ -GitHub按钮 \ No newline at end of file +GitHub Buttons \ No newline at end of file diff --git a/GHbutton/source/github-btn.html b/GHbutton/source/github-btn.html index 52fb480a..01183809 100644 --- a/GHbutton/source/github-btn.html +++ b/GHbutton/source/github-btn.html @@ -1 +1 @@ -GitHub Buttons \ No newline at end of file +GitHub Buttons \ No newline at end of file