home · contact · privacy
2869a29a3d414db7066c71e17ff1439b0b7c6c92
[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 escape_url() {
14   out=`python3 -c 'import sys, urllib.parse; print(urllib.parse.quote(sys.argv[1]))' "$1"`
15   printf "%s" "$out"
16 }
17
18 get_basepath() {
19   url_file="$1"url
20   redo-ifchange "$url_file"
21   base_url=`cat "$url_file" | head -1`
22   url_protocol=`echo $base_url | cut -d ':' -f 1`
23   url_basepath=`echo $base_url | cut -d '/' -f 3-`
24   url_basepath_escaped=`escape_url "$url_basepath"`
25   basepath="$url_protocol""://""$url_basepath_escaped"
26   printf "%s" "$basepath"
27 }
28
29 get_source_file() {
30   md_file="../${1%.*}.md"
31   rst_file="../${1%.*}.rst"
32   if [ -f "$rst_file" ]; then
33     src_file="$rst_file"
34   elif [ -f "$md_file" ]; then
35     src_file="$md_file"
36   else
37     exit 1
38   fi
39   redo-ifchange "$src_file"
40   printf "%s" "$src_file"
41 }
42
43 prep_sed () {
44   # Escape characters that confuse sed in a replacement string. Also replace
45   # occurences of % (which the templating uses as a variable marker) with
46   # non-printable placeholder \a (clear input of it first), to be replaced by
47   # % again when the templating has finished (so that no replacement string gets
48   # interpreted by the templating).
49   sedsafe_pattern='s/\\/\\\\/g; s/\//\\\//g; s/&/\\\&/g; $!s/$/\\/g; '
50   sed "$sedsafe_pattern" | tr -d '\a' | tr '%' '\a'
51 }