Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown 编辑器 Editor.md、标签自动生成插件、文件下载插件 #23

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
674 changes: 674 additions & 0 deletions AutoTags/LICENSE.txt

Large diffs are not rendered by default.

135 changes: 135 additions & 0 deletions AutoTags/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 标签自动生成插件
*
* @package AutoTags
* @author DT27
* @version 1.0.0
* @link https://dt27.org
*/
class AutoTags_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('AutoTags_Plugin', 'write');

}

/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @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)
{

$isActive = new Typecho_Widget_Helper_Form_Element_Radio('isActive',
array(
'1' => '是',
'0' => '否',
),'1', _t('是否启用标签自动提取功能'), _t('自动提取功能在文章已存在标签时不生效.'));
$form->addInput($isActive);
}

/**
* 个人用户的配置面板
*
* @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 write($contents, $edit)
{
$html = $contents['text'];
$isMarkdown = (0 === strpos($html, '<!--markdown-->'));
if($isMarkdown){
$html = Markdown::convert($html);
}
$text = str_replace("\n", '', trim(strip_tags(html_entity_decode($html))));
$autoTags = Typecho_Widget::widget('Widget_Options')->plugin('AutoTags');
//插件启用,且未手动设置标签
if($autoTags->isActive == 1 && !$contents['tags']) {
Typecho_Widget::widget('Widget_Metas_Tag_Admin')->to($tags);
foreach($tags->stack as $tag){
$tagNames[] = $tag['name'];
}
//str_replace("\n", '', trim(strip_tags($contents['text'])))
//过滤 html 标签等无用内容
$postString = json_encode($text);
$ch = curl_init('http://api.bosonnlp.com/tag/analysis?space_mode=0&oov_level=0&t2s=0');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Accept: application/json',
'X-Token: fpm1fDvA.5220.GimJs8QvViSK'
)
);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
$ignoreTag = array('w', 'wkz', 'wky', 'wyz', 'wyy', 'wj', 'ww', 'wt', 'wd', 'wf', 'wn', 'wm', 'ws', 'wp', 'wb', 'wh', 'email', 'tel', 'id', 'ip', 'url', 'o', 'y', 'u', 'uzhe', 'ule', 'ugou', 'ude', 'usou', 'udeng', 'uyy', 'udh', 'uzhi', 'ulian', 'c', 'p', 'pba', 'pbei', 'd', 'dl', 'q', 'm', 'r', 'z', 'b', 'bl', 'a', 'ad', 'an', 'al', 'v', 'vd', 'vshi', 'vyou', 'vl', 'f', 's', 't', 'nl');
$sourceTags = array();
foreach($result[0]->tag as $key => $tag){
if(!in_array($tag, $ignoreTag)){
if(in_array($result[0]->word[$key], $tagNames)){
if(in_array($result[0]->word[$key], $sourceTags)) continue;
$sourceTags[] = $result[0]->word[$key];
}
}
}
$contents['tags'] = implode(',', array_unique($sourceTags));
if(count($contents['tags'])<3){
$ch = curl_init('http://api.bosonnlp.com/keywords/analysis?top_k=5');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$postString);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Accept: application/json',
'X-Token: fpm1fDvA.5220.GimJs8QvViSK'
)
);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
foreach($result as $re){
$a[] = $re[1];
}
$contents['tags'] = $contents['tags']?$contents['tags'].','.implode(',', $a):implode(',', $a);
}
}
return $contents;
}
}
17 changes: 17 additions & 0 deletions AutoTags/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# [AutoTags](https://dt27.org/php/autotags-for-typecho/)

**标签自动生成插件 For Typecho**

关键词提取 API:[玻森](http://bosonnlp.com/)

## Features

* [x] 新建及编辑文章时自动提取标签, 默认生成5个
* [x] 当已存在标签或已手动设置标签时不再自动生成
* [x] 优先匹配博客内现有标签
* [ ] 对已发布文章批量提取

## License

> Copyright © 2016 [DT27](https://dt27.org)
> License: [GNU General Public License v3.0](http://www.gnu.org/licenses/gpl-3.0.html)
7 changes: 4 additions & 3 deletions B3logForHacPai/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ public function commentReceiver(){
if ($post) {
$comment = array(
'cid' => $result->comment->articleId,
'created' => Helper::options()->gmtTime,
'created' => $result->comment->time,
'text' => $result->comment->content,
'author' => $result->comment->authorName,
'mail' => $result->comment->authorEmail,
'url' => $result->comment->authorURL,
'agent' => $this->request->getAgent(),
'ip' => $this->request->getIp(),
'agent' => $result->comment->ua,//$this->request->getAgent(),
'ip' => $result->comment->ip,//$this->request->getIp(),
'ownerId' => $post['authorId'],
'type' => 'comment',
'status' => 'approved',
'authorId' => $result->comment->isArticleAuthor?1:0,
);
//print_r($result->comment->articleid);
//$article = Typecho_Widget::widget('Widget_Users_Author@' . $this->cid, array('cid' => $result->comment->articleId));
Expand Down
2 changes: 1 addition & 1 deletion B3logForHacPai/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*
* @package B3log for HacPai
* @author DT27
* @version 1.0.0
* @version 1.0.1
* @link https://dt27.org/B3log-for-HacPai/
*/
class B3logForHacPai_Plugin implements Typecho_Plugin_Interface
Expand Down
68 changes: 68 additions & 0 deletions DownloadFile/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 文件下载插件,使下载的文件保持上传时的文件名。
*
* @package DownloadFile
* @author DT27
* @version 1.0.0
* @link https://dt27.org/
*/
class DownloadFile_Action extends Typecho_Widget
{

/**
* 构造函数
*
* @param mixed $request
* @param mixed $response
* @param null $params
*/
public function __construct($request, $response, $params = NULL)
{
parent::__construct($request, $response, $params);
}

/**
* 下载文件
*
* @cid 文件ID
* @throws Typecho_Db_Exception
*/
public function downloadFile(){

//throw new Typecho_Widget_Exception(_t('文件不存在'), 404);
//Typecho_Widget::widget('Widget_Contents_Attachment_Edit')->to($attachment);
if($this->request->filter('int')->cid) {
Typecho_Db::get()->fetchRow(Typecho_Db::get()->select()->from('table.contents')
->where('table.contents.type = ?', 'attachment')
->where('table.contents.cid = ?', $this->request->filter('int')->cid)
->limit(1), array($this, 'push'));
if (!$this->have()) {
throw new \InvalidArgumentException("文件不存在");
}
$info = unserialize($this->text);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: {$info['mime']}");
header("Content-Disposition: attachment; filename=\"{$info['name']}\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: {$info['size']}");
// download
$file = @fopen($_SERVER['DOCUMENT_ROOT'].$info['path'],"rb");
if ($file) {
while(!feof($file)) {
print(fread($file, 1024*8));
flush();
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
}
}
}
Loading