Module:Sandbox/Ahecht/navboxtolua

From English Wikipedia @ Freddythechick

This is the current revision of this page, as edited by imported>Ahecht at 21:04, 25 December 2023 (consistency). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

return {[''] = function(frame)
	local lists = {}
	local params = {}
	local numbers = {}
	local args = require('Module:Arguments').getArgs(frame)
	local output = "{\n"
	for k,v in pairs(args) do
		local n = mw.ustring.sub(k,-1)
		if not params[n] then
			table.insert(numbers, n)
			params[n] = {}
		end
		params[n][k] = v
	end
	table.sort( numbers )
	for _,n in ipairs(numbers) do
		a = params[n]
		if a['group'..n] then
			output = output .. 'group' .. n .. ' = "' .. a['group'..n] .. '",\n'
		end
		if a['abbr'..n] then
			output = output .. 'abbr' .. n .. ' = "' .. a['abbr'..n] .. '",\n'
		end
		if a['state'..n] then
			output = output .. 'state' .. n .. ' = "' .. a['state'..n] .. '",\n'
		end
		if a['list'..n] then
			local k = 'list'..n
			local v = a['list'..n]
			if mw.ustring.find(v,'####',1,true) then
				output = output .. k .. ' = _navbox({[1] = "child",\n\n}),\n'
			elseif mw.ustring.find(v,'\n',1,true) then
				table.insert(lists, "===" .. mw.ustring.upper(k) .. "===\n" .. mw.ustring.gsub(mw.ustring.gsub(v,"%[","【"),"%]","】") .. "\n")
				output = output .. k .. ' = [=[<ul>\n<!--' .. mw.ustring.upper(k) .. '-->\n</ul>]=],\n'
			else
				output = output .. k .. ' = [=[<ul>\n<li>'  .. mw.ustring.gsub(v,'^%*%s*','') .. '</li>\n</ul>]=],\n'
			end
		end
	end
	
	return "==Params==\n" .. frame:callParserFunction( '#tag', 'pre', output .. "}" ) .. '\n==Lists==\n' .. table.concat(lists)
	end
}