-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_sitemap.sh
executable file
·53 lines (48 loc) · 1.17 KB
/
generate_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
#!/bin/bash
sitemap="sitemap.xml"
website_link="https://moto-penangf.github.io/documentation"
ignore=(
_sidebar.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