]> git.plutz.net Git - lobster/commitdiff
working vcard filter
authorPaul Hänsch <paul@plutz.net>
Wed, 6 Feb 2019 20:21:37 +0000 (21:21 +0100)
committerPaul Hänsch <paul@plutz.net>
Wed, 6 Feb 2019 20:21:37 +0000 (21:21 +0100)
cards/list.sh

index cb33637cdbc36c5f4ca7e2166c8c572dc2302653..8b911fb88e9bf63583d75702c79efccdeea81020 100755 (executable)
@@ -2,6 +2,9 @@
 
 . "${_EXEC}"/pdiread.sh
 
+unescape() { [ $# = 0 ] && sed -E 's;\\(.);\1;g' || printf %s "$*" |sed -E 's;\\(.);\1;g'; }
+upcase=' y;abcdefghijklmnopqrstuvwxyzäöüé;ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜÉ;; '
+
 card_fullname(){
   local card="$1" n1 n2 n3 n4 n5
 
@@ -21,6 +24,7 @@ card_fullname(){
   fi
 }
 
+
 card_item(){
   local card="$1"
   local item cnt c
@@ -30,13 +34,13 @@ card_item(){
     cnt="$(pdi_count "$card" "$item")"
 
     case $item in
-      FN) printf '[h2 .item .FN &shy;%s]' "$(card_fullname "$card" |HTML)"
+      FN) printf '[h2 .item .FN &shy;%s]' "$(card_fullname "$card" |unescape |HTML)"
         ;;
       GENDER) printf '[span .item .GENDER &shy;%s]' "$(pdi_value "$card" GENDER |l10n)"
         ;;
       NICKNAME) seq 1 $cnt |while read c; do
           printf '[span .item .NICKNAME &shy;aka. "%s"]' \
-                 "$(pdi_value "$card" NICKNAME $c |HTML)"
+                 "$(pdi_value "$card" NICKNAME $c |unescape |HTML)"
         done
         ;;
       X-ZACK-JOINDATE|X-ZACK-LEAVEDATE) if [ $cnt -gt 0 ]; then
@@ -59,7 +63,7 @@ card_item(){
         ;;
       PHOTO|LOGO) if [ $cnt -gt 0 ]; then 
           printf '[img .item .%s src="data:image/%s;base64,%s"]' "$item" \
-                 "$(pdi_attrib "$card" "$item" |sed -r 's;^(.*;)?TYPE="?(.+)"?(;.*)?$;\2;')" \
+                 "$(pdi_attrib "$card" "$item" |sed -E 's;^(.*;)?TYPE="?(.+)"?(;.*)?$;\2;')" \
                  "$(pdi_value "$card" "$item" |grep -xE '[a-zA-Z0-9/+=]+')"
         fi
         ;;
@@ -67,8 +71,8 @@ card_item(){
           printf '[h3 %s]' "$(l10n EMAIL)"
           seq 1 $cnt |while read c; do
             printf '[a .item .EMAIL href="mailto:%s" &shy;%s]' \
-                   "$(pdi_value "$card" EMAIL $c |HTML)" \
-                   "$(pdi_value "$card" EMAIL $c |HTML)"
+                   "$(pdi_value "$card" EMAIL $c |unescape |HTML)" \
+                   "$(pdi_value "$card" EMAIL $c |unescape |HTML)"
           done
         fi
         ;;
@@ -76,7 +80,7 @@ card_item(){
           printf '[h3 %s]' "$(l10n "$item")"
           seq 1 $cnt |while read c; do
             printf '[span .item .%s &shy;%s]' "$item" \
-                   "$(pdi_value "$card" "$item" $c |HTML)"
+                   "$(pdi_value "$card" "$item" $c |unescape |HTML)"
           done
         fi
         ;;
@@ -105,7 +109,7 @@ print_card(){
         $(grep -F "    ${cardfile##*/}" "$_DATA/mappings/attendance" |while read each discard; do
           printf '[li [a .item .attendance href="/courses#%s" &shy;%s]]' \
                  "$each" \
-                 "$(pdi_value "$(pdi_load "$_DATA/ical/$each")" SUMMARY |HTML)"
+                 "$(pdi_value "$(pdi_load "$_DATA/ical/$each")" SUMMARY |unescape |HTML)"
         done)]
         $(card_item "$card" CATEGORIES)
       ]
@@ -118,8 +122,41 @@ print_card(){
   fi
 }
 
-list_cards(){
+filter_cards(){
+  local filter f fex='x;p;'
+
+  filter="$(printf %s "${filter}" \
+            | sed -E 's;[]\/\(\)\\\^\$\?\.\+\*\;\[\{\}];\\\\&;g;
+                      '"$upcase"
+           )^"
+
+  while [ "$filter" ]; do
+    f="${filter%%^*}" filter="${filter#*^}"
+    case $f in
+      '') break
+        ;;
+      ANY:*) fex="/\n.*(\;[^:]*)?:.*(${f#*:}).*\r?\n/{${fex}}"
+        ;;
+      NAME:*) fex="/\n(N|FN|NICKNAME)(\;[^:]*)?:.*(${f#*:}).*\r?\n/{${fex}}"
+        ;;
+      STREET:*|ZIP:*) fex="/\nADR(\;[^:]*)?:.*(${f#*:}).*\r?\n/{${fex}}"
+        ;;
+      *) fex="/\n${f%%:*}(\;[^:]*)?:.*(${f#*:}).*\r?\n/{${fex}}"
+        ;;
+    esac
+  done
+
   for cardfile in "${_DATA}"/vcard/*.vcf; do
+    printf '%s\n' "$cardfile"
+    cat "$cardfile"
+  done \
+  | sed -En ':X; /\nEND:VCARD\r?$/!{ N; bX; }; h; s;\n.*$;;; x; s;^[^\n]+\n;;;
+             '"$upcase""$fex"
+}
+
+list_cards(){
+  filter_cards \
+  | while read cardfile; do
     print_card "$cardfile"
   done
 }