]> git.plutz.net Git - confetti/blobdiff - courses/export_pdf.sh
merge from cgilite
[confetti] / courses / export_pdf.sh
diff --git a/courses/export_pdf.sh b/courses/export_pdf.sh
new file mode 100755 (executable)
index 0000000..62bb69e
--- /dev/null
@@ -0,0 +1,132 @@
+#!/bin/sh
+
+. "${_EXEC}/pdiread.sh"
+. "$_EXEC/cards/l10n.sh"
+
+coursefile="${_DATA}/ical/$(GET course)"
+
+if [ ! -r "$coursefile" ]; then
+  SET_COOKIE 0 message="Cannot read course file"
+  REDIRECT /courses/
+  return 0
+elif ! mkdir -p "$_DATA/export"; then
+  SET_COOKIE 0 message="Cannot create export directory"
+  REDIRECT /courses/
+  return 0
+fi
+
+ics="$(pdi_load "$coursefile")"
+htmlfile="${_DATA}/export/$(pdi_value "$ics" SUMMARY |unescape |tr \\n/ __).html"
+pdffile=${htmlfile%.html}.pdf
+
+pdi_date() {
+  local pdt y m d H M S Z
+  [ $# -eq 0 ] && read pdt || pdt="$*"
+
+  case $pdt in
+    *T*Z)
+      Z=UTC; pdt="${pdt%Z}";;
+    TZID=*:*T*)
+      Z="${pdt%%:*}"; Z=${Z#TZID=}; pdt=${pdt#TZID=*:};;
+  esac
+
+  y="${pdt%%????T*}" pdt=${pdt#????}
+  m="${pdt%%??T*}" pdt=${pdt#??}
+  d="${pdt%%T*}" pdt=${pdt#??T}
+  H="${pdt%%????}" pdt=${pdt#??}
+  M="${pdt%%??}" pdt=${pdt#??}
+  S="${pdt}" pdt=''
+
+  case Z in 
+    UTC) date -d "${y}-${m}-${d} ${H}:${M}:${S} UTC" +%s;;
+     '') date -d "${y}-${m}-${d} ${H}:${M}:${S}" +%s;;
+      *) date -d "TZ=\"${Z}\" ${y}-${m}-${d} ${H}:${M}:${S}" +%s;;
+  esac
+}
+
+get_dates() {
+  local dts_date rrule rr_int rr_freq rec today="$(date +%Y%m%d)"
+
+  dts_date="$(pdi_value "$ics" DTSTART || printf %s "$today")"
+  dts_date="${dts_date#TZID=*:}" dts_date="${dts_date%%T*}"
+  rrule="$(pdi_value "$ics" RRULE)"
+  rr_int="${rrule##*INTERVAL=}" rr_int="${rr_int%%;*}"
+  rr_freq="${rrule##*FREQ=}" rr_freq="${rr_freq%%;*}"
+
+  [ "$rr_int" -ge 0 ] || rr_int=1 2>/dev/null
+  case "$rr_freq" in
+    YEARLY)  rec="$rr_int year";;
+    MONTHLY) rec="$rr_int month";;
+    DAILY)   rec="$rr_int day";;
+    WEEKLY)  rec="$rr_int week";;
+    *) rec="$rr_int week";;
+  esac
+
+  while [ "$dts_date" -lt "$today" ]; do dts_date="$(date -d "${dts_date} + ${rec}" +%Y%m%d)"; done
+  for n in 1 2 3 4 5 6 7 8 9 10; do
+    LANG=de_DE.UTF-8 date -d "$dts_date" +"%d. %b."
+    dts_date="$(date -d "${dts_date} + ${rec}" +%Y%m%d)"
+  done
+}
+
+# some table styles need to be inline, because this is how libreoffice works
+style_td='style="border: 1pt solid; padding: 1mm 2mm; vertical-align: top;"'
+
+"$_EXEC/cgilite/html-sh.sed" <<-EOF >"$htmlfile"
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
+[html [head
+  [meta http-equiv="content-type" content="text/html; charset=utf-8"]
+  [title]
+  [meta name="generator" content="Confetti"]
+  [meta name="created" content="$(date +%FT%T)"]
+  [meta name="changed" content="$(date +%FT%T)"]
+  [style type="text/css"
+    @page { size: 29.7cm 21cm; margin: 1.5cm; }
+    * { background: inherit; }
+    body { background: transparent; font-family: Liberation Sans, Sans-Serif; }
+
+    th { white-space: pre; }
+    th, td { text-align: left; }
+  ]
+][body lang="de_DE"
+  [table width="100%" style="page-break-after: always;"
+    [col width=10*] [col width=5*] [col width=10*] [col width=15*]
+    [thead
+      [tr [th $style_td . $(l10n N)] [th $style_td . $(l10n BDAY)] [th $style_td . $(l10n TEL)] [th $style_td . $(l10n NOTE)]]
+    ][tbody
+      $(grep -F "${coursefile##*/}     " "$_DATA/mappings/attendance" |while read discard each; do
+        vcf="$(pdi_load "$_DATA/vcard/$each")"
+        tel="$( seq 1 $(pdi_count "$vcf" TEL) |while read n; do
+                  type="$(pdi_attrib "$vcf" TEL $n TYPE)"
+                  [ "$type" ] && type="$(l10n "TYPE=$type"):"
+                  printf '%s %s<br>' "$type" "$(pdi_value "$vcf" TEL $n)"
+                done  )"
+        printf '[tr valign=top [td %s .N . %s] [td %s .BDAY . %s] [td %s .TEL . %s] [td %s .NOTE . %s]]\n' \
+               "$style_td" "$(pdi_value "$vcf" FN   |unescape |HTML)" \
+               "$style_td" "$(pdi_value "$vcf" BDAY |unescape |HTML)" \
+               "$style_td" "$tel" \
+               "$style_td" "$(pdi_value "$vcf" NOTE |unescape |HTML)"
+      done |sort)]
+  ]
+  [table width="100%"
+    [col width=30*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*] [col width=10*]
+    [thead
+      [tr [th $style_td ] $(get_dates |xargs -d\\n printf "[th $style_td . %s]")]
+    ][tbody
+    $(grep -F "${coursefile##*/}       " "$_DATA/mappings/attendance" |while read discard each; do
+      vcf="$(pdi_load "$_DATA/vcard/$each")"
+      printf '[tr [td %s .N . %s] [td %s] [td %s] [td %s] [td %s] [td %s] [td %s] [td %s] [td %s] [td %s] [td %s]]\n' \
+             "$style_td" "$(pdi_value "$vcf" FN |unescape |HTML)" \
+             "$style_td" "$style_td" "$style_td" "$style_td" "$style_td" "$style_td" "$style_td" "$style_td" "$style_td" "$style_td"
+    done |sort)]
+  ]
+]]
+EOF
+
+export            HOME="$_DATA"
+export XDG_CONFIG_HOME="$_DATA/xdg_config"
+export  XDG_CACHE_HOME="$_DATA/xdg_cache"
+export   XDG_DATA_HOME="$_DATA/xdg_local"
+
+lowriter --convert-to pdf --outdir "$_DATA/export/" "$htmlfile" >/dev/null
+REDIRECT "$(URL "/export/${pdffile##*/}")"