showdoc_api.sh 2.0 KB

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