diff options
| -rw-r--r-- | pkgs/simple/cgit-about-filter/cgit-about-filter.lua | 72 | ||||
| -rw-r--r-- | pkgs/simple/cgit-about-filter/default.nix | 3 |
2 files changed, 67 insertions, 8 deletions
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 = [[ <style> div#cgit div#summary table { @@ -258,8 +289,7 @@ function filter_open(...) current_file = "README.md" end - -- Strict route enforcement: Allow README.md OR any file starting with docs/ and ending in .md - if current_file == "README.md" or (current_file:sub(1, 5) == "docs/" and current_file:sub(-3) == ".md") then + if renderable(current_file) then should_render = true -- Extract directory context (e.g., "docs/ops.md" -> "docs/") @@ -296,16 +326,44 @@ function filter_close() query = "?h=" .. url_escape(head) end - local processed = buffer:gsub("%[([^%]]+)%]%(([^:)]+)%)", function(text, path) - -- Ignore protocols (http, https, etc.), absolute paths (/), and anchors (#) + local link = "%[([^%]]+)%]%(([^:)]+)%)" + + -- A link's path, resolved, and its fragment; nil for protocols (http, + -- https, etc.), absolute paths (/), and anchors (#). + local function target(path) if path:sub(1, 1) == "/" or path:sub(1, 1) == "#" or path:match("^%w+:") then - return string.format("[%s](%s)", text, path) + return nil end - local fragment path, fragment = path:match("^([^#]*)(.*)$") + return resolve(current_dir, path), fragment + end + + -- The README.md of a linked directory, if the filter renders it. + local function readme(path) + local file = (path == "" and "" or path .. "/") .. "README.md" + return renderable(file) and file or nil + end - path = resolve(current_dir, path) + local readmes = {} + for _, path in buffer:gmatch(link) do + path = target(path) + if path and readme(path) then + table.insert(readmes, readme(path)) + end + end + local existing = existing_files(head or os.getenv("CGIT_REPO_DEFBRANCH") or "HEAD", readmes) + + local processed = buffer:gsub(link, function(text, path) + local fragment + path, fragment = target(path) + if not path then + return nil + end + + if existing[readme(path)] then + path = readme(path) + end if path:sub(-3) == ".md" then return string.format("[%s](/%s/about/%s%s%s)", text, repo, path, query, fragment) diff --git a/pkgs/simple/cgit-about-filter/default.nix b/pkgs/simple/cgit-about-filter/default.nix index 215a44e..a05020f 100644 --- a/pkgs/simple/cgit-about-filter/default.nix +++ b/pkgs/simple/cgit-about-filter/default.nix @@ -1,5 +1,6 @@ -{ lib, md4c, replaceVars }: +{ gitMinimal, lib, md4c, replaceVars }: replaceVars ./cgit-about-filter.lua { + git = lib.getExe gitMinimal; md2html = lib.getExe md4c; } |
