-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathrender-readme.sh
executable file
·61 lines (53 loc) · 1.56 KB
/
render-readme.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
#!/bin/bash
scriptdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
basedir=$(echo "${scriptdir}" | grep -Po ".*(?=\/)")
mapfile -t arr < <(find "${basedir}/shared" -regex ".*\.xml$" | sort)
placeholder="___NEW_TABLE___"
readme_file="${basedir}/README.md"
temp_readme="/tmp/readme.tmp"
temp_table="/tmp/new_table.tmp"
truncate -s 0 "${temp_readme}"
truncate -s 0 "${temp_table}"
# functions
ett() {
echo -e "${1}" >>"${temp_table}"
}
make_table() {
echo "Generate shared catalog table"
ett "\n\n## Shared catalogs"
ett "\n|Title|File|"
ett "|---|---|"
for f in "${arr[@]}"; do
if [[ $(grep -Pc "<catalog\sdc:uri=.*>" "${f}") != 0 ]]; then
counter=$((counter + 1))
title=$(
tr '\n' ' ' <"${f}" |
grep -Po "<catalog\s.*<\/catalog>" |
grep -Po "(?<=title lang=\"en\">).*?(?=</title)"
)
file=$(echo "${f}" | grep -Po "(?<=${basedir}\/).*")
echo " Process ${file}"
ett "|${title}|${file}|"
fi
done
ett "\n${counter} catalogs shared\n"
}
remove_old_table() {
echo "Remove old table"
sed -ze "s/## Shared catalogs[^<]*catalogs shared/${placeholder}/g" \
"${readme_file}" >"${temp_readme}"
}
insert_new_table() {
echo "Insert new table"
sed -i "/${placeholder}/r ${temp_table}" "${temp_readme}"
}
clean_up() {
echo "Clean up README.md"
sed -i "s/${placeholder}//g" "${temp_readme}"
cat -s "${temp_readme}" >"${readme_file}"
}
# main
make_table
remove_old_table
insert_new_table
clean_up