-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
Copy pathshowdoc_api.sh
64 lines (57 loc) · 2.01 KB
/
showdoc_api.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#! /bin/bash
#
# 文档说明: https://www.showdoc.com.cn/page/741656402509783
#
api_key="60fc53cea6af4758c1686cb22ba20566472255580" #api_key
api_token="0bbb5f564a9ee66333115b1abb8f8d541979489118" #api_token
url="https://www.showdoc.com.cn/server/?s=/api/open/fromComments" #同步到的url。使用www.showdoc.com.cn的不需要修改,使用私有版的请修改
#
#
#
#
#
# 如果第一个参数是目录,则使用参数目录。若无,则使用脚本所在的目录。
if [[ -z "$1" ]] || [[ ! -d "$1" ]] ; then #目录判断,如果$1不是目录或者是空,则使用当前目录
curren_dir=$(dirname $(readlink -f $0))
else
curren_dir=$(cd $1; pwd)
fi
#echo "$curren_dir"
# 递归搜索文件
searchfile() {
old_IFS="$IFS"
IFS=$'\n' #IFS修改
for chkfile in $1/*
do
filesize=`ls -l $chkfile | awk '{ print $5 }'`
maxsize=$((1024*1024*1)) # 1M以下的文本文件才会被扫描
if [[ -f "$chkfile" ]] && [ $filesize -le $maxsize ] && [[ -n $(file $chkfile | grep text) ]] ; then # 只对text文件类型操作
echo "正在扫描 $chkfile"
result=$(sed -n -e '/\/\*\*/,/\*\//p' $chkfile | grep showdoc) # 正则匹配
if [ ! -z "$result" ] ; then
txt=$(sed -n -e '/\/\*\*/,/\*\//p' $chkfile)
#echo "sed -n -e '/\/\*\*/,/\*\//p' $chkfile"
#echo $result
if [[ $txt =~ "@url" ]] && [[ $txt =~ "@title" ]]; then
echo -e "\033[32m $chkfile 扫描到内容 , 正在生成文档 \033[0m "
txt2=${txt//&/_this_and_change_}
# 通过接口生成文档
curl -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' "${url}" --data-binary @- <<CURL_DATA
from=shell&api_key=${api_key}&api_token=${api_token}&content=${txt2}
CURL_DATA
fi
fi
fi
if [[ -d $chkfile ]] ; then
searchfile $chkfile
fi
done
IFS="$old_IFS"
}
#执行搜索
searchfile $curren_dir
#
sys=$(uname)
if [[ $sys =~ "MS" ]] || [[ $sys =~ "MINGW" ]] || [[ $sys =~ "win" ]] ; then
read -s -n1 -p "按任意键继续 ... " # win环境下为照顾用户习惯,停顿一下
fi