diff --git a/vcarded.scm b/vcarded.scm index e167d7b..c05cd44 100644 --- a/vcarded.scm +++ b/vcarded.scm @@ -54,14 +54,6 @@ (irregex-extract "(\\\\;|[^;])*" key-or-value)) -(define (parse-vcard-element kv-pair) - (case (car kv-pair) - ('VERSION - (append (list (car kv-pair) (string->number (second kv-pair))) - (cddr kv-pair))) - (else kv-pair))) - - ;; Parse a line of a vcard file into an alist-friendly format: ;; (KEY ("keyprop1=d" "keyprop2=b") "VALUE" ("valprop1=a" "valprop2=b")) (define (parse-vcard-line line) @@ -74,6 +66,25 @@ (cdr value-elements)))) -(define (vcard-string->alist string) - (map parse-vcard-line (lines string))) +;; Reader thunk. Read/parse an entire vcard into a “vcard alist.” +(define (read-vcard) + (let [(element (read-vcard-element))] + (if (not (eof-object? (peek-char))) + (append (list element) (read-vcard)) + element))) + +;; Read a single unfolded line into a vcard “element” list. +(define (read-vcard-element) + (parse-vcard-line (read-folded-line))) + + +;; Reader-thunk. Read a “logical” folded-line, where a line beginning with a +;; space is a continuation of the previous line — like with vcards. +(define (read-folded-line) + (let [(line (read-line))] + (if (eq? (peek-char) #\space) + (string-concatenate + (list line + (string-drop (read-element) 1))) + line)))