From e34b6de4e12507f2212106d5f3c182c84e2e82f5 Mon Sep 17 00:00:00 2001 From: tv Date: Tue, 17 Nov 2026 13:32:52 +0000 Subject: cgit-about-filter: link directories to their README.md A relative link to a directory goes to the about page of the directory's README.md, if the filter renders it and it exists on the branch being viewed, and to the tree otherwise. One git cat-file checks all links of a page. --- .../simple/cgit-about-filter/cgit-about-filter.lua | 72 +++++++++++++++++++--- pkgs/simple/cgit-about-filter/default.nix | 3 +- 2 files changed, 67 insertions(+), 8 deletions(-) (limited to 'pkgs/simple') diff --git a/pkgs/simple/cgit-about-filter/cgit-about-filter.lua b/pkgs/simple/cgit-about-filter/cgit-about-filter.lua index c7fb09c..8bab6b6 100644 --- a/pkgs/simple/cgit-about-filter/cgit-about-filter.lua +++ b/pkgs/simple/cgit-about-filter/cgit-about-filter.lua @@ -70,6 +70,37 @@ local function md2html(markdown) return run("@md2html@ --github", markdown) end +local function shell_quote(s) + return "'" .. s:gsub("'", "'\\''") .. "'" +end + +-- The files that exist in rev, as a set. +local function existing_files(rev, files) + local found = {} + if #files == 0 then + return found + end + local input = {} + for i, file in ipairs(files) do + input[i] = rev .. ":" .. file .. "\n" + end + local git_dir = shell_quote(os.getenv("CGIT_REPO_PATH") or ".") + local output = run("@git@ --git-dir=" .. git_dir .. " cat-file --batch-check", table.concat(input)) or "" + local i = 0 + for line in output:gmatch("[^\n]*\n") do + i = i + 1 + if line:match("^%x+ blob ") then + found[files[i]] = true + end + end + return found +end + +-- The files the filter renders: README.md, and Markdown files in docs/. +local function renderable(file) + return file == "README.md" or (file:sub(1, 5) == "docs/" and file:sub(-3) == ".md") +end + local table_style = [[