]> git.plutz.net Git - confetti/commitdiff
parsing and processing of aggregated values, split values, vcard escapes
authorpaul <paul@plutz.net>
Fri, 11 Nov 2016 18:32:21 +0000 (18:32 +0000)
committerpaul <paul@plutz.net>
Fri, 11 Nov 2016 18:32:21 +0000 (18:32 +0000)
svn path=/trunk/; revision=142

pages/cards.sh

index 3f0be4a8167ac0d2db5cafb57f09aea19ba60f9e..f6142dfd4f3acaa01de1e1577f845cda0cc2a6c0 100755 (executable)
@@ -111,7 +111,8 @@ vcf_parse() {
     y;abcdefghijklmnopqrstuvwxyz;ABCDEFGHIJKLMNOPQRSTUVWXYZ;
     G; s;\n;;;
 
-    
+    # === strip trailing CR (but keep CRs in property value) ===
+    s;\r$;;;
 
     # === Normalise various known vendor properties ===
                 s;^X-MS-CARDPICTURE(\;|:);PHOTO\1;;
@@ -175,16 +176,59 @@ vcf_parse() {
         if [ -z "$key" ]; then
           eval "$line"
         else
-          values[${key}]="${value//\\r\\n/$BR}"
-          for t in ${(k)tag}; do
-            values[${key}_${t}]="${tag[$t]}"
+          printf '%s\n' "$value" |sed -rn '
+            :X
+            s;((^|[^\\])(\\\\)*),;\1\n;g; 
+            tX;
+            s;\\,;,;g;
+            p
+          ' \
+          | while read -r val; do
+            while [ -n "${values[$key$n]+x}" ]; do n=$((${n=-1} + 1)); done
+            if printf '%s\n' "$val" |grep -qE '((^|[^\\])(\\\\)*)\;'; then
+              m=0
+              values[${key}${n}]="${val}"
+              printf '%s\n' "$val" |sed -rn '
+                :X
+                s;((^|[^\\])(\\\\)*)\;;\1\n;g; 
+                tX;
+                s;\\\;;\;;g;
+                p
+              ' \
+              | while read -r v; do
+                values[${key}${n}+${m}]="$(
+                  printf %s\\n "${v}" \
+                  | sed -rn '
+                    :X
+                    s;((^|[^\\])(\\\\)*)\\n;\1\n;g; 
+                    tX;
+                    s;\\\\;\\;g;
+                    p
+                  '
+                )"
+                m=$(($m + 1))
+              done
+            else
+              values[${key}${n}]="$(
+                printf %s\\n "${val}" \
+                | sed -rn '
+                  :X
+                  s;((^|[^\\])(\\\\)*)\\n;\1\n;g; 
+                  tX;
+                  s;\\\;;\;;g;
+                  s;\\\\;\\;g;
+                  p
+                '
+              )"
+            fi
+            for t in ${(k)tag}; do
+              values[${key}${n}_${t}]="${tag[$t]}"
+            done
           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 n
+          while [ -n "${values[$key$n]+x}" ]; do n=$((${n=-1} + 1)); done
           unset value
           unset tag
         fi