Module:Set categories

From Sun Haven Wiki
Jump to navigation Jump to search

Used in the {{Set categories}} template.

Subpages

local p = {}
local lib = require('Module:Feature')

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst = true,
		wrapper = { 'Template:Set categories' }
	})

	if string.find(args[1] or "", "»") then
		return p.split_on_arrows(args)
	else
		return p.split_on_asterisk(args)
	end
end

function p.split_on_asterisk(args)
	local items = lib.split((args[1] or ""), ";")
	local formatString = args[2] or ""
	if (formatString == "") then return "" end;
	local result = ""
	for i, item in ipairs(items) do
		local value = formatString:gsub("{item}", lib.split(item, "*")[1])
		result = result .. "[[Category:".. value .. "|"
		if (lib.split(item, "*")[2] ~= nil) then
			result = result .. lib.split(item, "*")[2]
		elseif (lib.split(item, "*")[2] ~= nil) then
			result = result .. lib.split(item, "*")[2]
		else
			result = result .. "1"
		end
		result = string.lower(result) .. "]]"
	end
	return require('Module:Namespace detect').main{ ["main"] = result }
end

function p.split_on_arrows(args)
	local items = lib.split((args[1] or ""), ";")
	local formatString = args[2] or ""
	if (formatString == "") then return "" end;
	local result = ""
	for i, item in ipairs(items) do
		local itemWithoutPrefix = lib.split(item, "«")[1]
		
		local value = formatString:gsub("{item}", lib.split(itemWithoutPrefix, "»")[1])
		
		result = result .. "[[Category:".. value .. "|"

		if (lib.split(itemWithoutPrefix, "»")[2] ~= nil) then
			result = result .. lib.split(itemWithoutPrefix, "»")[2]
		else
			result = result .. "1"
		end
		result = string.lower(result) .. "]]"
	end
	return require('Module:Namespace detect').main{ ["main"] = result }
end
	
return p