Files
2dph/bin/md/import
T
eSlider 8781c0c3eb refactor(tools): bin/{subject}/{method} layout; Go serve+watch modules
Move serve/ (module) -> bin/server, tools/ -> bin/tools, replace bin/kb-watch
bash with bin/watch Go package; self-executing Go shebangs bin/serve.go and
bin/kb/watch.go; Docker + CI + git/import + docs repointed. Multi-stage image
builds static serve+watch binaries (no Go runtime in container).
2026-08-11 09:52:20 +01:00

50 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
from __future__ import annotations
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools"))
from mdleaves import leaves_to_json, read_markdown, to_all, walk_markdown # noqa: E402
from yamlout import to_yaml # noqa: E402
def main(argv: list[str]) -> int:
import argparse
p = argparse.ArgumentParser(description="md/import - split markdown corpus into leafs")
p.add_argument("root", nargs="?", default=".", help="directory to walk for .md files")
p.add_argument("--files", action="store", help="comma-separated file list")
p.add_argument("--json", action="store_true")
a = p.parse_args(argv)
paths: list[Path] = []
if a.files:
paths = [Path(f) for f in a.files.split(",")]
else:
root = Path(a.root)
if not root.exists():
print(f"md/import: no such path {root}", file=sys.stderr)
return 2
paths = [root] if root.is_file() else walk_markdown(root)
if not paths:
print("md/import: no markdown files", file=sys.stderr)
return 1
all_leafs: list[dict] = []
for path in paths:
try:
text = read_markdown(path)
except OSError as e:
print(f"md/import: {path}: {e}", file=sys.stderr)
continue
all_leafs.extend(to_all(text, path))
out = leaves_to_json(all_leafs)
print(out if a.json else to_yaml(__import__("json").loads(out)))
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))