home · contact · privacy
Re-write date handling, remove dependency on filesystem lastmod dates.
[redo-blog] / processor / helpers.sh
1 #!/bin/sh
2
3 escape_html() {
4   out=`python3 -c 'import sys, html; print(html.escape(sys.argv[1]))' "$1"`
5   printf "%s" "$out"
6 }
7
8 read_and_escape_file() {
9   in=$(cat "$1")
10   escape_html "$in"
11 }
12
13 get_uuid_from_meta_file() {
14   probable_uuid=$(cat "$1" | head -1)
15   if printf "$probable_uuid" | grep -Eq "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"; then
16     printf "$probable_uuid"
17   else
18     echo "Malformed UUID in meta file." >&2
19     exit 1
20   fi
21 }
22
23 get_creation_date_from_meta_file_seconds() {
24   cat "$1" | sed -n '2p' | cut -d'_' -f1
25 }
26
27 get_creation_date_from_meta_file_nanoseconds() {
28   cat "$1" | sed -n '2p'
29 }
30
31 get_lastmod_date_from_meta_file() {
32   cat "$1" | sed -n '4p'
33 }
34
35 escape_url() {
36   out=`python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.argv[1]))' "$1"`
37   printf "%s" "$out"
38 }
39
40 get_basepath() {
41   url_file="$1"url
42   redo-ifchange "$url_file"
43   base_url=`cat "$url_file" | head -1`
44   url_protocol=`echo $base_url | cut -d ':' -f 1`
45   url_basepath=`echo $base_url | cut -d '/' -f 3-`
46   url_basepath_escaped=`escape_url "$url_basepath"`
47   basepath="$url_protocol""://""$url_basepath_escaped"
48   printf "%s" "$basepath"
49 }
50
51 get_source_file() {
52   md_file="../${1%.*}.md"
53   rst_file="../${1%.*}.rst"
54   if [ -f "$rst_file" ]; then
55     src_file="$rst_file"
56   elif [ -f "$md_file" ]; then
57     src_file="$md_file"
58   else
59     exit 1
60   fi
61   redo-ifchange "$src_file"
62   printf "%s" "$src_file"
63 }
64
65 prep_sed () {
66   # Escape characters that confuse sed in a replacement string. Also replace
67   # occurences of % (which the templating uses as a variable marker) with
68   # non-printable placeholder \a (clear input of it first), to be replaced by
69   # % again when the templating has finished (so that no replacement string gets
70   # interpreted by the templating).
71   sedsafe_pattern='s/\\/\\\\/g; s/\//\\\//g; s/&/\\\&/g; $!s/$/\\/g; '
72   sed "$sedsafe_pattern" | tr -d '\a' | tr '%' '\a'
73 }