Moduuli:TORcite

Jedipediasta, vapaasta Tähtien sota-tietosanakirjasta tänään, 19. huhtikuuta 2024
Siirry navigaatioonSiirry hakuun

Tämän moduulin ohjeistuksen voi tehdä sivulle Moduuli:TORcite/ohje

-- This module implements [[Template:TORcite]].

local p = {}

local function makeCategoryLink(cat)
	-- "Category" is split out here so that the module isn't put into the
	-- category "%s" when the page is saved.
	return string.format('[[%s:%s]]', 'Luokka', cat)
end

local function makeWikitextError(msg)
	local ret = ''
	ret = ret .. string.format(
		'<strong class="error">[[Malline:TORcite]]-virhe: %s.</strong>',
		msg
	)
	if mw.title.getCurrentTitle().namespace == 0 then
		ret = ret .. makeCategoryLink('Artikkelit, joissa on mallinevirheitä')
	end
	return ret
end

local function check(str)
	if str == nil then
		str = ""
	end
	return str
end
		
function p.citation(args, data)
	local image = "[[Tiedosto:SWTOR mini.png|x14px|link=Star Wars: The Old Republic]] "
	
	local game = "''[[Star Wars: The Old Republic]]''&mdash;"
	if args.exp then
		game = data.expansions[args.exp]
	end
	
	local src = ''
	if args.chapter then
		local chap = tonumber(args.chapter)
		if not chap then
			src = args[1] or '{{{1}}}'
		else
			if args.exp then
				src = data.chapters[args.exp][chap]
			else
				src = data.chapters.kotfe[chap]
			end
		end
	else		
		local mode = data.modes[args.type]
		if not mode then
			src = args[1] or '{{{1}}}'
		else
			src = check(mode.a) .. check(args[1]) .. check(mode.b) .. check(args[2]) .. check(mode.c) .. check(args[3])
		end
	end
	
	local result = image .. game .. src
	return result
end

function p.main(frame)
	local data = mw.loadData('Moduuli:TORcite/data')
	local args = {}
	for k, v in pairs(frame:getParent().args) do 
		v = v:match('^%s*(.-)%s*$') -- trim whitespace
		if v ~= '' then
			args[k] = v
		end
	end
	return p.citation(args, data) or '{{{1}}}'
end

return p