]> git.plutz.net Git - confetti/commitdiff
unify ics parser
authorpaul <paul@plutz.net>
Thu, 10 Nov 2016 14:24:02 +0000 (14:24 +0000)
committerpaul <paul@plutz.net>
Thu, 10 Nov 2016 14:24:02 +0000 (14:24 +0000)
svn path=/trunk/; revision=141

actions/generate_courselist.sh
pages/courses.sh

index 35c9fac6041c9afb5c263855c3729d634e28dddc..07fb0bbb79bf1f19c94afa7a4f0c86b345aa79de 100755 (executable)
@@ -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)
index 0fa5542f13efa1ceaafe7fa709e2d8e0236c467b..02a16669a21a0e6c410b7adb2ac72c35a21bd226 100755 (executable)
@@ -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
 }