Module:Link

Jump to navigation Jump to search

Documentation for this module may be created at Module:Link/doc

local getArgument = require('Module:Arguments').getArgument

local p = {}

function capitalFirst(str) -- Allows capitalization of the first letter of each word in a string; adapted from (https://stackoverflow.com/questions/20284515/capitalize-first-letter-of-every-word-in-lua)
    return (str:gsub("(%l)(%w*)", function(a,b) return string.upper(a)..b end))
end

function trim(s) -- Allows removing of whitespace from inputs; adapted from (https://www.lua.org/manual/2.4/node32.html)
	local l = 1
	while string.sub(s,l,l) == ' ' do
	l = l+1
	end
	local r = string.len(s)
	while string.sub(s,r,r) == ' ' do
	r = r-1
	end
	return string.sub(s,l,r)
end

function p.Main(frame)
	local args = frame.args
	
	if args[1] and args[2] and args[1] ~= '' and args[2] ~= '' then
		local type = trim(args[1]:lower())
		local abbr = trim(args[2]:lower())
		if type == 'tv' or type == 'ova' or type == 'trpg' then
			type = type:upper()
		end
		local getType = require('Module:Data/Media/' .. capitalFirst(type))
		if mw.title.makeTitle('Module', 'Data/Media/' .. capitalFirst(type) .. '/raw').exists == true then
			getTypeRaw = require('Module:Data/Media/' .. capitalFirst(type) .. '/raw')
		end
	
		if type == 'TV' then
			type = 'TV series'
		elseif type == 'drama' then
			type = 'audio drama'
		elseif type == 'stage' then
			type = 'stage play'
		end
		
		
		if abbr:sub(-1) == '-' then
			abbr = string.sub(abbr,1,-2)
			if type == 'series' and abbr == 'mtf' then
				diff = ' franchise'
			else
				diff = ' ' .. type
			end
		elseif abbr:sub(-1) == '+' then
			diff = ' (' .. capitalFirst(type) .. ')'
			abbr = string.sub(abbr,1,-2)
		elseif abbr:sub(-1) == '*' then
			abbr = string.sub(abbr,1,-2)
			diff = ' ' .. string.sub(getType.media[abbr],string.find(getType.media[abbr], '%('), string.len(getType.media[abbr]))
		else
			diff = ''
		end
		
		link = getType.media[abbr]
		if getTypeRaw and getTypeRaw.media[abbr] then
			name = "''" .. getTypeRaw.media[abbr] .. "''"
		else
			if link:sub(-1) == ')' then
				name = "''" .. string.sub(link,1,string.find(link, '%(') - 2) .. "''"
			else
				name = "''" .. link .. "''"
			end
		end
		
		finalout = '[[' .. link .. '|' .. name .. diff .. ']]'
	else
		finalout = "''Missing Parameters in Template:Link.''[[Category:Pages with script errors]]"
	end
	
	return finalout
end

return p