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 string, table, yesno, mCollection, color, libraryutil, arguments = loader.require('String', 'Table', 'Yesno', 'Collection', 'Color', 'LibraryUtil', 'Module:Arguments')

local getArgs = arguments.getArgs local mergeArgsSyntax = arguments.mergeArgsSyntax

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 = mCollection.toTierNum local addColor = color._colorTemplates local checkType = libraryutil.checkTypeLight

local enchant_data, enchant_aliases, potion_data, potion_aliases = loader.loadData('Enchantment/Data','Enchantment/Aliases', 'Potion/Data', 'Potion/Aliases')

local bossCollections = { ['Bonzo'] = 'Bonzo Head', ['Scarf'] = 'Scarf Head', ['The Professor'] = 'The Professor Head', ['Professor'] = 'The Professor Head', ['Thorn'] = 'Thorn Head', ['Livid'] = 'Livid Head', ['Sadan'] = 'Sadan Head', ['Maxor'] = 'Wither Skeleton Skull', ['Goldor'] = 'Wither Skeleton Skull', ['Storm'] = 'Wither Skeleton Skull', ['Necron'] = 'Wither Skeleton Skull', }

local slayer_icons = { ['Zombie Slayer'] = 'Rotten Flesh', ['Spider Slayer'] = 'Web', ['Wolf Slayer'] = 'Raw Mutton', ['Enderman Slayer'] = 'Ender Pearl', }

local align_aliases = { ['l'] = 'left', ['c'] = 'center', ['r'] = 'right', ['t'] = 'thumb', ['n'] = 'none', }

- -- 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	local inter = collection:match('%s(LVL)') or collection:match('%s([Tt]ier)') or collection:match('%s([Ll]evel)') if inter then collection = collection:gsub('%s'..inter, '') else inter = '' end -- 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 local img = '' if collection == 'Professor' then collection = 'The Professor' end if slayer_icons[collection] and showIcon then img = ' ' elseif bossCollections[collection] and showIcon then img = ' ' elseif showIcon then img = tier and (' ') or (' ') end return tier and (img .. '' .. collection ..' '.. inter .. ' ') or (img ..  .. collection .. ) 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

- -- Template:EnchantmentsLink Module Access Point - function p._enchantmentsLink(enchant, alt, options) 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 = enchant_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 = enchant_data[enchant] and enchant_data[enchant].max or 5 local isHighMark = (not string.match(tier or '', '%-')) and (tonumber(toArabic(tier or '')) >= (enchant_data[enchant].highLevelMark or math.huge) and showHighMark) or false if tier and not string.match(tier, '%-') then tier = toRoman(tier) elseif tier and tier:match('%-') then local min, max = tier:lower:match('([%divx]+)%s*%-%s*([%divx]+)') min, max = toArabic(min), toArabic(max) if min >= max or min <= 0 or max <= 0 then return string.error('Invalid enchantment range %s-%s', toRoman(min), toRoman(max)) end tier = toRoman(min) .. '-' .. toRoman(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) return text and ( .. text .. ) or ( .. reforge .. ) 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

p.potionsLink = p.potionName

function p._potion(effect, text, img, potion, format, hover, bold, style) -- 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, '%s[Pp][Oo][Tt][Ii][Oo][Nn][Ss]?$') or string.find(effect, '%s[Pp][Oo][Tt][Ss]?$'))) then potion = true plural = (effect:match('(s)$') and true or false) effect = effect:lower:gsub('%s[Pp][Oo][Tt][Ii][Oo][Nn][Ss]?$', '') effect = effect:gsub('%s[Pp][Oo][Tt][Ss]?$', '') 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 img = img and (' ') or '' str[#str+1] = effect --Add 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 = string.wrapHtml(str, 'b') end if hover == 'DEFAULT' then str = string.wrapHtml(str, 'span', { title = 'Click here to see more information about the effects of ' .. effect .. '.' }) elseif (hover and hover ~= '') then str = string.wrapHtml(str, 'span', { title = hover }) end return preprocess(img ..  .. str .. ) end

- -- Template:FileLink -- -- Makes file linking a tiny bit easier - 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 local multiline = yesno(args['m'] or args['multi'] or args['multiline'], false) local list = mergeArgsSyntax(args) 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 multiline = yesno(list.m or list.mult or list.multiline or multiline, false) 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]) newLineSep = multiline and ' ' or '' 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) then list[1] = '#' .. list[1] .. '' end if (i == 2 and not list[i]:match('[\"\'](.*)[\"\']')) then list[2] = mw.text.nowiki(':') .. name .. ' &lt;' .. list[2] .. '&gt; ' elseif (i == 2 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