forked from LCTT/lctt-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_translate.sh
executable file
·64 lines (57 loc) · 1.58 KB
/
auto_translate.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
#!/bin/bash
set -e
source base.sh
while getopts :rf: OPT; do
case $OPT in
r|+r)
replace_flag="True"
;;
f|+f)
source_file="$OPTARG"
;;
*)
echo "usage: ${0##*/} [+-rf ARG} [--] ARGS..."
exit 2
esac
done
shift $(( OPTIND - 1 ))
OPTIND=1
if [[ "${replace_flag}" == "True" ]];then
dest_file="${source_file}"
else
dest_file="/dev/tty"
fi
function do_translate()
{
article="$*"
position="元数据" # 其他可能值包括 "正文","引用","结尾"
while read line
do
echo "${line}"
if [[ "${position}" == "元数据" ]];then
if [[ "${line}" == \[#\]:* ]];then
continue
else
position="正文"
fi
fi
if [[ "${line}" == '```'* ]];then
if [[ "${position}" != "引用" ]];then
position="引用"
echo 引用----------------------------------------------------
continue
else
position="正文"
fi
fi
if [[ "${line}" == "--------------------------------------------------------------------------------"* ]];then
position="结尾"
continue
fi
if [[ "${position}" == "正文" && "${line}" == *[a-zA-Z]* ]];then
youdao.sh "${line}" # 至少包含一个英文字母才需要翻译
fi
done < <(cat "${article}")
}
translated_content="$(do_translate "${source_file}")"
echo "${translated_content}" > "${dest_file}"