-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsitemap.sh
64 lines (59 loc) · 1.33 KB
/
sitemap.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
sitemap="sitemap.xml"
website_link="https://studeyang.tech/technotes"
ignore=(
sidebar.md
A类/Python/sidebar.md
B类/sidebar.md
C类/sidebar.md
D类/sidebar.md
README.md
A类/Python/README.md
A类/README.md
B类/README.md
C类/README.md
D类/README.md
coverpage.md
navbar.md
)
urlencode() {
local length="${#1}"
for ((i = 0; i < length; i++)); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_+-/]) printf "$c" ;;
*) printf "$c" | xxd -p -c1 | while read x; do printf "%%%s" "$x"; done ;;
esac
done
}
files=$(
git ls-files -z '*.md' |
xargs -0 -n1 -I{} -- git log -1 --format="%at {}" {} |
sort -r |
cut -d " " -f2-
)
items=""
for file in ${files[@]}; do
[[ ${ignore[@]/${file}/} != ${ignore[@]} ]] && continue
echo $file
encode=$(urlencode "${file::-3}")
link="$website_link/#/$encode"
date=$(git log -1 --format="%ad" --date="iso-strict-local" -- $file)
item="
<url>
<loc>$link</loc>
<lastmod>$date</lastmod>
</url>
"
items="$items $item"
done
now=$(git log -1 --format="%ad" --date="iso-strict-local")
sitemap_content="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">
<url>
<loc>$website_link</loc>
<lastmod>$now</lastmod>
</url>
$items
</urlset>"
echo "$sitemap_content" >$sitemap