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: AchievementLink -- *Template: PotionName -- *Template: PotionsLink -- *Template: FileLink -- *Template: ParserLink - local p = {} local loader = require('Module:Loader') local getArgs = require('Module:Arguments').getArgs local string, table, yesno = loader.require('String', 'Table', 'Yesno')

local toRoman = string._toRoman local toArabic = string._toArabic local splitString = string._splitNameAndTier local _error = string._error local preprocess = string._preprocess local delDoubleSpace = string._delDoubleSpace

local toTierNum = require('Module:Collection').toTierNum local addColor = require('Module:Color')._colorTemplates local checkType = require('libraryUtil').checkType

- -- 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', ['Enderman Slayer'] = 'Ender Pearl', }	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["alt"] or 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) return p._enchantmentsLink(enchant, alt, {		book=book, 		format=format, 		nolink=nolink, 		showHighMark=showHighMark,		showComma=showComma,	}) end

-- function p.getEnchData(name, dataType) --	 checkType('getEnchData', 1, name, 'string') --	 checkType('getEnchData', 2, dataType, 'string') --	 local enchData, enchant_aliases = loader.loadData('Enchantment/Data','Enchantment/Aliases') --	 name = name:gsub('^(.+) ([%dIVXivx%-]+)$', ''):lower --	 local name = enchant_aliases[name] --	 return name and enchData[name][dataType] -- end

- -- Template:EnchantmentsLink Module Access Point - function p._enchantmentsLink(enchant, alt, options) local data, enchant_aliases = loader.loadData('Enchantment/Data','Enchantment/Aliases') checkType('_enchantmentsLink', 1, enchant, 'string', true) checkType('_enchantmentsLink', 2, alt, 'string', true) checkType('_enchantmentsLink', 3, options, 'table', true) local options = options or {} local format, nolink, showHighMark, book, showComma = 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;	local overrideAlt = alt --Exit if enchant is empty/nil if enchant == nil or enchant ==  then return  end local format = alt and false or true if not enchant then return '' end enchant = delDoubleSpace(enchant) if (not book and enchant:lower:find("%s*book$")) then enchant = enchant:gsub("%s*[bB][oO][oO][kK]$", "") book = true end local tier = nil if enchant:match('^(.+) ([%dIVXivx%-]+)$') then enchant, tier = enchant:match('^(.+) ([%dIVXivx%-]+)$') end if (not enchant_aliases[enchant:lower]) then return _error("Invalid enchantment name \"" .. enchant..'\"') end enchant = enchant_aliases[enchant:lower] local isUltimate = data[enchant].isUltimate or false -- Turbo-crop Enchant Handling if enchant:match('^Turbo-') then alt = alt and alt or enchant enchant = "Turbo-Crop" end local maxLevel = data[enchant] and data[enchant].max or 5 local isHighMark = (not string.match(tier or '', '%-')) and (tonumber(toArabic(tier or '')) >= (data[enchant].highLevelMark or math.huge) and showHighMark) or false if tier and not string.match(tier, '%-') then tier = toArabic(tier)

tier = toRoman(tier) elseif tier and tier:match('%-') then min, max = tier:lower:match('([%divx]+)%s*%-%s*([%divx]+)') local min = toArabic(min) local max = toArabic(max) local e = string.error(						'Invalid enchantment range %s-%s', 						toRoman(min), 						toRoman(max)					) if min >= max then return e					elseif min <= 0 or max <= 0 then return e				 end min = toRoman(min) max = toRoman(max) tier = min..'-'..max end return table.concat{ (format and book) and '' or '', book and (nolink and 'Enchanted Book' or 'Enchanted Book') or '', book and (format and ' (' or ' (') or '', nolink and  or (isUltimate and , 		overrideAlt and  or (format and (isUltimate 			and '' 			or (isHighMark				and ''				or ''				)			) or ),		(alt and (overrideAlt and overrideAlt or alt) or enchant),		(overrideAlt and  or (tier and ' '..(			format and string.wrapHtml(tier, 'span', {				style={ 					['font-family']="times new roman"; 				}}) or tier) 			or )),		showComma and not overrideAlt and ', ' or ,		(overrideAlt and  or (format and ' ' or )),		nolink and  or ,		book and ')' or ,	} 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:AchievementLink -- -- Makes a link that links to a specific achievement - function p.achievementLink(frame) local args = getArgs(frame) local achievement = args[1] local text = args[2] local ach = yesno(args['ach'], false) return p._achievementLink(achievement, text, ach) end function p._achievementLink(achievement, text, ach) achievement = delDoubleSpace(achievement) if (text) then return ""..text.."" elseif (ach) == true then return "\""..achievement.."\" Achievement" else return ""..achievement.."" 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_data, potion_aliases = loader.loadData('Potion/Data', 'Potion/Aliases') -- 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:FileLink -- -- Makes file linking a tiny bit easier - local align_aliases = { ["l"] = "left", ["c"] = "center", ["r"] = "right", ["t"] = "thumb", ["n"] = "none", } function p.fileLink(frame) local args = getArgs(frame) local file = args[1] local size = args["size"] or args["s"] or args[2] local align = args["alignment"] or args["align"] or args["a"] local link = args["alt_link"] or args["al"] local yesno_link = yesno(args["link"] or args["l"]) return p._fileLink(file, size, align, link, yesno_link) end function p._fileLink(file, size, align, link, yesno_link) if (not file:find("%.")) then file = file .. ".png" end local text = {} text[#text+1] = "[[File:"..file	if (align) then		align = align_aliases[align] or align		text[#text+1] = "|"..align	end	if (size) then text[#text+1] = "|"..size end	if (not link) then		link = file:match("(.*)%.")	end	if (yesno_link) then		text[#text+1] = "|link="..link	end	text[#text+1] = "]]" return table.concat(text, "") 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