Module:Link

-- - -- Module:Link -- This Module is used to house all linking templates on the wiki. -- -- TABLE OF CONTENTS -- *Template: CollectionLink -- *Template: EnchantmentsLink -- *Template: ReforgingLink -- *Template: PotionName -- *Template: PotionsLink -- *Template: FileLink -- *Template: ParserLink - local p = {} local getArgs = require('Module:Arguments').getArgs local yesno = require('Module:Yesno') local formatNum = require('Module:String')._formatNum local toTierNum = require('Module:Collection').toTierNum local toRoman = require('Module:String')._toRoman local toArabic = require('Module:String')._toArabic local addColor = require('Module:Color')._colorTemplates local string = require('Module:String') local splitString = string._splitNameAndTier local _error = string._error local preprocess = string._preprocess local delDoubleSpace = string._delDoubleSpace

local enchData = mw.loadData(Module:Link/enchantmentsLink/Data) local enchantAliases = mw.loadData(Module:Link/enchantmentsLink/Aliases) local ultimateEnchantAliases = mw.loadData(Module:Link/ultimateEnchantmentsLink/Aliases)

- -- Template:CollectionLink -- -- Makes a link that links to a specific collection tier - function p.collectionLink(frame) local args = getArgs(frame) local collection = args[1] local tier = args[2] local showIcon = yesno(args["showIcon"] or args["icon"] or args["image"] or args["img"], false) return p._collectionLink( collection, tier, showIcon ) end function p._collectionLink( collection, tier, showIcon ) -- We want to show LVL in some cases, but never read it	collection = string.gsub(collection, "%sLVL", ""); --if the collection is set to 'none' if collection:lower == 'none' then return ' '..addColor('red', 'None') end if (not tier) then local str = splitString(collection) collection = str[1] tier = str[2] end collection = delDoubleSpace(collection) if tier and not tier:find('%d') then tier = toArabic(tier) end mw.log(tier) local bossCollections = { ["Bonzo"]=1, ["Scarf"]=1, ["The Professor"]=1, ["Professor"]=1, ["Thorn"]=1, ["Livid"]=1, ["Sadan"]=1, ["Maxor"]=1, ["Goldor"]=1, ["Storm"]=1, ["Necron"]=1 } local slayer_icons = { ['Zombie Slayer'] = 'Rotten Flesh', ['Spider Slayer'] = 'Web', ['Wolf Slayer']  = 'Raw Mutton', }	local img = "" if (slayer_icons[collection]) then --slayer collection if (showIcon) then img = " " end if tier then return img..""..collection.." LVL "..tier.."" else return img..""..collection.."" end elseif bossCollections[collection] then --boss collection if collection == "Professor" then collection = "The Professor" end if showIcon then img = table.concat{" "} end if tier then return img..""..collection.." Collection "..toRoman(tier).."" else return img..""..collection.."" end else --normal collection if (showIcon) then if tier then img = " " else img = " " end end if tier then return img..""..collection.." " else return img..""..collection.."" end end end

- -- Template:EnchantmentsLink -- -- Makes a link that links to a specific enchantment - function p.enchantmentsLink(frame) local args = getArgs(frame) local enchant = args[1] local alt = args[2] local book = yesno(args["book"] or args["b"], false) local format = yesno(args["format"] or args["for"] or args["f"], true) local nolink = yesno(args['nolink'] or args['nl'], false) local showHighMark = yesno(args["showhighmark"] or args["showhigh"] or args["showmark"] or args["shm"] or args["sm"], false) local showComma = yesno(args["showcomma"] or args["showcom"] or args["comma"] or args["com"] or args["c"], false) local noRoman = yesno(args["noroman"] or args["norom"] or args["roman"] or args["rom"] or args["r"], false) local noImage = yesno(args["noimage"] or args["noimg"] or args["ni"], false) local tier = args["tier"] or args["t"] return string.pcall(p._enchantmentsLink, enchant, alt, {		book=book, 		tier=tier,		format=format, 		nolink=nolink, 		showHighMark=showHighMark,		showComma=showComma,		noRoman=noRoman,		noImage=noImage,	}) end

function p.getEnchData(name, dataType) checkType('getEnchData', 1, name, 'string') checkType('getEnchData', 2, dataType, 'string') name = name:gsub('^(.+) ([%dIVXivx%-]+)$', '%1'):lower local isUltimate = not enchantAliases[name] and ultimateEnchantAliases[name] local name = enchantAliases[name] or ultimateEnchantAliases[name] if isUltimate then return 5 else return enchData[name][dataType] end end - -- Template:EnchantmentsLink Module Access Point - function p._enchantmentsLink(enchant, alt, options) checkType(1, enchant, { 'string' }, true) checkType(2, alt, { 'string', 'table' }, true) checkType(3, options, { 'table' }, true) local data = enchData local aliases = enchantAliases local options = options or {} if type(alt) == 'table' then options = alt alt = nil end local format, nolink, showHighMark, book, showComma, noimage, noRoman = options.format or options.f,        options.nolink or options.nl, options.showHighMark or options.showmark or options.shm or options.sm, options.book or options.b,       options.showComma or options.comma or options.c,        options.noImage or options.image or options.i,        options.noRoman or options.roman or options.r;	if enchant == nil or enchant ==  then return  end local function splitText(s) local newStr, tier = s:match('^(.+) ([%dIVXivx]+%-?[%dIVXivx]*)$') if not tier then return s, nil end if tier:match('%-') then return newStr, tier:match('^(.+)%s*%-%s*(.+)$') else return newStr, tier end end local function parseText(enchant) enchant = enchant:lower local book = book or enchant:match('%s*e?n?c?h?a?n?t?e?d? ?books?$') if book then enchant = enchant:gsub('%s*e?n?c?h?a?n?t?e?d? ?books?$', '') end local enchant, min, max = splitText(enchant) min, max = min and toArabic(min), max and toArabic(max) enchant = enchant:gsub('[%-_]', ' ') local isUltimate = ultimateEnchantAliases[enchant] local formatted = aliases[enchant] or ultimateEnchantAliases[enchant] local enchant = assertTrue(formatted, 'Invalid Enchantment %q', 3, enchant) local enchData = data[enchant] local maxLevel = enchData and enchData.max or 5 if min == 0 and max == 0 then min = nil max = nil elseif max == 0 then max = nil elseif min == 0 and max > 0 then min = max max = nil end

assertFalse(max and maxLevel maxLevel, 'The maximum enchantment level for %q is %s, got %s instead', 3, enchant, maxLevel, max or min) assertFalse(max and (max == min or min > max), 'Invalid enchantment range "%s-%s": invalid number order', 3, min, max) local marker = enchData and enchData.highLevelMark or math.huge local isHigh = (max or min or 0) >= marker return enchant, (options.tier or min), max, isUltimate, maxLevel, enchData, isHigh, marker, (options.book or book) end local colors = { ['ultimate'] = '#FF55FF', ['high'] = '#FFAA00', ['normal'] = '#5555FF', }

local enchant, min, max, isUltimate, maxLevel, enchData, isHigh, marker, book = parseText(enchant) local tier = table.concat{ min and (not noRoman and toRoman(toArabic(min)) or min) or '', min and max and '-' or '', min and max and (not noRoman and toRoman(max) or max) or '', }	tier = string.styleString(tier, {		['font-family'] = 'Times New Roman',	}) local enchantStr = table.concat{ enchant, (min or max) and ' ' or '', tier, }	local color = isUltimate and colors['ultimate'] or (isHigh and showHighMark) and colors['high'] or colors['normal'] if format == false then color = nil end local formattedText = string.styleString(alt or enchantStr, {		color = color,		['font-weight'] = 'bold'	})

local endPage = (isUltimate and 'Ultimate Enchantments' or 'Enchantments') .. '#' .. enchant local ret = nolink and enchantStr or string.makeLink(endPage, formattedText)

if book then ret = table.concat{ not noimage and string.makeImage('Enchanted Book.png', { size=18, link=not nolink and 'Enchanted Book' or  }) or , not noimage and ' ' or '', noLink and 'Enchanted Book' or string.makeLink('Enchanted Book', string.styleString('Enchanted Book', { ['font-weight'] = 'bold' })), ' ', 			'(', ret, ')' }	end

return ret end

- -- Template:ReforgingLink -- -- Makes a link that links to a specific reforge - function p.reforgingLink(frame) local args = getArgs(frame) local reforge = args[1] local text = args[2] return p._reforgingLink(reforge, text) end function p._reforgingLink(reforge, text) reforge = delDoubleSpace(reforge) if (text) then return ""..text.."" else return ""..reforge.."" end end

- -- Template:PotionsLink -- -- Makes a link that links to a specific potion - function p.potionName(frame) local args = getArgs(frame) local effect = args[1] local text = args[2] local potion = yesno(args["potion"] or args["pot"] or args["p"] or args["sp"]) local img = yesno(args["image"] or args["img"] or args["show_image"] or args["si"]) local format = yesno(args["format"] or args ["f"], true) local hover = args["title"] or args["t"] if (hover == nil or hover == "") then hover = "DEFAULT" end local bold = yesno(args["bold"] or args["b"] or args["show_bold"] or args["sb"], true) local style = args["style"] or args["s"] or args["css"] return p._potion(effect, text, img, potion, format, hover, bold, style) end

function p.potionsLink(frame) local args = getArgs(frame) local effect = args[1] local text = args[2] local potion = yesno(args["potion"] or args["pot"] or args["p"]) local img = yesno(args["image"] or args["img"] or args["show_image"] or args["si"]) local format = yesno(args["format"] or args ["f"], false) local hover = args["title"] or args["t"] if (hover == nil or hover == "") then hover = "" end local bold = yesno(args["bold"] or args["b"] or args["show_bold"] or args["sb"]) local style = args["style"] or args["s"] or args["css"] return p._potion(effect, text, img, potion, format, hover, bold, style) end

function p._potion(effect, text, img, potion, format, hover, bold, style) --Get Potion Data local potion_aliases = require('Module:Link/potion/Aliases') local potion_data = require('Module:Link/potion/Data') -- Remove any multiple spaces effect = delDoubleSpace(effect) --Storage Table local str = {} --find any invalid characters if (effect:match("([%-,%%%.%(%)%{%}%[%]%?/:;]+)")) then return _error('Invalid characters', effect:match("([%-,%%%.%(%)%{%}%[%]%?/:;]+)")) end --get whether or not "Potion" suffix is requested local effect = effect:lower if (potion == nil and (string.find(effect, "%spotions?$") or string.find(effect, "%spots?$"))) then potion = true plural = (effect:match("(s)$") and true or false) effect = effect:lower:gsub("%spotions?$", "") effect = effect:gsub("%spots?$", "") end --get tier number local tier = string.match(effect:upper, "^.* ([%dIVX]+)$") if (tier ~= nil) then --convert to arabic number if (tier:find("[IVX]+")) then tier = toArabic(tier) elseif (tier:find("%d+")) then tier = tonumber(tier) --return error if tier is not matching any valid input else return _error('Invalid potion tier\"'..tier..'\"') end end --get potion name if (tier ~= nil) then effect = effect:match("^(.*) [%dIVXivx]+$") else effect = effect end local src_effect = effect --save source input to later use to display error message --get name from aliases effect = potion_aliases[effect:lower] --return error if effect name is invalid if (not effect) then return _error('Invalid potion name', src_effect) end --check if potion tier is withing the range of 1 to max level if (tier ~= nil) then if (tier > potion_data[effect].maxLevel or tier <= 0) then return _error('Potion tier is out of range: "'..tier..'"') end --convert tier back to roman tier = toRoman(tier) end --Find Image if img then img = " " else img = "" end str[#str+1] = effect --Ad code to the tier num if (tier) then str[#str+1] = " " end if (yesno(potion)) then str[#str+1] = " Potion"..(plural and 's' or '') end str = table.concat(str) if (text) then str = text end if (not style) then style = '' end if (format) then str = '' end if (bold) then str = ""..str.."" end if hover == "DEFAULT" then str = ' '..str.." " elseif (hover == "") then str = str elseif (hover) then str = ' '..str.." " end return preprocess(img..""..str.."") end

- -- Template:ParserLink -- -- Creates a link to a parser function on MediaWiki and provide parameter -- formatting -- -- See also: Dev:Module:T - function p.parserLink(frame) local args = getArgs(frame, {removeBlanks = false}) if args[1] == "" or not args[1] then return _error('No title specified.') end if yesno(args["m"] or args["multi"] or args["multiline"], false) then multiline = true else multiline = false end local list,i = {},1 while args[i] or args[i] == "" do		list[#list+1] = args[i] i = i + 1 end return p._parserLink(list, multiline) end

- -- Template:ParserLink Module Access point - function p._parserLink(list, multiline) if type(list) ~= "table" then error("bad argument #1 to \"_parserLink\": table expected, got "..type(list).."", 2) end if #list == 0 then error('No title specified', 2) end local function delete(v) end if yesno(list.m or list.mult or list.multiline or multiline, false) then multiline = true list.m = delete(list.m)			 list.mult = delete(list.mult) list.multiline = delete(list.multiline) else multiline = false end for i = 1, #list, 1 do		if (type(list[i]) ~= "string" and type(list[i]) ~= "number") then error('Invalid list entry #'..i..": invalid data type (string/number expected, got "..type(list[i])..")", 2) end list[i] = tostring(list[i]) if (multiline) then newLineSep = " " else newLineSep = "" end if list[i]:gsub(mw.text.nowiki('='), '=', 1):match('=') then name, list[i] = list[i]:gsub(mw.text.nowiki('='), '=', 1):match('^(.+)%s*=%s*(.+)$') name = string.wrapHtml(name, 'b')..' = ' else name = '' end if (i == 1 and i ~= 2) then list[1] = '#'..list[1]..'' end if (i == 2 and i ~= 1 and not list[i]:match('[\"\'](.*)[\"\']')) then list[2] = mw.text.nowiki(':')..name..' &lt;'..list[2]..'&gt; ' elseif (i == 2 and i ~= 1 and list[i]:match('[\"\'](.*)[\"\']')) then list[2] = list[2]:gsub('[\"\'](.*)[\"\']', mw.text.nowiki(':')..name..'%1') end if (i ~= 1 and i ~= 2 and list[i] == '') then list[i] = newLineSep..'...' end if (i ~= 1 and i ~= 2 and not list[i]:match('[\"\'](.*)[\"\']')) then list[i] = newLineSep..mw.text.nowiki('|')..name..' &lt;'..list[i]..'&gt; ' elseif i ~= 1 and i ~= 2 and list[i]:match('[\"\'](.*)[\"\']') then list[i] = list[i]:sub(2, -2) list[i] = newLineSep..mw.text.nowiki('|')..name..list[i] end end return ' &#x7b;&#x7b;'..table.concat(list, '')..newLineSep..'&#x7d;&#x7d; ' end

-- Finish Module return p