tags are now separated by ' ' (old format is still readable)

This commit is contained in:
2023-08-05 17:45:43 +02:00
parent caf5244d52
commit f8ed0e3636
2 changed files with 11 additions and 8 deletions
+7 -4
View File
@@ -7,10 +7,13 @@ from typing import List, Dict, Any, Optional
def read_file(fname: pathlib.Path, tags_only: bool = False) -> Dict[str, Any]:
with open(fname, "r") as fd:
tagline = fd.readline().strip().split(':')[1].strip()
# also support tags separated by ',' (old format)
separator = ',' if ',' in tagline else ' '
tags = [t.strip() for t in tagline.split(separator)]
if tags_only:
return {"tags": [x.strip() for x in fd.readline().strip().split(':')[1].strip().split(',')]}
return {"tags": tags}
text = fd.read().strip().split('\n')
tags = [x.strip() for x in text.pop(0).split(':')[1].strip().split(',')]
question_idx = text.index("=== QUESTION ===") + 1
answer_idx = text.index("==== ANSWER ====")
question = "\n".join(text[question_idx:answer_idx]).strip()
@@ -21,7 +24,7 @@ def read_file(fname: pathlib.Path, tags_only: bool = False) -> Dict[str, Any]:
def dump_data(data: Dict[str, Any]) -> str:
with io.StringIO() as fd:
fd.write(f'TAGS: {", ".join(data["tags"])}\n')
fd.write(f'TAGS: {" ".join(data["tags"])}\n')
fd.write(f'=== QUESTION ===\n{data["question"]}\n')
fd.write(f'==== ANSWER ====\n{data["answer"]}\n')
return fd.getvalue()
@@ -29,7 +32,7 @@ def dump_data(data: Dict[str, Any]) -> str:
def write_file(fname: str, data: Dict[str, Any]) -> None:
with open(fname, "w") as fd:
fd.write(f'TAGS: {", ".join(data["tags"])}\n')
fd.write(f'TAGS: {" ".join(data["tags"])}\n')
fd.write(f'=== QUESTION ===\n{data["question"]}\n')
fd.write(f'==== ANSWER ====\n{data["answer"]}\n')