-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv2vcf.lisp
executable file
·412 lines (361 loc) · 17.3 KB
/
csv2vcf.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
;;;; csv2vcf.lisp
(in-package #:csv2vcf)
;;; "csv2vcf" goes here. Hacks and glory await!
;; "xing" is the PRC pinyin of the Hanzi for "family name".
(define-condition no-pinyin-for-hanzi-error (error)
((hanzi :initarg :hanzi :reader no-pinyin-for-hanzi-error-hanzi)))
(defmethod print-object ((object no-pinyin-for-hanzi-error) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "Could not find pinyin for hanzi: ~A~%"
(no-pinyin-for-hanzi-error-hanzi object))))
(define-condition unsupported-field-error (error)
((name
:initarg :name :reader unsupported-field-error-name)))
(defmethod print-object ((object unsupported-field-error) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "Unsupported fields name: ~A~%"
(unsupported-field-error-name object))))
(defvar *supported-vcard-fields* '(full-name
org
mobile
work-fixed-line
email
work-address
home-address
fax
title
url
note))
(defvar *hanzi->pinyin-alist-filename* nil "Chinese family name to pinyin mapping file")
(setq *hanzi->pinyin-alist-filename* "family-name-pinyin.txt")
(defvar *hash-xing->pinyin* nil "Chinese family name to pinyin mapping hash")
(defun load-xing-pinyin-pairs ()
(setq *hash-xing->pinyin*
(with-open-file (in (asdf:system-relative-pathname
'csv2vcf
*hanzi->pinyin-alist-filename*)
:direction :input
:external-format :utf-8)
(let ((hash-xing->pinyin (make-hash-table))
(xing->pinyin-alist (read in nil)))
(mapcar #'(lambda (pair)
(setf (gethash (intern (car pair))
hash-xing->pinyin)
(cdr pair)))
xing->pinyin-alist)
hash-xing->pinyin))))
(defun xing->pinyin (hanzi)
(gethash (intern hanzi) *hash-xing->pinyin*))
(defun parse-csv-row->alist (fields csv-row &key note org)
(let ((result (list (cons 'org org)
(cons 'note note))))
(mapcar #'(lambda (field value)
(setq field (intern (symbol-name field) :csv2vcf))
(setq value (cl-ppcre:regex-replace-all #\Newline value ""))
(or (member field *supported-vcard-fields*)
(error 'unsupported-field-error
:name field))
(if (> (length value) 0)
(case field
((mobile work-fixed-line fax email)
(push (cons field
(split-sequence #\Space value))
result))
(t
(push (cons field value)
result)))))
fields
;; (split-sequence #\, csv-line)
csv-row)
result))
(defun parse-csv-file->alists (fields csv-filepath section-mark &key org note)
(let ((section-mark-length (length section-mark))
(results '())
note-2)
(with-open-file (input-stream csv-filepath
:direction :input
:external-format :utf-8)
(loop for csv-row in (cl-csv:read-csv input-stream)
do (if (and section-mark
(>= (length (nth 0 csv-row)) section-mark-length)
(string-equal (subseq (nth 0 csv-row) 0 section-mark-length)
section-mark))
;; (break)
(setq note-2 (subseq (nth 0 csv-row) section-mark-length))
(push (parse-csv-row->alist fields
csv-row
:note (or note-2
note)
:org org)
results))))
(reverse results)))
(defun get-vcf-field-value (field alist)
(cdr (assoc field alist)))
(defun collect-xing-pinyin-pairs ()
(let ((existing-pairs '()))
(maphash #'(lambda (key value)
(push (cons key value)
existing-pairs))
*hash-xing->pinyin*)
(reverse existing-pairs)))
(defun add-xing-pinyin-pair (xing pinyin)
(if (null (assoc (intern xing) (collect-xing-pinyin-pairs)))
(setf (gethash (intern xing)
*hash-xing->pinyin*)
pinyin)))
(defun dump-xing-pinyin-pairs ()
(with-open-file (dumped-file (asdf:system-relative-pathname
'csv2vcf
*hanzi->pinyin-alist-filename*)
:direction :output
:external-format :utf-8
:if-exists :overwrite)
(let ((pairs (collect-xing-pinyin-pairs)))
(format dumped-file "~A~%" "(")
(loop for pair in pairs
do (format dumped-file "(\"~A\" . \"~A\")~%" (car pair) (cdr pair)))
(format dumped-file "~A~%" ")"))))
;; todo: move the xing->pinyin into parse-csv-line->alist
;; to separate logic from presentation.
(defun alist->vcf3.0-format (alist)
(format t "Processing: ~A~%" (get-vcf-field-value 'full-name alist))
(with-output-to-string (out-stream)
(format out-stream "BEGIN:VCARD~%VERSION:3.0")
(let ((full-name (get-vcf-field-value 'full-name alist)))
(let ((family-name-2 (subseq full-name 0 2))
(family-name-1 (subseq full-name 0 1)))
(let ((code (char-code (char family-name-1 0)
;; (coerce family-name-1
;; 'character)
)))
(if (> code 255)
(let ((pinyin-2 (xing->pinyin family-name-2))
(pinyin-1 (xing->pinyin family-name-1)))
(restart-case
(progn
(if pinyin-2
(format out-stream
"~%N;CHARSET=UTF-8:~A;~A;;;"
family-name-2
(subseq full-name 2))
(if pinyin-1
(format out-stream
"~%N;CHARSET=UTF-8:~A;~A;;;"
family-name-1
(subseq full-name 1))
(error 'no-pinyin-for-hanzi-error
:hanzi (list family-name-1 family-name-2))))
(format out-stream "~%X-PHONETIC-LAST-NAME:~A" (string-upcase (or pinyin-2 pinyin-1))))
(use-manual-value (family-name given-name pinyin)
:interactive read-manual-value
(format out-stream
"~%N;CHARSET=UTF-8:~A;~A;;;"
family-name
given-name)
(format out-stream "~%X-PHONETIC-LAST-NAME:~A" (string-upcase pinyin))
(add-xing-pinyin-pair family-name pinyin)))
(format out-stream "~%FN;CHARSET=UTF-8:~A" full-name))
;; todo: support English full names
))))
(let ((title (get-vcf-field-value 'title alist)))
(if (> (length title) 0)
(format out-stream "~%TITLE;CHARSET=UTF-8:~A" title))
)
(let ((phone-numbers (get-vcf-field-value 'mobile alist)))
(loop for number in phone-numbers
do (format out-stream "~%TEL;TYPE=CELL:~A" number)))
(let ((phone-numbers (get-vcf-field-value 'work-fixed-line alist)))
(loop for number in phone-numbers
do (format out-stream "~%TEL;TYPE=WORK:~A" number)))
(let ((fax-numbers (get-vcf-field-value 'fax alist)))
(loop for number in fax-numbers
do (format out-stream "~%TEL;TYPE=FAX:~A" number)))
(let ((org (get-vcf-field-value 'org alist)))
(if (> (length org) 0)
(format out-stream "~%ORG;CHARSET=UTF-8:~A" org)))
(let ((emails (get-vcf-field-value 'email
alist)))
(loop for email in emails
do (format out-stream
"~%item1.EMAIL;TYPE=pref;TYPE=INTERNET:~A~%item1.X-ABLABEL:email"
email)))
(let ((address (get-vcf-field-value 'work-address alist)))
(if (> (length address) 0)
(format out-stream "~%ADR;TYPE=WORK;CHARSET=UTF-8:;;~A" address)))
(let ((address (get-vcf-field-value 'home-address alist)))
(if (> (length address) 0)
(format out-stream "~%ADR;TYPE=HOME;CHARSET=UTF-8:;;~A" address)))
(let ((url (get-vcf-field-value 'url alist)))
(if (> (length url) 0)
(format out-stream "~%URL:~A" url)))
(let ((note (get-vcf-field-value 'note alist)))
(if note
(format out-stream "~%NOTE;CHARSET=UTF-8:~A" note)))
(format out-stream "~%END:VCARD")))
(defun alist->vcf3.0-format-qprint (alist out-stream)
(format t "Processing: ~A~%" (get-vcf-field-value 'full-name alist))
(format out-stream
"BEGIN:VCARD~%VERSION:3.0")
(let ((full-name (get-vcf-field-value 'full-name alist)))
(let ((family-name-2 (subseq full-name 0 2))
(family-name-1 (subseq full-name 0 1)))
(let ((code (char-code (char family-name-1 0)
;; (coerce family-name-1
;; 'character)
)))
(if (> code 255)
(let ((pinyin-2 (xing->pinyin family-name-2))
(pinyin-1 (xing->pinyin family-name-1)))
(restart-case
(progn
(if pinyin-2
(format out-stream
"~%N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:~A;~A;;;"
(qp-encode family-name-2)
(qp-encode (subseq full-name 2)))
(if pinyin-1
(format out-stream
"~%N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:~A;~A;;;"
(qp-encode family-name-1)
(qp-encode (subseq full-name 1)))
(error 'no-pinyin-for-hanzi-error
:hanzi (list family-name-1 family-name-2))))
(format out-stream "~%X-PHONETIC-LAST-NAME:~A" (string-upcase (or pinyin-2 pinyin-1))))
(use-manual-value (family-name given-name pinyin)
:interactive read-manual-value
(format out-stream
"~%N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:~A;~A;;;"
(qp-encode family-name)
(qp-encode given-name))
(format out-stream "~%X-PHONETIC-LAST-NAME:~A" (string-upcase pinyin))
(add-xing-pinyin-pair family-name pinyin)))
(format out-stream
"~%FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:~A"
(qp-encode full-name)))
;; todo: support English full names
))))
(let ((title (get-vcf-field-value 'title alist)))
(if (> (length title) 0)
(format out-stream "~%TITLE;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:~A" (qp-encode title)))
)
(let ((phone-numbers (get-vcf-field-value 'mobile alist)))
(loop for number in phone-numbers
do (format out-stream "~%TEL;TYPE=CELL:~A" number)))
(let ((phone-numbers (get-vcf-field-value 'work-fixed-line alist)))
(loop for number in phone-numbers
do (format out-stream "~%TEL;TYPE=WORK:~A" number)))
(let ((fax-numbers (get-vcf-field-value 'fax alist)))
(loop for number in fax-numbers
do (format out-stream "~%TEL;TYPE=FAX:~A" number)))
(let ((org (get-vcf-field-value 'org alist)))
(if (> (length org) 0)
(format out-stream "~%ORG;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:~A" (qp-encode org))))
(let ((emails (get-vcf-field-value 'email
alist)))
(loop for email in emails
do (format out-stream
"~%item1.EMAIL;TYPE=pref;TYPE=INTERNET:~A~%item1.X-ABLABEL:email"
email)))
(let ((address (get-vcf-field-value 'work-address alist)))
(if (> (length address) 0)
(format out-stream "~%ADR;TYPE=WORK;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;;~A" (qp-encode address))))
(let ((address (get-vcf-field-value 'home-address alist)))
(if (> (length address) 0)
(format out-stream "~%ADR;TYPE=HOME;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:;;~A" (qp-encode address))))
(let ((note (get-vcf-field-value 'note alist)))
(if note
(format out-stream "~%NOTE;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:~A" (qp-encode note))))
(format out-stream "~%END:VCARD~%~%"))
(defun read-manual-value ()
(let (family-name given-name pinyin)
(format t "Input Family Name:")
(setq family-name (read-line))
(format t "Input Given Name:")
(setq given-name (read-line))
(format t "Input Pinyin for Family Name:")
(setq pinyin (read-line))
(list family-name
given-name
pinyin)))
;;
(defun use-manual-value (c)
(declare (ignore c))
(let (family-name
given-name
pinyin)
(format t "Input Family Name:")
(setq family-name (read-line))
(format t "Input Given Name:")
(setq given-name (read-line))
(format t "Input Pinyin for Family Name:")
(setq pinyin (read-line))
(invoke-restart 'use-manual-value family-name given-name pinyin)))
;; the driver function
;; take /path/to/contacts.csv and output /path/to/contacts.vcf
;; supported fileds:
;; full-name
;; org
;; mobile
;; work-fixed-line
;; email
;; work-address
;; home-address
;; note
(defun csv->vcf (fields csv-filepath &key note org section-mark use-quoted-printable (gen-qr t))
(load-xing-pinyin-pairs)
(let ((output-filepath (make-pathname :type "vcf"
:defaults csv-filepath)))
(with-open-file (output output-filepath
:direction :output
:external-format :utf-8
:if-exists :overwrite
:if-does-not-exist :create
)
;; (let ((records (handler-bind ((unsupported-field-error
;; #'(lambda (c)
;; (format t
;; "Abort upon a unsupported field: ~A~%"
;; (unsupported-field-error-name c))
;; (invoke-restart 'abort))))
;; (parse-csv-file->alists fields
;; csv-filepath
;; section-mark
;; :note note
;; :org org))))
(let ((records (restart-case
(parse-csv-file->alists fields
csv-filepath
section-mark
:note note
:org org)
(re-type-fields ()
(format t "Please input the correct fields, separated by space:~%")
(let ((new-fields (mapcar #'intern
(split-sequence #\Space
(string-upcase (read-line))))))
(csv->vcf new-fields
csv-filepath
:note note
:org org))))))
(loop for alist in records
do (handler-bind ((no-pinyin-for-hanzi-error
#'use-manual-value))
(let ((vcf-string (if use-quoted-printable
(alist->vcf3.0-format-qprint alist output)
(alist->vcf3.0-format alist)))
(qr-png-filepath (make-pathname :type "png"
:name (get-vcf-field-value 'full-name alist)
:defaults csv-filepath)))
(format t "~A~%" vcf-string)
(format output "~A~%~%" vcf-string)
(if gen-qr
(cl-qrencode:encode-png-bytes
(map 'list
#'identity
(flexi-streams:string-to-octets
vcf-string
:external-format :utf-8))
:fpath qr-png-filepath
:pixsize 3
:level :level-l))))))))
(dump-xing-pinyin-pairs))