Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 32 additions & 15 deletions codeview/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'<img class="imgfile" src="/{slug}/{quote(path)}" '
Expand All @@ -241,6 +233,19 @@ def file_href(parent_dir: str, child: str) -> 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)
Expand Down Expand Up @@ -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'<div class="readme-head"><a href="/{slug}/{tpath}.html">'
f"{html.escape(top)}</a></div>{body}"
)
elif readme is None:
main = f'<p class="empty">{html.escape(title)}<br>Select a file</p>'
else:
rpath = f"{parent}/{readme}" if parent else readme
Expand All @@ -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",
),
)
Expand Down
2 changes: 1 addition & 1 deletion docs/guarantees.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/` を作らない
Expand Down
7 changes: 5 additions & 2 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<a href="guide.md.html">guide.md</a>' in docs
assert "Select a file" not in docs


def test_no_plugins_means_plain_top(site):
Expand Down
Loading