|
1 | 1 | import argparse |
2 | 2 | from pathlib import Path |
3 | 3 |
|
| 4 | +import pandas as pd |
| 5 | + |
4 | 6 | import pygambit as gbt |
5 | 7 |
|
6 | | -CATALOG_CSV = Path(__file__).parent.parent.parent / "doc" / "catalog.csv" |
| 8 | +CATALOG_RST_TABLE = Path(__file__).parent.parent.parent / "doc" / "catalog_table.rst" |
7 | 9 | CATALOG_DIR = Path(__file__).parent.parent.parent / "catalog" |
8 | 10 | MAKEFILE_AM = Path(__file__).parent.parent.parent / "Makefile.am" |
9 | 11 |
|
10 | 12 |
|
| 13 | +def _write_efg_table(df: pd.DataFrame, f): |
| 14 | + """Write the EFG games list-table to file handle f.""" |
| 15 | + f.write(".. list-table::\n") |
| 16 | + f.write(" :header-rows: 1\n") |
| 17 | + f.write(" :widths: 100\n") |
| 18 | + f.write(" :class: tight-table\n") |
| 19 | + f.write("\n") |
| 20 | + f.write(" * - **Extensive form games**\n") |
| 21 | + |
| 22 | + efg_df = df[df["Format"] == "efg"] |
| 23 | + for _, row in efg_df.iterrows(): |
| 24 | + slug = row["Game"] |
| 25 | + title = str(row.get("Title", "")).strip() |
| 26 | + description = str(row.get("Description", "")).strip() |
| 27 | + # Skip any games which lack a description |
| 28 | + if description: |
| 29 | + # Main dropdown |
| 30 | + f.write(f" * - .. dropdown:: {title}\n") |
| 31 | + f.write(" :open:\n") |
| 32 | + f.write(" \n") |
| 33 | + for line in description.splitlines(): |
| 34 | + f.write(f" {line}\n") |
| 35 | + f.write(" \n") |
| 36 | + f.write(" **Load in PyGambit:**\n") |
| 37 | + f.write(" \n") |
| 38 | + f.write(" .. code-block:: python\n") |
| 39 | + f.write(" \n") |
| 40 | + f.write(f' pygambit.catalog.load("{slug}")\n') |
| 41 | + f.write(" \n") |
| 42 | + |
| 43 | + # Download links (inside the dropdown) |
| 44 | + download_links = [row["Download"]] |
| 45 | + f.write(" **Download:**\n") |
| 46 | + f.write(" \n") |
| 47 | + f.write(f" {' '.join(download_links)}\n") |
| 48 | + f.write(" \n") |
| 49 | + |
| 50 | + |
| 51 | +# def _write_nfg_table(df: pd.DataFrame, f): |
| 52 | +# """Write the NFG games list-table to file handle f.""" |
| 53 | +# f.write(".. list-table::\n") |
| 54 | +# f.write(" :header-rows: 1\n") |
| 55 | +# f.write(" :widths: 100\n") |
| 56 | +# f.write(" :class: tight-table\n") |
| 57 | +# f.write("\n") |
| 58 | +# f.write(" * - **Strategic form games**\n") |
| 59 | + |
| 60 | +# nfg_df = df[df["Format"] == "nfg"] |
| 61 | +# for _, row in nfg_df.iterrows(): |
| 62 | +# slug = row["Game"] |
| 63 | + |
| 64 | +# # Title as plain text header |
| 65 | +# f.write(" * - \n") |
| 66 | +# f.write(" \n") |
| 67 | + |
| 68 | +# # Jupyter-execute block (no dropdown) |
| 69 | +# f.write(" .. jupyter-execute::\n") |
| 70 | +# f.write(" \n") |
| 71 | +# f.write(" import pygambit\n") |
| 72 | +# f.write(f' pygambit.catalog.load("{slug}")\n') |
| 73 | +# f.write(" \n") |
| 74 | + |
| 75 | +# # Download link (plain, no dropdown) |
| 76 | +# f.write(f" :download:`{slug}.nfg <../catalog/{slug}.nfg>`\n") |
| 77 | +# f.write(" \n") |
| 78 | + |
| 79 | + |
| 80 | +def generate_rst_table(df: pd.DataFrame, rst_path: Path): |
| 81 | + """Generate RST output with two list-tables: one for EFG and one for NFG games.""" |
| 82 | + |
| 83 | + with open(rst_path, "w", encoding="utf-8") as f: |
| 84 | + # TOC linking to both sections |
| 85 | + # f.write(".. contents::\n") |
| 86 | + # f.write(" :local:\n") |
| 87 | + # f.write(" :depth: 1\n") |
| 88 | + # f.write("\n") |
| 89 | + |
| 90 | + # EFG section |
| 91 | + # f.write("Extensive form games\n") |
| 92 | + # f.write("--------------------\n") |
| 93 | + # f.write("\n") |
| 94 | + _write_efg_table(df, f) |
| 95 | + # f.write("\n") |
| 96 | + |
| 97 | + # # NFG section |
| 98 | + # f.write("Strategic form games\n") |
| 99 | + # f.write("--------------------\n") |
| 100 | + # f.write("\n") |
| 101 | + # _write_nfg_table(df, f) |
| 102 | + |
| 103 | + |
11 | 104 | def update_makefile(): |
12 | 105 | """Update the Makefile.am with all games from the catalog.""" |
13 | 106 |
|
@@ -60,15 +153,14 @@ def update_makefile(): |
60 | 153 |
|
61 | 154 |
|
62 | 155 | if __name__ == "__main__": |
63 | | - |
64 | 156 | parser = argparse.ArgumentParser() |
65 | 157 | parser.add_argument("--build", action="store_true") |
66 | 158 | args = parser.parse_args() |
67 | 159 |
|
68 | | - # Create CSV used by RST docs page |
69 | | - gbt.catalog.games().to_csv(CATALOG_CSV, index=False) |
70 | | - print(f"Generated {CATALOG_CSV} for use in local docs build. DO NOT COMMIT.") |
71 | | - |
72 | | - # Update the Makefile.am with the current list of catalog files |
| 160 | + # Create RST list-table used by doc/catalog.rst |
| 161 | + df = gbt.catalog.games(include_descriptions=True) |
| 162 | + generate_rst_table(df, CATALOG_RST_TABLE) |
| 163 | + print(f"Generated {CATALOG_RST_TABLE} for use in local docs build. DO NOT COMMIT.") |
73 | 164 | if args.build: |
| 165 | + # Update the Makefile.am with the current list of catalog files |
74 | 166 | update_makefile() |
0 commit comments