diff options
Diffstat (limited to 'custom/extensions/link-commits.lua')
| -rw-r--r-- | custom/extensions/link-commits.lua | 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1 file changed, 29 insertions, 24 deletions
diff --git a/custom/extensions/link-commits.lua b/custom/extensions/link-commits.lua index 3f2429d..42252f3 100644 --- a/custom/extensions/link-commits.lua +++ b/custom/extensions/link-commits.lua @@ -10,12 +10,10 @@ -- Object names are handled apart from the rules below because the length --- bound on them cannot be written as a plain Lua pattern. Recognition is by --- shape, since a commit-filter cannot ask the repository whether a hash is --- real, so any hex run within the bounds is linked whatever mix of digits and --- letters it has and abbreviated and all-digit names are both caught. The --- cost is that a long hex-looking number now and then links to an object that --- does not exist, which cgit renders as a harmless "bad object name" page. +-- bound on them cannot be written as a plain Lua pattern. A commit-filter +-- cannot ask the repository whether a hash is real, so matching is by shape +-- and a long hex number may link to an object that does not exist, which +-- cgit renders as a harmless "Bad object id" page. local objects = { -- Set false to stop linking bare hashes. enabled = true, @@ -32,24 +30,23 @@ local objects = { -- Text-reference rules, each one a Lua pattern with a single capture and a -- URL where %s is replaced by that capture, percent-encoded. The whole match -- is what gets shown and the capture is only what goes into the URL. Rules are --- tried in order and the leftmost match on the line wins, so put the more --- specific patterns first, and an empty list leaves only object names linked. +-- tried in order and the leftmost match wins, so put the more specific +-- patterns first, and an empty list leaves only object names linked. -- --- Lua patterns are not regular expressions. There is no alternation and no --- {n,m} repetition, %d is a digit, %a a letter, %w a letter or digit, %x a --- hex digit, and a literal magic character is escaped with %, so a literal --- dash is '%-'. The whole set is in the reference manual at +-- Lua patterns are not regular expressions. The reference is -- https://www.lua.org/manual/5.1/manual.html#5.4.1 -- --- Patterns run against the escaped message, so '&', '<' and '>' reach them as --- '&', '<' and '>'. Match those entity spellings rather than the --- bare character, and keep a pattern from ending part way through one, since --- the matched run is what gets wrapped in the anchor. +-- Patterns run against the escaped message, so match the entity spellings +-- '&', '<' and '>' rather than the bare characters, and keep a +-- pattern from ending part way through one. local rules = { { pattern = "#(%d+)", url = "https://bugs.example.com/?bug=%s" }, - -- { pattern = "CVE%-(%d%d%d%d%-%d+)", url = "https://www.cve.org/CVERecord?id=CVE-%s" }, - -- { pattern = "!(%d+)", url = "https://gitlab.example.com/group/repo/-/merge_requests/%s" }, - -- { pattern = "RFC%s?(%d+)", url = "https://www.rfc-editor.org/rfc/rfc%s" }, + -- { pattern = "CVE%-(%d%d%d%d%-%d+)", + -- url = "https://www.cve.org/CVERecord?id=CVE-%s" }, + -- { pattern = "!(%d+)", + -- url = "https://gitlab.example.com/my/repo/-/merge_requests/%s" }, + -- { pattern = "RFC%s?(%d+)", + -- url = "https://www.rfc-editor.org/rfc/rfc%s" }, } @@ -68,7 +65,9 @@ end -- for a gsub reference. local function make_link(url_template, capture, display) local encoded = url_encode(capture) - local href = string.gsub(url_template, "%%s", function() return encoded end) + local href = string.gsub(url_template, "%%s", function() + return encoded + end) return "<a href='" .. href .. "'>" .. display .. "</a>" end @@ -89,7 +88,9 @@ local function collect(text) capture = string.sub(text, start, stop) end candidates[#candidates + 1] = { - start = start, stop = stop, priority = priority, + start = start, + stop = stop, + priority = priority, link = make_link(rule.url, capture, string.sub(text, start, stop)), } @@ -106,9 +107,12 @@ local function collect(text) local start, stop, run = string.find(text, "%f[%w](%x+)%f[%W]", init) if not start then break end - if #run >= objects.min_length and #run <= objects.max_length then + if #run >= objects.min_length + and #run <= objects.max_length then candidates[#candidates + 1] = { - start = start, stop = stop, priority = priority, + start = start, + stop = stop, + priority = priority, link = make_link(objects.url, run, run), } end @@ -141,7 +145,8 @@ function filter_close() -- A candidate reaching back into one already emitted is -- dropped, so no run of text is ever wrapped twice. if candidate.start >= pos then - out[#out + 1] = string.sub(text, pos, candidate.start - 1) + out[#out + 1] = + string.sub(text, pos, candidate.start - 1) out[#out + 1] = candidate.link pos = candidate.stop + 1 end |
