Skip to content

Commit 97bc989

Browse files
committed
feat(contact): 支持备注字段
fixes #ICAWCE
1 parent e2f9496 commit 97bc989

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ File list for each system:
6969
## Usage
7070

7171
1. Open the app.
72-
2. Copy the name and phone number in the format of `Name PhoneNumber` on each line into the editing box below;
72+
2. Copy the name and phone number in the format of `Name PhoneNumber Note` on each line into the editing box below. The note can be ignored.
7373
```text
74-
Hardy Buck 13445467890
75-
Alva Mackintosh 13554678907
76-
Hobart Baker 13645436748
74+
Isaac Newton 13445467890 British mathematician
75+
Muhammad 13554678907
76+
Confucius 13645436748
7777
```
7878
3. Click "生成" (Generate), select a path to save the file.
7979
4. Copy the generated VCF file to your phone, select "Contacts" when opening the file, and then follow the prompts.
@@ -85,11 +85,12 @@ File list for each system:
8585
> - The program will automatically remove excess spaces from the input box.
8686
> - If there are multiple spaces in each line, all characters before the last space will be treated as names.
8787
>
88-
> For example, ` Hardy Buck 13333333333 ` will be recognized as
88+
> For example, ` Han Meimei 13333333333 A well-known girl` will be recognized as
8989
>
9090
> ```text
91-
> Name: Hardy Buck
91+
> Name: Han Meimei
9292
> Phone: 13333333333
93+
> Note: A well-known girl
9394
> ```
9495
9596
## Compatibility

README.zh.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ VCF 生成器,输入姓名与手机号则自动生成用于批量导入到通
8080

8181
## 使用方法
8282

83-
1. 把名字和电话以每行 `姓名 电话号码` 的格式复制到下面的编辑框内;
83+
1. 把名字和电话以每行 `姓名 电话号码 备注` 的格式复制到下面的编辑框内,其中备注可忽略
8484
```text
85+
张三 13345367789 著名的法外狂徒
8586
李四 13445467890
8687
王五 13554678907
8788
赵六 13645436748
@@ -92,15 +93,16 @@ VCF 生成器,输入姓名与手机号则自动生成用于批量导入到通
9293

9394
> [!NOTE]
9495
>
95-
> - 制表符将会自动转换为空格处理
96+
> - 制表符会自动转换为空格处理,您可以同时使用制表符和空格分割
9697
> - 程序会自动去除输入框内多余的空格。
9798
> - 如果每行有多个空格,则会将最后一个空格以前所有的字符当作姓名处理。
9899
>
99-
> 比如 ` Wang lei 13333333333 ` 将会被识别为
100+
> 比如 `东坡居士 苏轼 13333333333 眉州眉山人` 将会被识别为
100101
>
101102
> ```text
102-
> 姓名:Wang lei
103+
> 姓名:东坡居士 苏轼
103104
> 电话:13333333333
105+
> 备注:眉州眉山人
104106
> ```
105107
106108
## 兼容性
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
import re
2-
from typing import NamedTuple
2+
from typing import NamedTuple, Optional
33

44
CHINA_PHONE_PATTERN = re.compile(r"^1[356789]\d{9}$")
55

66

77
class Contact(NamedTuple):
88
name: str
99
phone: str
10+
note: Optional[str] = None
1011

1112

12-
def is_china_phone(phone: str) -> bool:
13-
return CHINA_PHONE_PATTERN.match(phone) is not None
13+
def is_china_mobile_phone(phone: str) -> bool:
14+
return len(phone) == 11 and CHINA_PHONE_PATTERN.match(phone) is not None
1415

1516

1617
def parse_contact(person_text: str):
17-
info_list: list[str] = person_text.rsplit(None, 1)
18-
if len(info_list) != 2:
18+
# 修改分割逻辑以支持备注字段
19+
parts = person_text.split()
20+
if len(parts) < 2:
1921
raise ValueError(f"The person info is illegal: '{person_text}'.")
20-
name, phone = info_list
21-
if not name:
22-
raise ValueError(f"The name is illegal: '{name}'.")
23-
if not is_china_phone(phone):
24-
raise ValueError(f"The phone number is illegal: '{phone}'.")
25-
return Contact(name, phone)
22+
23+
for i, part in enumerate(parts):
24+
if is_china_mobile_phone(part):
25+
phone = part
26+
name = " ".join(parts[:i])
27+
note_parts = parts[i + 1:]
28+
note = " ".join(note_parts) if note_parts else None
29+
break
30+
else:
31+
raise ValueError(f"The person info is illegal: '{person_text}'.")
32+
33+
return Contact(name, phone, note)

src/vcf_generator_lite/services/vcf_generator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ class GenerateResult:
3333

3434

3535
def serialize_to_vcard(contact: Contact):
36-
name_encoded = binascii.b2a_qp(contact.name.encode("utf-8")).decode("utf-8")
3736
return f"""BEGIN:VCARD
3837
VERSION:2.1
39-
FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:{name_encoded}
40-
TEL;CELL:{contact.phone}
38+
FN;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:{binascii.b2a_qp(contact.name.encode("utf-8")).decode("utf-8")}
39+
TEL;CELL:{contact.phone}{f"""
40+
NOTE;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:{contact.note}""" if contact.note else ""}
4141
END:VCARD"""
4242

4343

src/vcf_generator_lite/window/main/constants.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
EVENT_CLEAN_QUOTES = "<<CleanQuotes>>"
33
EVENT_GENERATE = "<<Generate>>"
44

5-
DEFAULT_INPUT_CONTENT = """张三 13345367789
6-
李四 13445467890
7-
王五 13554678907
8-
赵六 13645436748
5+
DEFAULT_INPUT_CONTENT = """张三\t13345367789\t著名的法外狂徒
6+
李四\t13445467890
7+
王五\t13554678907
8+
赵六\t13645436748
99
"""
1010

1111
USAGE = """使用说明:
12-
1. 把名字和电话以每行“姓名 电话号码”的格式复制到下面的编辑框内;
12+
1. 把名字和电话以每行“姓名 电话号码 备注”的格式复制到下面的编辑框内,其中备注可忽略
1313
2. 点击“生成”,选择一个路径保存文件;
1414
3. 将生成后的 VCF 文件复制到手机内,打开文件时选择使用“通讯录”,然后根据提示操作。
1515
4. 等待导入完成"""

0 commit comments

Comments
 (0)