public-inbox.git  about / heads / tags
an "archives first" approach to mailing lists
blob 9857b3c9572a33a0a3e7723f96a0de5e753a6bba 1568 bytes (raw)
$ git show HEAD:examples/cgit-commit-filter.lua	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 
-- Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
-- License: GPLv2 or later <https://www.gnu.org/licenses/gpl-2.0.txt>
-- This commit filter maps a subject line to a search URL of a public-inbox
-- disclaimer: written by someone who does not know Lua.
--
-- This requires cgit linked with Lua
-- Usage (in your cgitrc(5) config file):
--
--   commit-filter=lua:/path/to/this/script.lua
--
-- Example site: https://80x24.org/public-inbox.git/

local urls = {}
urls['public-inbox.git'] = 'https://public-inbox.org/meta/'
-- additional URLs here...
-- TODO we should be able to auto-generate this based on "coderepo"
-- directives in the public-inbox config file; but keep in mind
-- the mapping is M:N between inboxes and coderepos

function filter_open(...)
	lineno = 0
	buffer = ""
end

function filter_close()
	-- cgit opens and closes this filter for the commit subject
	-- and body separately, and we only generate the link based
	-- on the commit subject:
	if lineno == 1 and string.find(buffer, "\n") == nil then
		u = urls[os.getenv('CGIT_REPO_URL')]
		if u == nil then
			html(buffer)
		else
			html("<a\ntitle='mail thread'\n")
			html('href="' .. u .. '?x=t&amp;q=')
			s = string.gsub(buffer, '"', '""')
			html_url_arg('"' .. s .. '"')
			html('">')
			html_txt(buffer)
			html('</a>')
		end
	else
		-- pass the body-through as-is
		-- TODO: optionally use WwwHighlight for linkification like
		-- cgit-wwwhighlight-filter.lua
		html(buffer)
	end
	return 0
end

function filter_write(str)
	lineno = lineno + 1
	buffer = buffer .. str
end

git clone https://public-inbox.org/public-inbox.git
git clone http://7fh6tueqddpjyxjmgtdiueylzoqt6pt7hec3pukyptlmohoowvhde4yd.onion/public-inbox.git