-
Notifications
You must be signed in to change notification settings - Fork 11
/
base.sh
executable file
·303 lines (271 loc) · 7.87 KB
/
base.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/bin/echo Warinng: this library should be sourced!
CFG_PATH=$(cd $(dirname "${BASH_SOURCE[0]}")&&pwd)
SCRIPT_NAME=$(basename ${CFG_PATH})
SCRIPT_VERSION=$(stat -c %Y $(ls -t ${CFG_PATH}/*.sh |head -n 1 ))
# 允许直接调用第三方库
export PATH=$PATH:${CFG_PATH}/libs
function get-cfg-option ()
{
option="$@"
egrep "^${option}=" ${CFG_PATH}/lctt.cfg |cut -d "=" -f 2-
}
function reset-lctt-path()
{
export LCTT=$(get-cfg-option ProjectRoot)
if [[ -z "${LCTT}" || ! -d "${LCTT}" ]]; then
LCTT=$(find $HOME -iname TranslateProject 2>/dev/null |\
awk -F "TranslateProject"IGNORECASE=1 '{print $1}')
fi
}
function get-lctt-path()
{
if [[ -z ${LCTT} ]];then
reset-lctt-path
fi
echo ${LCTT}
}
function file-translating-p ()
{
local file="$*"
if head -n 1 "${file}" |grep '^\[#\]:'>/dev/null 2>&1 ;then
# 新模板
head -n 12 "$file" |grep '^\[#\]: translator: ([^[:space:]].*)' >/dev/null 2>&1
else
# 旧模板
head -n 3 "$file" |grep -E -i "translat|fanyi|翻译" >/dev/null 2>&1
fi
}
function file-translating-by-me-p()
{
local file="$*"
if head -n 1 "${file}" |grep '^\[#\]:'>/dev/null 2>&1 ;then
# 新模板
head -n 12 "$file" |grep '^\[#\]: translator: ([^[:space:]].*)' |grep $(get-github-user) >/dev/null 2>&1
else
# 旧模板
head -n 3 "$file" |grep -E -i "translat|fanyi|翻译"|grep $(get-github-user) >/dev/null 2>&1
fi
}
function search-similar-articles ()
{
if [[ $# -eq 0 ]];then
cat <<EOF
该函数可用于检查是否存在重复的文章。
Usage: $0 文件名称
EOF
return 1
fi
url="$*"
clean_url=${url%%[?#]*} # 去掉URL中?或#后面的内容
clean_url=${clean_url##http*://} # 去掉http://或https://
if [[ "${clean_url}" == */ ]];then
clean_url=${clean_url%%/} # 去掉最后的/
fi
echo clean_url= ${clean_url}
# find $(get-lctt-path) -type f -name "[0-9]*.md" -print0 |xargs -I{} -0 grep -i "via:" "{}" |cut -d ":" -f2- |grep -i "${clean_url}"
(cd $(get-lctt-path) && git grep -E "via: *https?://${clean_url}")
}
function command-exist-p()
{
command -v "$@" >/dev/null 2>/dev/null
}
function continue-p()
{ read -p "$*,CONTINUE? [y/n]" CONT
case $CONT in
[yY]*)
break
;;
*)
exit 1
;;
esac
}
function get-github-user()
{
local user=$(get-cfg-option GithubUser)
if [[ -z ${user} ]];then
user=$(git config --list |grep "user.name="|awk -F "=" '{print $2}')
fi
echo ${user}
}
function get-browser()
{
local browser=$(get-cfg-option Browser)
if [[ -z ${browser} ]];then
browser="firefox"
fi
echo ${browser}
}
function get-editor()
{
local editor=$(get-cfg-option Editor)
if [[ -z ${editor} ]];then
editor="vi"
fi
echo ${editor}
}
function git-branch-exist-p()
{
local branch="$*"
git branch -a |grep -E "${branch}" >/dev/null
}
function get-domain-from-url ()
{
local url="$*"
echo "${url}"|sed 's#^https*://\([^/]*\).*$#\1#'
}
function url-blocked-p()
{
local url="$*"
local Allowed_domain=$(get-cfg-option AllowedDomains)
local domain=$(get-domain-from-url "$url")
# echo "$blocked_domain" |grep "$domain" >/dev/null
[[ "${Allowed_domain}" != *"${domain}"* ]] # 可以用 == 来匹配
}
function warn ()
{
echo "$*" >&2
}
function git-get-current-branch ()
{
git branch |grep "*" |cut -d " " -f2
}
function filename-to-branch ()
{
local operation=$1
shift
local filename="$*"
local code=$(echo "${filename}"|base64 -w 0)
echo "${operation}-${code}"
}
# 解析branch中包含的操作类型 add/translate/revert
function git-branch-to-operation()
{
local branch="$*"
local operation=$(echo "${branch}"|cut -d "-" -f1)
echo "${operation}"
}
# 解析branch中包含的文件名信息
function git-branch-to-filename()
{
local branch="$*"
local code=$(echo "${branch}" |cut -d "-" -f2)
echo "${code}"|base64 -d
}
# 返回branch参数中正在编辑文件的 *绝对路径*
function git-branch-to-file-path()
{
local branch="$*"
local filename=$(git-branch-to-filename "${branch}")
find "$(get-lctt-path)" -name "${filename}"
}
# 根据当前branch得到当前编辑的文件 *绝对路径*
function git-current-branch-to-file-path()
{
# 在子shell中操作,不要修改原work directory
(
cd "$(get-lctt-path)"
local branch=$(git-get-current-branch)
git-branch-to-file-path "${branch}"
)
}
# 根据时间以及文章title转换成标准的文件名
function date-title-to-filename()
{
local date="$1"
shift
title=$(echo "$*" |sed 's/[^0-9a-zA-Z.,()‘_ -]/-/g'|sed 's/-*$//') # 特殊字符换成-号,最后的-去掉
echo "${date} ${title}.md"
}
# 为文件加上翻译中的标记
function mark-file-as-tranlating()
{
local filename="$*"
local git_user=$(get-github-user)
if head -n 1 "${filename}" |grep '^\[#\]:'>/dev/null 2>&1 ;then
# 新模板
sed -i "s/\[#\]: translator: \" \"/\[#\]: translator: \"${git_user}\"/g" "${filename}"
else
# 旧模板
sed -i "1i translating by ${git_user}" "${filename}"
fi
sed -i "/-------------------------------/,$ s/译者ID/${git_user}/g" "${filename}"
}
# 根据url和author获取作者链接
function get-author-link()
{
url="$1"
domain=$(get-domain-from-url "${url}")
author="$2"
# 在子shell中操作,不要影响原shell的工作目录
(
cd $(get-lctt-path)
# 选择最多的url作为author link
git grep -iEc "via: *https*://${domain}|\[${author}\]"|grep ":2$"|cut -d":" -f1|xargs -I{} grep "\[a\]:" '{}' |sort |uniq -c |sort -n |tail -n 1 |cut -d":" -f2-|sed 's/^[[:space:]]*//'
# git grep -il "${domain}"|xargs -I{} grep -il "\[${author}\]" '{}' |tail -n 1 |xargs -I{} grep '\[a\]:' '{}' |cut -d ":" -f2-
)
}
# 判断文件的类型是tech还是talk
function guess-article-type()
{
local article="$*"
if grep '```' "${article}" >/dev/null;then
echo "tech"
else
echo "talk"
fi
}
# 获取git仓库remote中的user
function git-get-remote-user()
{
local remote="$*"
local user_repo=$(git remote -v |grep "${remote}" |grep fetch |awk '{print $2}')
if [[ "${user_repo}" =~ ^[email protected]: ]];then
user_repo=${user_repo##*:}
elif [[ "$user_repo" =~ ^https://github.com/ ]];then
user_repo=${user_repo#https://github.com/}
fi
local user=${user_repo%%/*}
echo ${user}
}
# 获取git仓库remote中的repo
function git-get-remote-repo()
{
local remote="$*"
local user_repo=$(git remote -v |grep "${remote}" |grep fetch |awk '{print $2}')
if [[ "${user_repo}" =~ ^[email protected]: ]];then
user_repo=${user_repo##*:}
elif [[ "$user_repo" =~ ^https://github.com/ ]];then
user_repo=${user_repo#https://github.com/}
fi
local repo=${user_repo#*/}
repo=${repo%.git}
echo ${repo}
}
# 将URL从相对路径改成绝对路径
function get-abstract-url()
{
local baseUrl="$1"
if [[ "${baseUrl}" == */ ]];then
baseUrl="${baseUrl}index.html" # 补上默认页面,这一步同一格式到页面
fi
local rootUrl=$(echo "${baseUrl}"|sed 's#^\([a-z]\+://[^/]*\).*$#\1#')
local relativeUrl="$2"
(
shopt -s extglob
if [[ ${relativeUrl} == +([a-zA-Z])://* ]];then
echo ${relativeUrl}
elif [[ ${relativeUrl} == /* ]];then
echo "${rootUrl}${relativeUrl}"
elif [[ ${relativeUrl} == ../* ]];then
baseUrl=${baseUrl%/?*} # 替代dirname
relativeUrl=${relativeUrl#*/}
get-abstract-url ${baseUrl} ${relativeUrl}
elif [[ ${relativeUrl} == +([a-z0-9A-Z])/* ]];then
echo "${baseUrl}/${relativeUrl}"
else
echo "ERROR URL:${relativeUrl}" >&2
exit 1
fi
)
}