From 75fa7a469607908b3ac08824c948cd2545bf3d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=B8=D0=BD=D0=B0=D1=80=20=D0=9C=D0=B8=D1=80=D0=B7?= =?UTF-8?q?=D0=B0=D0=B3=D0=B8=D1=82=D0=BE=D0=B2?= Date: Wed, 27 May 2026 11:11:13 +0000 Subject: [PATCH] Add main.py --- main.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..31efadd --- /dev/null +++ b/main.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +""" +Simple utility to convert a raw text file into a flat JSON card. +The input format is assumed to be a plain text where each line contains a key and value separated by a colon. +Example: + title: My Card + description: This is a test. + tags: python, example + +The output will be a JSON object with the extracted fields. +""" +import json +import argparse +from pathlib import Path + +def parse_raw_text(text: str) -> dict: + card = {} + for line in text.splitlines(): + if not line.strip() or ':' not in line: + continue + key, value = line.split(':', 1) + card[key.strip()] = value.strip() + return card + +def main(): + parser = argparse.ArgumentParser(description="Convert raw text to flat JSON card") + parser.add_argument("input", type=Path, help="Input raw text file") + parser.add_argument("output", type=Path, help="Output JSON file") + args = parser.parse_args() + + input_text = args.input.read_text(encoding='utf-8') + card = parse_raw_text(input_text) + args.output.write_text(json.dumps(card, ensure_ascii=False, indent=2), encoding='utf-8') + print(f"Card written to {args.output}") + +if __name__ == "__main__": + main()