Jump to content

Module:Sandbox/MSGJ

From Wikipedia, the free encyclopedia
require('strict')
local p = {}

p.main = function(frame)
local args = require('Module:Arguments').getArgs(frame)
local title = args.page and mw.title.new(args.page) or mw.title.getCurrentTitle()
	local cfg = {
		forms = {'C T of I', 'I C T', 'C I T', 'C, I T'}
	}
	local class, importance, topic
		for _, form in pairs(cfg.forms) do
			local offsets, order = {}, {}
			for _, s in ipairs{'C', 'I', 'T'} do
				table.insert(offsets, {letter = s, offset = form:find(s)})
			end
			table.sort(offsets, function(m, n) return m.offset<n.offset end)
			for n, v in ipairs(offsets) do
				order[v.letter] = n
			end
			local form2 = form
				:gsub('C', '_C_'):gsub('I', '_I_'):gsub('T', '_T_')
				:gsub('_C_', '(%a+)-Class')
				:gsub('_I_', '(%a+)-importance')
				:gsub('_T_', '(.+) %a+$')
			local pattern = '^' .. form2 .. '$'
			local m1, m2, m3 = title.text:match(pattern)
			if m1 then -- match found
				local m = {m1, m2, m3}
				class = m[order.C]
				importance = m[order.I]
				topic = m[order.T]
				break
			end
		end
	return class, importance, topic
end

return p