#!/bin/zsh
-# Copyright 2015 Paul Hänsch
+# Copyright 2015, 2017 Paul Hänsch
#
# This file is part of Confetti.
#
catfile="${_DATA}/mappings/categories"
listcards() {
- ls -1 ${_DATA}/vcard/*vcf 2>/dev/null |while read file; do
- fn=$(sed -rn 's:^N(;.+)*\:([^;]*;){1} *([^;]*).*$:\3:p' "$file")
- echo "$fn\t$file"
- done |sort |sed -r 's:^.*\t(.*/)([^/]+)$:\2:'
+ for file in "${_DATA}/vcard/"*.vcf; do
+ printf '%s\t%s\n' \
+ "$(sed -rn 's:^N(;.+)*\:([^;]*;){1} *([^;]*).*$:\3:p' "$file")" \
+ "$file"
+ done \
+ | sort \
+ | sed -r 's;^.*\t(.*/)([^/]+)$;\2;'
}
get_name() {
cfile="${_DATA}/vcard/$1"
- sed -rn 's:^N(;[^"\:]+|;"[^"]+")*\:([^;]*)(\;[^;]*)(\;[^;]*)?(\;[^;]*)?(\;[^;]*)?\r?$:\5 \3 \4 \2 \6:p' "$cfile" \
- |sed -r 's:,: :;s:\;: :g;s: +: :g' \
+ sed -rn 's;^N(\;[^":]+|\;"[^"]+")*:([^\;]*)(\;[^\;]*)(\;[^\;]*)?(\;[^\;]*)?(\;[^\;]*)?\r?$;\5 \3 \4 \2 \6;;
+ tX; b; :X s;[,\; ]+; ;g; p;' "$cfile"
}
get_categories(){
cfile="${_DATA}/vcard/$1"
- sed -rn 's:^CATEGORIES(;[^"\:]+|;"[^"]+")*\:(.+)\r?$:\2:p' "$cfile" \
- | sed -r 's;(^|[^\\]+)((\\\\)+),;\1\2\n;g; s;(^|[^\\]),;\1\n;g; s;(^|[^\\]+)((\\\\)+),;\1\2\n;g; s;(^|[^\\]),;\1\n;g; s;\\,;,;g' \
+ sed -rn 's;^CATEGORIES(\;[^":]+|\;"[^"]+")*:(.+)\r?$;\2;;
+ tX; b; :X
+ s;(^|[^\\]+)((\\\\)+),;\1\2\n;g;
+ s;(^|[^\\]),;\1\n;g; s;(^|[^\\]+)((\\\\)+),;\1\2\n;g;
+ s;(^|[^\\]),;\1\n;g; s;\\,;,;g; p;' "$cfile" \
| sort -u
}
list_categories() {
- sort -u "$catfile"
+ sort -u "$catfile" \
+ | sed -r '/^[\t ]*$/d'
}
-# Copyright 2015 Paul Hänsch
+# Copyright 2015, 2017 Paul Hänsch
#
# This file is part of Confetti.
#
cat_listing(){
list_categories | while read cat; do
- cat <<-END
- <li>${cat}<button type="submit" name="remove" value="${cat}">$(l10n cat_remove)</button></li>
- END
+ printf '<li>%s<button type="submit" name="remove" value="%s">%s</button></li>\n' \
+ "$(htmlsafe "$cat")" "$(attribsafe "$cat")" "$(l10n cat_remove)"
done
}
list_catsel(){
card="$1"
- cats="$(get_categories $card)"
+ cats="$(get_categories "$card")"
list_categories |while read cat; do
- cat <<-END
- <li><label><input type="checkbox" name="$card" value="$cat"
- $(printf %s "$cats" |grep -qF "$cat" && printf %s 'checked="checked"')>${cat}</label></li>
- END
+ printf '<li><label><input %s type="checkbox" name="%s" value="%s">%s</label></li>' \
+ "$(printf %s "$cats" |grep -qF "$cat" && printf 'checked="checked"')" \
+ "$(attribsafe "$card")" "$(attribsafe "$cat")" "$(htmlsafe "$cat")"
done
}
display_catsel(){
card="$1"
- cat <<-END
- <li><h2>$(get_name "$card")</h2><ul>$(list_catsel "$card")</ul></li>
- END
+ printf '<li><h2>%s</h2><ul>' "$(htmlsafe "$(get_name "$card")")"
+ list_catsel "$card"
+ printf '</ul></li>'
}
cat <<EOF
<button type="submit" name="submit" value="submit">$(l10n cat_update)</button>
</fieldset>
<ul class="namelist">
- $(listcards |while read card; do display_catsel "$card"; done )
+EOF
+listcards \
+| while read card; do
+ display_catsel "$card"
+done
+cat <<EOF
</ul>
<fieldset>
<button type="submit" name="submit" value="submit">$(l10n cat_update)</button>
</fieldset>
</form>
-
EOF