]> Sergey Matveev's repositories - public-inbox.git/blob - examples/cgit-commit-filter.lua
No ext_urls
[public-inbox.git] / examples / cgit-commit-filter.lua
1 -- Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
2 -- License: GPLv2 or later <https://www.gnu.org/licenses/gpl-2.0.txt>
3 -- This commit filter maps a subject line to a search URL of a public-inbox
4 -- disclaimer: written by someone who does not know Lua.
5 --
6 -- This requires cgit linked with Lua
7 -- Usage (in your cgitrc(5) config file):
8 --
9 --   commit-filter=lua:/path/to/this/script.lua
10 --
11 -- Example site: https://80x24.org/public-inbox.git/
12
13 local urls = {}
14 urls['public-inbox.git'] = 'https://public-inbox.org/meta/'
15 -- additional URLs here...
16 -- TODO we should be able to auto-generate this based on "coderepo"
17 -- directives in the public-inbox config file; but keep in mind
18 -- the mapping is M:N between inboxes and coderepos
19
20 function filter_open(...)
21         lineno = 0
22         buffer = ""
23 end
24
25 function filter_close()
26         -- cgit opens and closes this filter for the commit subject
27         -- and body separately, and we only generate the link based
28         -- on the commit subject:
29         if lineno == 1 and string.find(buffer, "\n") == nil then
30                 u = urls[os.getenv('CGIT_REPO_URL')]
31                 if u == nil then
32                         html(buffer)
33                 else
34                         html("<a\ntitle='mail thread'\n")
35                         html('href="' .. u .. '?x=t&amp;q=')
36                         s = string.gsub(buffer, '"', '""')
37                         html_url_arg('"' .. s .. '"')
38                         html('">')
39                         html_txt(buffer)
40                         html('</a>')
41                 end
42         else
43                 -- pass the body-through as-is
44                 -- TODO: optionally use WwwHighlight for linkification like
45                 -- cgit-wwwhighlight-filter.lua
46                 html(buffer)
47         end
48         return 0
49 end
50
51 function filter_write(str)
52         lineno = lineno + 1
53         buffer = buffer .. str
54 end