From: paul Date: Thu, 10 Nov 2016 14:05:21 +0000 (+0000) Subject: unify vcf parsers X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=51b0a660590e9a0adaaa35a3259de32156d1dee6;p=confetti unify vcf parsers svn path=/trunk/; revision=140 --- diff --git a/actions/generate_courselist.sh b/actions/generate_courselist.sh index f2f2aec..35c9fac 100755 --- a/actions/generate_courselist.sh +++ b/actions/generate_courselist.sh @@ -35,36 +35,10 @@ list_attendee() { #Parameter: Cardfile id="$1" cardfile="$_DATA/vcard/${id}" - unset key - declare -A tags declare -A values if [ -r "$cardfile" ]; then - vcf_parse "$cardfile" |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 + vcf_parse "$cardfile" n=$(printf %s "$values[N]" \ | sed -rn 's:^([^;]*)(;[^;]*)(;[^;]*)?(;[^;]*)?(;[^;]*)?$:\4 \2 \3 \1 \5:gp' \ diff --git a/pages/cards.sh b/pages/cards.sh index 0ead803..3f0be4a 100755 --- a/pages/cards.sh +++ b/pages/cards.sh @@ -102,14 +102,17 @@ listcards() { } vcf_parse() { + 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 === - h; s;^([^\;:]+);;; - x; s;^([^\;:]+).*$;\1;; + # === turn property names to upper case, strip group names === + h; s;^([^;:]+);;; + x; s;^([^;:\.]+\.)?([^;:]+).*$;\2;; y;abcdefghijklmnopqrstuvwxyz;ABCDEFGHIJKLMNOPQRSTUVWXYZ; G; s;\n;;; + + # === Normalise various known vendor properties === s;^X-MS-CARDPICTURE(\;|:);PHOTO\1;; s;^X-GENDER(\;|:);GENDER\1;; @@ -129,73 +132,76 @@ vcf_parse() { s;^X-KADDRESSBOOK-X-SPOUSENAME(\;|:);RELATED\;VALUE=text\;TYPE=spouse\1;; # === Normalise obsolete vendor IM properties === - s;^X-AIM((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;; - s;^X-ICQ((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;; - s;^X-GOOGLE-TALK((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:xmpp:;; - s;^X-JABBER((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:xmpp:;; - s;^X-MSN((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:msn:;; - s;^X-YAHOO((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):;IMPP\1:ymsgr:;; - s;^X-SIP((\;[a-zA-Z0-9-]+|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):(sip:)?;IMPP\1:sip:;; + s;^X-AIM((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;; + s;^X-ICQ((\;[a-Z0-9-]+|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):;IMPP\1:aim:;; + s;^X-GOOGLE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=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):(.*)$;key='\''\1'\'';; H; g; - s;^([a-zA-Z0-9-]+)((\;[a-zA-Z0-9-]+=?|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\6;; + + s;^([A-Z0-9-]+)((\;[a-Z0-9-]+=?|\;[a-Z0-9-]+=([^;,:"]+|"[^"]+")(,[^;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\2;; s;'\'';'\'\\\\\'\'';g - s;^.*$;value='\''&'\''; - H; g; - s;^([a-zA-Z0-9-]+)((\;[a-zA-Z0-9-]+=?|\;[a-zA-Z0-9-]+=([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*)*):([^\n]*)\n.*$;\2;; + 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;\;([a-zA-Z0-9-]+)(=|=(([^\;,:"]+|"[^"]+")(,[^\;,:"]+|,"[^"]+")*))?;\ntag[\1]='\''\3'\'';g + s;^.*$;value='\''&'\''; H; g; - s;^[^\n]*[\n ]+;; - s;\n[\n ]+;\n;g; s;\n$;;; + s;^[^\n]*[\n ]+;; + s;\n+$;;; p; } - ' + ' \ + | 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//\\r\\n/$BR}" + 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 } view_card() { #Parameter: Cardfile id="$1" cardfile="$_DATA/vcard/${id}" cachefile="$_DATA/cache/${id}.cache" - unset key if [ "$cachefile" -nt "$cardfile" ]; then cat "$cachefile" else - declare -A tags declare -A values - vcf_parse "$cardfile" |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//\\r\\n/$BR}" - 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 + vcf_parse "$cardfile" . $view_card |tee "$cachefile" fi } @@ -205,34 +211,8 @@ edit_card() { #Parameter: Cardfile cardfile="$_DATA/vcard/$id" tempfile="$_DATA/temp/$id" [ -f "$tempfile" ] && cardfile="$tempfile" - unset key - declare -A tags declare -A values - vcf_parse "$cardfile" |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//\\r\\n/$BR}" || 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 + vcf_parse "$cardfile" . $edit_card } diff --git a/templates/edit_attendee.sh b/templates/edit_attendee.sh index f12633d..047879a 100755 --- a/templates/edit_attendee.sh +++ b/templates/edit_attendee.sh @@ -70,8 +70,8 @@ cat <' echo ' ' fi) - ${values[PHOTO]:+} - ${values[LOGO]:+} + ${values[PHOTO]:+} + ${values[LOGO]:+}
diff --git a/templates/edit_client.sh b/templates/edit_client.sh index ed51dde..b2331aa 100755 --- a/templates/edit_client.sh +++ b/templates/edit_client.sh @@ -85,8 +85,8 @@ cat <' echo ' ' fi) - ${values[PHOTO]:+} - ${values[LOGO]:+} + ${values[PHOTO]:+} + ${values[LOGO]:+}
@@ -103,9 +103,9 @@ cat <

$(l10n TEL)

- $(teltype "${tags[TEL_TYPE]}") + $(teltype "${values[TEL_TYPE]}") $(for n in TEL{0..10}; do - printf "${values[$n]:+$(teltype "${tags[$n_TYPE]}")}" + printf "${values[$n]:+$(teltype "${values[${n}_TYPE]}")}" done)

$(l10n EMAIL)

diff --git a/templates/view_attendee.sh b/templates/view_attendee.sh index bdfdc6a..1394ced 100755 --- a/templates/view_attendee.sh +++ b/templates/view_attendee.sh @@ -34,14 +34,14 @@ cat <$(l10n label_join): $values[X-ZACK-JOINDATE]} ${values[X-ZACK-LEAVEDATE]:+$(l10n label_leave): $values[X-ZACK-LEAVEDATE]} ${values[SOUND]:+} - ${values[PHOTO]:+} - ${values[LOGO]:+} + ${values[PHOTO]:+} + ${values[LOGO]:+}

$(l10n TEL)

$(for n in TEL TEL{0..10}; do - echo "${values[$n]:+$tags[${n}_TYPE] $values[$n]}" + echo "${values[$n]:+$values[${n}_TYPE] $values[$n]}" done)

$(l10n TEL)

$(for n in TEL TEL{0..10}; do - echo "${values[$n]:+$(l10n t${tags[${n}_TYPE]}) $values[$n]}" + echo "${values[$n]:+$(l10n t${values[${n}_TYPE]}) $values[$n]}" done) ${values[EMAIL]:+

$(l10n EMAIL)

} $(for n in EMAIL EMAIL{0..10}; do @@ -61,7 +61,7 @@ cat <$(l10n IMPP)} $(for n in IMPP IMPP{0..10}; do - echo "${values[$n]:+$tags[${n}_TYPE] $values[$n]}" + echo "${values[$n]:+$values[${n}_TYPE] $values[$n]}" done)