X-Git-Url: https://git.plutz.net/?p=confetti;a=blobdiff_plain;f=courses%2Fexport_pdf.sh;fp=courses%2Fexport_pdf.sh;h=7a9a9dd17ee49e876d40cfc7cd62ddad06d7b8ef;hp=0000000000000000000000000000000000000000;hb=d87e28c811b76a4f9bdcc5b8478f7e7b0db37c8f;hpb=27eaed1f6c5be64e58e84a7019f6d9e6d08ce6fb diff --git a/courses/export_pdf.sh b/courses/export_pdf.sh new file mode 100755 index 0000000..7a9a9dd --- /dev/null +++ b/courses/export_pdf.sh @@ -0,0 +1,123 @@ +#!/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 |URL |tr / _).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" +"%A, %d. %b." + dts_date="$(date -d "${dts_date} + ${rec}" +%Y%m%d)" + done +} + +"$_EXEC/cgilite/html-sh.sed" <<-EOF |sed -E 's;<(td|th)([^>]*)>;<\1 \2 style="border: 1pt solid\; padding: 1mm 2mm\;">;g' >"$htmlfile" + +[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%" + [col width=10*] [col width=5*] [col width=10*] [col width=15*] + [thead + [tr [th . $(l10n N)] [th . $(l10n BDAY)] [th . $(l10n TEL)] [th . $(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
' "$type" "$(pdi_value "$vcf" TEL $n)" + done )" + printf '[tr [td .N . %s] [td .BDAY . %s] [td .TEL . %s] [td .NOTE . %s]]\n' \ + "$(pdi_value "$vcf" FN |unescape |HTML)" \ + "$(pdi_value "$vcf" BDAY |unescape |HTML)" \ + "$tel" \ + "$(pdi_value "$vcf" NOTE |unescape |HTML)" + done |sort -k4)] + ] + [table width="100%" style="page-break-before: always;" + [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 ] $(get_dates |xargs -d\\n printf '[th . %s]')] + ][tbody + $(grep -F "${coursefile##*/} " "$_DATA/mappings/attendance" |while read discard each; do + vcf="$(pdi_load "$_DATA/vcard/$each")" + printf '[tr [td .N . %s] [td] [td] [td] [td] [td] [td] [td] [td] [td] [td]]\n' \ + "$(pdi_value "$vcf" FN |unescape |HTML)" + done |sort -k4)] + ] +]] +EOF + +lowriter --convert-to pdf --outdir "$_DATA/export/" "$htmlfile" +REDIRECT "/export/${pdffile##*/}"