38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
#!/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()
|