Skip to content

Instantly share code, notes, and snippets.

@legendlee
Created December 2, 2012 07:32
Show Gist options
  • Save legendlee/4187609 to your computer and use it in GitHub Desktop.
Save legendlee/4187609 to your computer and use it in GitHub Desktop.
让Typecho支持Gist的插件
<?php
/**
* 替换内容源代码中的 Gist 标签为 Gist 脚本
* Gist 标签格式为: [gist gist地址]
*
* @package GistEcho
* @author woody
* @version 1.0.0
* @link http://www.fanrenxiu.com
*/
class GistEcho_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('GistEcho_Plugin', 'parse');
Typecho_Plugin::factory('Widget_Abstract_Contents')->excerptEx = array('GistEcho_Plugin', 'parse');
Typecho_Plugin::factory('Widget_Abstract_Comments')->contentEx = array('GistEcho_Plugin', 'parse');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @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) {}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form) {}
/**
* 解析
*
* @access public
* @param array $matches 解析值
* @return string
*/
public static function parseCallback($matches) {
return '<script src="'.trim($matches[2]).'.js"></script>';
}
/**
* 插件实现方法
*
* @access public
* @return string
*/
public static function parse($text, $widget, $lastResult) {
$text = empty($lastResult) ? $text : $lastResult;
if ($widget instanceof Widget_Archive || $widget instanceof Widget_Abstract_Comments) {
return preg_replace_callback("/\[gist( |&nbsp;)([a-zA-z0-9\:\/\.]+)\]/", array('GistEcho_Plugin', 'parseCallback'), $text);
} else {
return $text;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment