diff options
Diffstat (limited to 'make_sitemap.sh')
-rwxr-xr-x | make_sitemap.sh | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/make_sitemap.sh b/make_sitemap.sh new file mode 100755 index 00000000..4777f948 --- /dev/null +++ b/make_sitemap.sh | |||
@@ -0,0 +1,67 @@ | |||
1 | #!/bin/sh | ||
2 | |||
3 | # This initial version builds on code from ssg4 | ||
4 | # copyright is as follows: | ||
5 | # ----- | ||
6 | # https://www.romanzolotarev.com/bin/ssg4 | ||
7 | # Copyright 2018 Roman Zolotarev <hi@romanzolotarev.com> | ||
8 | # | ||
9 | # Permission to use, copy, modify, and/or distribute this software for any | ||
10 | # purpose with or without fee is hereby granted, provided that the above | ||
11 | # copyright notice and this permission notice appear in all copies. | ||
12 | # | ||
13 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
14 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
15 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
16 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
17 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
18 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
19 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
20 | # ----- | ||
21 | |||
22 | list_pages(){ | ||
23 | cd rendered && find . -type f ! -path '*/.*' ! -path '*/_*' -name '*.html' | sed 's#^./##;#' | ||
24 | } | ||
25 | |||
26 | main(){ | ||
27 | dst=rendered | ||
28 | base_url="$4" | ||
29 | date=$(date +%Y-%m-%d) | ||
30 | urls=$(list_pages "$src") | ||
31 | |||
32 | test -n "$urls" && | ||
33 | render_sitemap "$urls" "$base_url" "$date" > "$dst/sitemap.xml" | ||
34 | |||
35 | print_status 'url' 'urls' "$urls" >&2 | ||
36 | echo >&2 | ||
37 | } | ||
38 | |||
39 | print_status() { | ||
40 | test -z "$3" && printf 'no %s' "$2" && return | ||
41 | |||
42 | echo "$3" | awk -v singular="$1" -v plural="$2" ' | ||
43 | END { | ||
44 | if (NR==1) printf NR " " singular | ||
45 | if (NR>1) printf NR " " plural | ||
46 | }' | ||
47 | } | ||
48 | |||
49 | render_sitemap() { | ||
50 | urls="$1" | ||
51 | base_url="$2" | ||
52 | date="$3" | ||
53 | |||
54 | echo '<?xml version="1.0" encoding="UTF-8"?>' | ||
55 | echo '<urlset' | ||
56 | echo 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' | ||
57 | echo 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9' | ||
58 | echo 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"' | ||
59 | echo 'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' | ||
60 | echo "$urls" | | ||
61 | sed -E 's#^(.*)$#<url><loc>'"$base_url"'/\1</loc><lastmod>'\ | ||
62 | "$date"'</lastmod><priority>1.0</priority></url>#' | ||
63 | echo '</urlset>' | ||
64 | } | ||
65 | |||
66 | main "$@" | ||
67 | |||