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 arrayContains(array,value) --Use to see if an array contains a value
for i = 1,#array do
if (array[i] == value) then
return true
end
end
return false
end
function p.Main(frame)
local args = frame.args
if args[1] and args[2] and args[1] ~= '' and args[2] ~= '' then
local mediaType = trim(args[1]:lower())
local abbr = trim(args[2]:lower())
if mediaType == 'tv' or mediaType == 'ova' or mediaType == 'trpg' then
mediaType = mediaType:upper()
end
local getType = mw.loadData('Module:Data/Media/' .. capitalFirst(mediaType))
if mw.title.makeTitle('Module', 'Data/Media/' .. capitalFirst(mediaType) .. '/raw').exists == true then
getTypeRaw = mw.loadData('Module:Data/Media/' .. capitalFirst(mediaType) .. '/raw')
end
if mediaType == 'TV' then
mediaType = 'TV series'
elseif mediaType == 'drama' then
mediaType = 'audio drama'
elseif mediaType == 'stage' then
mediaType = 'stage play'
end
if abbr:sub(-1) == '-' then
abbr = string.sub(abbr,1,-2)
if mediaType == 'series' and abbr == 'mtf' then
diff = ' franchise'
else
diff = ' ' .. mediaType
end
elseif abbr:sub(-1) == '+' then
diff = ' (' .. capitalFirst(mediaType) .. ')'
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
local isShortStory = mediaType == "short story"
if isShortStory or mediaType == "platform" then
name = mw.ustring.gsub(name,"''","")
end
if isShortStory then
finalout = '"[[' .. link .. '|' .. name .. diff .. ']]"'
else
finalout = '[[' .. link .. '|' .. name .. diff .. ']]'
end
else
finalout = "''Missing Parameters in Template:Link.''[[Category:Pages with script errors]]"
end
return finalout
end
return p