Skip to content

Commit 2d569b5

Browse files
committed
feat: add link_solution script
the script has the two functions: 1. Link solution page to each practice page where there is no solution link. 2. Correct solution link address (i.e. non-corresponding address).
1 parent af2a2a6 commit 2d569b5

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

scripts/link_solution

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Function:
4+
# 1. Link solution page to each practice page where there is no solution link
5+
# 2. Correct link address (i.e. non-corresponding address)
6+
#
7+
# Usage:
8+
# locate in the project root directory and execute the file
9+
#
10+
# Tips:
11+
# Use 'git diff' to show the change after executing this command
12+
13+
set -e # exit on error
14+
15+
SOLUTION_DIR="solutions"
16+
ZH_DIR="zh-CN/src"
17+
EN_DIR="en/src"
18+
LINK_PREFIX="https://github.com/sunface/rust-by-practice/blob/master"
19+
20+
# create an array of the relative address of all markdown file about practice in both Chinese and English
21+
read -ra markdown_files <<< "$(echo "${ZH_DIR}"/**/*.md "${EN_DIR}"/**/*.md)"
22+
23+
for mdfile in "${markdown_files[@]}"; do
24+
# if the corresponding solution file exist, check whether the practice file contians a
25+
# link to solution or not. If not, create a link
26+
solution_file=$( sed "s#\(${ZH_DIR}\|${EN_DIR}\)#${SOLUTION_DIR}#g" <<< "${mdfile}" )
27+
if [[ -f "${solution_file}" ]]; then # exist solution file
28+
echo -n "${mdfile}"
29+
correct_link="${LINK_PREFIX}/${solution_file}"
30+
found=$(grep -Ei '^\s*>.*https://.*solution.*$' < "${mdfile}" || true)
31+
if [[ -z "${found}" ]]; then # not found link
32+
echo >> "${mdfile}" # some file ends without newline character
33+
if [[ "${mdfile}" == "${ZH_DIR}"/* ]]; then # written in Chinese
34+
echo "> 你可以在[这里](${correct_link})找到答案(在 solutions 路径下)" >> "${mdfile}"
35+
elif [[ "${mdfile}" == "${EN_DIR}"/* ]]; then # written in English
36+
echo "> You can find the solutions [here](${correct_link})(under the solutions path), but only use it when you need it :)" >> "${mdfile}"
37+
fi
38+
else # found link in file
39+
# simply correct all matched links
40+
sed -i 's#^\(.*\)>\(.*\)(https://[^)]*)\(.*\)solution\(.*\)$#\1>\2('"${correct_link}"')\3solution\4#' "${mdfile}"
41+
fi
42+
echo " --done"
43+
fi
44+
done

0 commit comments

Comments
 (0)