Jump to content

Module:SAL

From Wikitech
Module documentation

Module for Template:SAL entry

-- Pretty print [[Server Admin Log]] entries
local p = {}

local function linkify(str)
	local placeholders = {}
	local placeholderNum = 0
	local makePlaceholder = function (m)
		local key = "\0" .. placeholderNum .. "\0"
		placeholderNum = placeholderNum + 1
		placeholders[key] = m
		return key
	end
	if (string.find(str, "[%[{]")) then
		-- Replace [[...]], [http...], and {{...}} with placeholders
		str = string.gsub(str, "(%[%[.-%]%])", makePlaceholder)
		str = string.gsub(str, "(%[http.-%])", makePlaceholder)
		str = string.gsub(str, "({{.-}})", makePlaceholder)
	end
	
	-- Gerrit change-id or git commit hash
	-- '/(?<=^|\s)\b(I?[0-9a-f]{6,})\b(?=\s|:|,|$)/'
	str = string.gsub(str, "(.?)%f[%w](I?[0-9a-f]+)%f[%W](.?)",
		function (f, ref, l)
			if (f == "" or f == "(" or f == "[" or string.match(f, "%s")) and 
				string.len(ref) >= 7 and
				(l == "" or l == ":" or l == "," or l == ")" or l == "]" or string.match(l, "%s"))
			then
				return f .. "{{Gerrit|" .. ref .. "}}" .. l
			end
		end
	)
	
	-- Phab task
	-- '#(?<!/)\b(T\d+)\b#'
	str = string.gsub(str, "(.?)%f[%w](T%d+)%f[%W]",
		function (f, task)
			if f ~= "/" then
				return f .. "[[phab:" .. task .. "|" .. task .. "]]"
			end
		end
	)

	if (placeholderNum > 0) then
		-- Expand placeholders
		str = string.gsub(str, "(%z%d+%z)", placeholders)
	end
	return str
end

-- Look through a SAL log line and make interesting things into links
function p.linkify(frame)
	local msg = linkify(frame.args[1])
	if (string.find(msg, "[%[{][%[{]")) then
		-- Expand templates and links
		msg = frame:preprocess(msg)
	end
	return msg
end

return p