模組:Science redirect

local conf = require( "Module:Science redirect/conf" )

local p = {}

function p.R(frame)
	local template = mw.ustring.gsub(frame.args[1], ' ', '_')
	if conf.templates[template] then
		return p._main(frame, 'R_' .. template, conf.templates[template].name, conf.templates[template].from, conf.templates[template].to, conf.templates[template].category, conf.templates[template].info)
	elseif template then
		return '<span class="error">'..template..'模板不可用。</span>\n'
	else
		return '<span class="error">未指明任何模板。</span>\n'
	end
end

function p.list(frame)
	local outstr = ""
	local ocats={}

	for _, v in pairs(conf.cats) do ocats[v] = true end
	
	for k, _ in pairs(ocats) do
	    outstr = outstr .. "\n=== " .. frame:expandTemplate{ title = 'cl', args = { (k .. "重定向"), count = "yes" } } .. " ===\n"
	    for kk, vv in pairs (conf.templates) do
	        outstr = outstr .. "*" .. frame:expandTemplate{ title = 'cl', args = { (mw.ustring.gsub(vv.category, '$1', k) .. "重定向"), count = "yes" } } .. "\n"
	    end
	end
	
	return outstr
end

function p._main(frame, id, name, from, to, category, info)
	--initialize variables
	local args = frame:getParent().args
	local chineseNoun = ''
	local outStr = ''
	
	--Check for known parameter 1
	local cat = mw.ustring.match(mw.ustring.lower(args[1] or 'none'), '^(.-)s?$')
	if conf.cats[cat] then chineseNoun = conf.cats[cat] else
		chineseNoun = ''
		outStr = '[[Category:使用学名重定向未知参数1]]'
	end
	
	--support alternative indications for printworthy
	if args[2] == 'unprintworthy' or args['unprintworthy'] == 'true' then args['printworthy'] = 'no' end
	
	--build template arguments
	local outArgs = {
		id = id,
		name = mw.ustring.gsub(name..'重定向', '$1', chineseNoun),
		from = mw.ustring.gsub(from, '$1', chineseNoun),
		to = mw.ustring.gsub(to, '$1', chineseNoun),
		['all category'] = mw.ustring.gsub(category..'重定向', '$1', chineseNoun),
		printworthy = (args['printworthy'] or 'yes'),
		info = info,
	}
	
	--build output string
	if frame.args['debug'] == 'true' then
		local debugStr = '{{Redirect template<br />\n'
		for k,v in pairs( outArgs ) do
			debugStr = debugStr..'| '..k..' = '..v..'<br />\n'
		end
		outStr = debugStr..'}}'..frame:extensionTag{ name = 'nowiki', content = outStr}
	else
		outStr = frame:expandTemplate{ title = 'Redirect template', args = outArgs }..outStr
	end
	
	return outStr
end

return p