Read a file from disk and return a structured document.
(path: str)
| 10 | |
| 11 | |
| 12 | def parse_file(path: str) -> dict: |
| 13 | """Read a file from disk and return a structured document.""" |
| 14 | with open(path, "r") as f: |
| 15 | raw = f.read() |
| 16 | |
| 17 | ext = path.rsplit(".", 1)[-1].lower() |
| 18 | if ext == "md": |
| 19 | doc = parse_markdown(raw) |
| 20 | elif ext == "json": |
| 21 | doc = parse_json(raw) |
| 22 | else: |
| 23 | doc = parse_plaintext(raw) |
| 24 | |
| 25 | doc["source"] = path |
| 26 | return doc |
| 27 | |
| 28 | |
| 29 | def parse_markdown(text: str) -> dict: |
no test coverage detected