diff --git a/osmsg/export/markdown.py b/osmsg/export/markdown.py index cfac160..3604146 100644 --- a/osmsg/export/markdown.py +++ b/osmsg/export/markdown.py @@ -17,15 +17,16 @@ def _stringify(v: Any) -> str: return str(v) -def table_markdown(rows: list[dict[str, Any]], headers: list[str] | None = None) -> str: +def table_markdown(rows: list[dict[str, Any]], output_path: Path, headers: list[str] | None = None) -> Path: """Return a GitHub-flavored markdown table for the given rows.""" - if not rows: - return "" headers = headers or list(rows[0].keys()) lines = ["| " + " | ".join(headers) + " |", "| " + " | ".join("---" for _ in headers) + " |"] for r in rows: lines.append("| " + " | ".join(_stringify(r.get(h)) for h in headers) + " |") - return "\n".join(lines) + output_path = Path(output_path) + output_path.parent.mkdir(parents=True, exist_ok=True) + output_path.write_text("\n".join(lines), encoding="utf-8") + return output_path def _human(n: int) -> str: diff --git a/osmsg/pipeline.py b/osmsg/pipeline.py index 636805b..50d2543 100644 --- a/osmsg/pipeline.py +++ b/osmsg/pipeline.py @@ -22,7 +22,7 @@ from .db.queries import attach_metadata, attach_tag_stats, daily_summary, list_changesets, user_stats from .db.schema import get_state, upsert_state from .exceptions import CredentialsRequiredError, NoDataFoundError, OsmsgError -from .export import summary_markdown, to_csv, to_json, to_parquet, to_psql +from .export import summary_markdown, table_markdown, to_csv, to_json, to_parquet, to_psql from .fetch import download_osm_file from .geofabrik import country_geometry, country_update_url from .replication import ( @@ -521,19 +521,10 @@ def run(cfg: RunConfig) -> dict[str, Any]: written["json"] = str(to_json(rows, out / f"{cfg.name}.json")) if "markdown" in cfg.formats: - from .export.markdown import summary_markdown as render_md - md_path = out / f"{cfg.name}.md" - render_md( + table_markdown( rows, output_path=md_path, - start_date=start_date_utc, - end_date=end_date_utc, - additional_tags=cfg.additional_tags, - length_tags=cfg.length_tags, - tag_mode=cfg.tag_mode, - fname=cfg.name, - tm_stats=cfg.tm_stats, ) written["markdown"] = str(md_path) diff --git a/tests/test_export.py b/tests/test_export.py index 7faf389..e4ed25e 100644 --- a/tests/test_export.py +++ b/tests/test_export.py @@ -61,9 +61,14 @@ def test_json_writes_native_types(tmp_path: Path): assert payload[0]["tags_create"] == {"building": 5} -def test_table_markdown_renders_header_and_rows(): - md = table_markdown(SAMPLE_ROWS, headers=["rank", "name", "map_changes"]) - lines = md.splitlines() +def test_table_markdown_writes_header_and_rows(tmp_path: Path): + output = table_markdown( + SAMPLE_ROWS, + output_path=tmp_path / "stats.md", + headers=["rank", "name", "map_changes"], + ) + body = output.read_text(encoding="utf-8") + lines = body.splitlines() assert lines[0] == "| rank | name | map_changes |" assert lines[1] == "| --- | --- | --- |" assert "alice" in lines[2]