From: paul Date: Thu, 10 Nov 2016 14:24:02 +0000 (+0000) Subject: unify ics parser X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=cbc94b0f474f7540954fbcdaaa1a6cb88f4daab6;p=lobster unify ics parser svn path=/trunk/; revision=141 --- diff --git a/actions/generate_courselist.sh b/actions/generate_courselist.sh index 35c9fac..07fb0bb 100755 --- a/actions/generate_courselist.sh +++ b/actions/generate_courselist.sh @@ -62,35 +62,9 @@ list_attendee() { #Parameter: Cardfile get_dates() { #Parameter: Calendarfile calendarfile="$_DATA/ical/$course" - unset key - declare -A tags declare -A values - ics_parse "$calendarfile" |while read -r line; do - declare -A tag - case "$line" in - value*) eval "$line";; - tag*) eval "$line";; - key*) - if [ -z "$key" ]; then - eval "$line" - else - values[$key]="$(tex_clean "$value")" - for t in ${(k)tag}; do - tags[${key}_$t]="$tag[$t]" - done - eval "$line" - if [ -n "$values[$key]" ]; then - n=0 - while [ -n "$values[$key$n]" ]; do n=$(($n + 1)); done - key=$key$n - fi - unset value - unset tag - fi - ;; - esac - done + ics_parse "$calendarfile" dtstart="$values[DTSTART]" [ -z "$dtstart" ] && dtstart=$(date +%Y%m%dT%H%M%S) diff --git a/pages/courses.sh b/pages/courses.sh index 0fa5542..02a1666 100755 --- a/pages/courses.sh +++ b/pages/courses.sh @@ -85,27 +85,61 @@ course_mail() { } ics_parse() { - tr -d '\n' <"$1" |sed -r 's:\r ::g;s:\r:\n:g' |tr -d '\r' \ - | sed -r 's:^([^;\:]+)(;[^"\:]+|;"[^"]+")*\:(.*)$:key="\1"\nvalue="\3"\ntag=\2:g' \ + unset key + sed -r ':X;N;$!bX; s;\r\n[ \t];;g; s;\r\n;\n;g;' "$1" \ + | sed -rn ' + # === turn property names to upper case, strip group names === + h; s;^([^;:]+);;; + x; s;^([^;:\.]+\.)?([^;:]+).*$;\2;; + y;abcdefghijklmnopqrstuvwxyz;ABCDEFGHIJKLMNOPQRSTUVWXYZ; + G; s;\n;;; + + /^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$/{ + h; + s;^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$;key='\''\1'\'';; + H; g; + + s;^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\2;; + s;'\'';'\'\\\\\'\'';g + s;\;([A-Z0-9-]+)(=|=(([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*))?;\ntag[\1]='\''\3'\'';g + s;^\n+;;; s;\n+$;;; + /^.+$/H; g; + + s;^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\6;; + # :X s;((^|[^\\])(\\\\)*)[;,];\1\n;g; s;((^|[^\\])(\\\\)*)\\([;,]);\4;g; tX; + s;'\'';'\'\\\\\'\'';g + s;^.*$;value='\''&'\''; + H; g; + + s;^[^\n]*[\n ]+;; + s;\n+$;;; + + p; + } + ' \ | while read -r line; do + declare -A tag case "$line" in - key=*) echo -E "$line" - ;; - value=*) echo -E "$line" - ;; - tag=*) ot='' - echo -E "$line" \ - | sed -r 's:^tag=::;s:\;([A-Z+_-]+="[^"]+"|[A-Z+_-]+=[^\;]+):\n\1:g;' \ - | sed -r 's:([A-Z+_-]+)="?(.*)"?:tag\[\1\]="\2":g' \ - | sed -r '/^ *$/d' \ - | sort |while read -r tag; do - nt="$(echo -E "$tag" |sed -r 's:^tag\[([A-Z+_-]+)\]="(.*)"$:\1:')" - nv="$(echo -E "$tag" |sed -r 's:^tag\[([A-Z+_-]+)\]="(.*)"$:\2:')" - [ "$nt" = "$ot" ] && vl="$nv,$vl" || vl="$nv" - echo -E "tag[$nt]=\"$vl\"" - ot="$nt" - done - ;; + value*) eval "$line";; + tag*) eval "$line";; + key*) + if [ -z "$key" ]; then + eval "$line" + else + values[$key]="$value" + for t in ${(k)tag}; do + values[${key}_$t]="$tag[$t]" + done + eval "$line" + if [ -n "$values[$key]" ]; then + n=0 + while [ -n "$values[$key$n]" ]; do n=$(($n + 1)); done + key=$key$n + fi + unset value + unset tag + fi + ;; esac done } @@ -118,33 +152,8 @@ view_course() { #Parameter: Calendarfile if [ "$cachefile" -nt "$calendarfile" ]; then cat "$cachefile" else - declare -A tags declare -A values - ics_parse "$calendarfile" |while read -r line; do - declare -A tag - case "$line" in - value*) eval "$line";; - tag*) eval "$line";; - key*) - if [ -z "$key" ]; then - eval "$line" - else - values[$key]="$value" - for t in ${(k)tag}; do - tags[${key}_$t]="$tag[$t]" - done - eval "$line" - if [ -n "$values[$key]" ]; then - n=0 - while [ -n "$values[$key$n]" ]; do n=$(($n + 1)); done - key=$key$n - fi - unset value - unset tag - fi - ;; - esac - done + ics_parse "$calendarfile" . ${_EXEC}/templates/view_course.sh |tee "$cachefile" fi } @@ -156,32 +165,7 @@ edit_course() { #Parameter: Calendarfile [ -f "$tempfile" ] && calendarfile="$tempfile" unset key - declare -A tags declare -A values - ics_parse "$calendarfile" |while read -r line; do - declare -A tag - case "$line" in - value*) eval "$line";; - tag*) eval "$line";; - key*) - if [ -z "$key" ]; then - eval "$line" - else - [ -n "$value" ] && values[$key]="$value" || values[$key]='\r' - for t in ${(k)tag}; do - tags[${key}_$t]="$tag[$t]" - done - eval "$line" - if [ -n "$values[$key]" ]; then - n=0 - while [ -n "$values[${key}${n}]" ]; do n=$(($n + 1)); done - key=$key$n - fi - unset value - unset tag - fi - ;; - esac - done + ics_parse "$calendarfile" . ${_EXEC}/templates/edit_course.sh }