From 03b68e1f52bf7b98a29ca7f32db2f5d49fa1fcc7 Mon Sep 17 00:00:00 2001 From: yktsnet Date: Sun, 25 Oct 2026 23:26:20 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20README=20=E3=81=AE=E7=84=A1=E3=81=84?= =?UTF-8?q?=E3=83=87=E3=82=A3=E3=83=AC=E3=82=AF=E3=83=88=E3=83=AA=E3=81=AE?= =?UTF-8?q?=E3=83=9A=E3=83=BC=E3=82=B8=E3=81=AB=E3=80=81=E4=B8=80=E8=A6=A7?= =?UTF-8?q?=E3=81=AE=E5=85=88=E9=A0=AD=E3=81=AE=E3=83=95=E3=82=A1=E3=82=A4?= =?UTF-8?q?=E3=83=AB=E3=82=92=E6=8F=8F=E3=81=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit README の無いディレクトリは「Select a file」だけを出していた。一覧の先頭の ファイルを README と同じ位置に、ファイルのページと同じ描き方で描く。 直下にファイルが無いディレクトリは案内文のまま。 Co-Authored-By: Claude --- codeview/build.py | 47 ++++++++++++++++++++++++++++++--------------- docs/guarantees.md | 2 +- tests/test_build.py | 7 +++++-- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/codeview/build.py b/codeview/build.py index 4ed0980..8632ede 100644 --- a/codeview/build.py +++ b/codeview/build.py @@ -204,22 +204,14 @@ def links_for(md_path: str) -> LinkContext: def file_href(parent_dir: str, child: str) -> str: return f"/{slug}/" + (f"{parent_dir}/" if parent_dir else "") + f"{child}.html" - for path, sha in entries: - data = blobs.get(sha, b"") - parts = path.split("/") - parent = "/".join(parts[:-1]) - fname = parts[-1] - + def file_body(path: str, data: bytes) -> tuple[str, str, str, list[str], bool]: + """(本文, 足すスクリプト, 幅, メタ情報, 本文をコピーできるか)。""" + fname = path.split("/")[-1] extra_script = "" copy_body = False width = "w-md" meta = [human(len(data))] - if path in images: - target = out_root / slug / path - target.parent.mkdir(parents=True, exist_ok=True) - target.write_bytes(data) - is_svg = fname.lower().endswith(".svg") - if is_image(path) and not is_svg: + if is_image(path) and not fname.lower().endswith(".svg"): if path in images: body = ( f' str: copy_body = True width = "w-code" meta[:0] = [_lines(data)] + ([lang] if lang else []) + return body, extra_script, width, meta, copy_body + + for path, sha in entries: + data = blobs.get(sha, b"") + parts = path.split("/") + parent = "/".join(parts[:-1]) + fname = parts[-1] + + if path in images: + target = out_root / slug / path + target.parent.mkdir(parents=True, exist_ok=True) + target.write_bytes(data) + body, extra_script, width, meta, copy_body = file_body(path, data) files = siblings.get(parent, []) i = files.index(fname) @@ -285,8 +290,20 @@ def file_href(parent_dir: str, child: str) -> str: slug, parent, children_of(parent) ) readme = _readme_of(tree.get(parent, {})) + # README が無ければ、一覧の先頭のファイルを README の位置に描く + top = None if readme else next(iter(siblings.get(parent, [])), None) extra_script = "" - if readme is None: + width = "w-md" + if top is not None: + tpath = f"{parent}/{top}" if parent else top + body, extra_script, width, _, _ = file_body( + tpath, blobs.get(dict(entries).get(tpath, ""), b"") + ) + main = ( + f'{body}" + ) + elif readme is None: main = f'

{html.escape(title)}
Select a file

' else: rpath = f"{parent}/{readme}" if parent else readme @@ -309,8 +326,8 @@ def file_href(parent_dir: str, child: str) -> str: main, detail=False, extra_script=extra_script, - width="w-md" if readme else "", - readme=readme is not None, + width=width if readme or top else "", + readme=readme is not None or top is not None, path=f"/{slug}/" + (f"{parent}/" if parent else "") + "index.html", ), ) diff --git a/docs/guarantees.md b/docs/guarantees.md index af2a8f7..519d916 100644 --- a/docs/guarantees.md +++ b/docs/guarantees.md @@ -12,7 +12,7 @@ - 走査対象に無い slug を `[scan.repos]` に書いても build は止まらず、その slug を標準エラーに出す - `skip_dirs` に挙げたディレクトリと、`.git` がファイルであるディレクトリ(worktree)は走査しない - `path` のルートでは、そのリポ1つだけを走査する -- README を持つディレクトリのページには、一覧の下に README を描く。README の無いディレクトリは案内文を出す +- README のあるディレクトリのページには、一覧の下に README を描く。README の無いディレクトリは、一覧の先頭のファイルを同じ位置に描く。直下にファイルが無いディレクトリは案内文を出す - 拡張を設定しなければ、`functions/` には `_middleware.js` だけを書き、トップはリポの一覧と件数だけになる - 設定ファイルが無いときは `error:` を出して非0で終わり、`public/` を作らない - 設定の書式が誤っているときは `error:` を出して非0で終わり、`public/` を作らない diff --git a/tests/test_build.py b/tests/test_build.py index 1be57c5..a00b37f 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -134,8 +134,11 @@ def test_readme_on_directory_page(site): page = (public / "alpha" / "index.html").read_text() assert 'class="readme"' in page assert "alpha の説明" in page - # README の無いディレクトリは案内文のまま - assert "Select a file" in (public / "alpha" / "docs" / "index.html").read_text() + # README の無いディレクトリは、一覧の先頭のファイルを描く + docs = (public / "alpha" / "docs" / "index.html").read_text() + assert 'class="readme"' in docs + assert 'guide.md' in docs + assert "Select a file" not in docs def test_no_plugins_means_plain_top(site):