blog-weight.nix (1930B)
1 { 2 pkgs, 3 domain, 4 ... 5 }: let 6 blog-weight = pkgs.writeShellScriptBin "blog-weight" '' 7 # variables 8 weight_filename="$HOME/vault/src/blog.${domain}/content/weight.md" 9 if [ "$1" = "date" ]; then 10 printf "Writing empty dates... " 11 page_source="$(head -n -1 "$weight_filename")" 12 previous_date="$(printf %s "$page_source" | awk -F, 'END{print $1}')" 13 sequence_count="$((($(date --date="$(date +%F)" +%s) - $(date --date="$previous_date" +%s)) / (60 * 60 * 24)))" 14 { 15 printf "%s\\n" "$page_source" 16 printf "%s" "$(for i in $(seq $sequence_count); do printf "%s,\\n" "$(date -d "$previous_date+$i day" +%F)"; done)" 17 printf "\\n</pre></details>" 18 } >"$weight_filename" 19 printf "done\\n" 20 exit 0 21 fi 22 printf "Drawing graph... " 23 weight_rawdata="$(awk '/<pre>/{flag=1; next} /<\/pre>/{flag=0} flag' "$weight_filename" | sort -u)" 24 weight_dateinit="$(awk '/date:/ {print $2}' "$weight_filename")" 25 grep "^$(date +%Y)-" <<<"$weight_rawdata" >temp.dat 26 ${pkgs.gnuplot}/bin/gnuplot <<-EOF 27 set grid 28 set datafile separator comma 29 set xlabel "Month" 30 set xdata time 31 set timefmt "%Y-%m-%d" 32 set xtics format "%b" 33 set ylabel "Weight (kg)" 34 set key off 35 set term svg font 'sans-serif,12' 36 set sample 50 37 set output "temp.svg" 38 plot "temp.dat" using 1:2 smooth cspline with lines 39 EOF 40 printf "done\\nWriting page... " 41 { 42 printf -- "---\\ntitle: Weight\\nlayout: single\\ndate: %s\\nlastmod: %(%Y-%m-%dT%H:%M:00)T\\n---\\n\\n" "$weight_dateinit" -1 43 printf "%s\\n\\n" "$(${pkgs.scour}/bin/scour -i temp.svg --strip-xml-prolog --remove-descriptive-elements --create-groups --enable-id-stripping --enable-comment-stripping --shorten-ids --no-line-breaks)" 44 printf "<details><summary>Raw data</summary>\\n<pre>\\n%s\\n</pre></details>" "$weight_rawdata" 45 46 } >"$weight_filename" 47 printf "done\\nCleaning up... " 48 rm temp.{dat,svg} 49 printf "done\\n" 50 51 ''; 52 in { 53 environment.systemPackages = [blog-weight]; 54 }