Module:Infobox/Hooks

From Support Wiki
Jump to navigation Jump to search

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

local p = {}

function p.onCastArgsStart(args, sep, kind)
	-- execute code to run before DRUID does its own args parsing
	
	-- the remainder of this function is example code that can be deleted
	
	-- Split out the contents of the arg `items` into several different fields that are all
	-- part of the `Items` section
	
	-- this function processes the following example infobox:
	
	-- {{#invoke:Infobox|main
	-- |kind=exampleCharacter
	-- <!-- code in the infobox template -->
	-- |sections=Abilities,Items
	-- |Abilities=Spells
	-- |Items_columns=3
	-- <!-- note that the contents of `Items` is not defined anywhere here -->
	-- 
	-- <!-- code on the content page -->
	-- |title=Sorcerer
	-- |Spells=Fireball,Chain Lightning
	-- |items=Hat,Cloak,Staff,Wand
	-- }}
	
	-- %s* is "any amount of whitespace", so strip any whitespace that the user provides when doing our logic
	local separator = '%s*' .. sep .. '%s*'
	-- only execute this block of code if we're in the "exampleCharacter" infobox
	if kind == "exampleCharacter" then
		local itemsArg = {} -- an empty table if args.items is empty, otherwise the contents of args.items
		if args.items then
			-- we don't want to try to split an empty string
			itemsArg = mw.text.split(args.items or '', separator)
		end
		local items = {} -- the names of the items data
		for i, item in ipairs(itemsArg) do
			items[i] = 'item' .. i
			args['item' .. i] = item
			args['item' .. i .. '_nolabel'] = "Yes"
		end
		if #items > 0 then
			-- only define args.Items if there is data to define
			-- use the same separator as the rest of the code
			args.Items = table.concat(items, sep)
		end
	end
	
	-- end example code
	
end

function p.onCastArgsEnd(args, sep, kind)
	-- execute code to run after DRUID does its own args parsing
	
	-- the remainder of this function is example code that can be deleted
	
	-- add a `%` at the end of every `Accuracy` value for each tab specified by the user
	
	-- this function processes the following example infobox:
	
	-- {{#invoke:Infobox|main
	-- |kind=exampleWeapon
	-- <!-- code in the infobox template -->
	-- |sections=Stats
	-- |Stats=Damage,Accuracy,Attack Speed
	-- 
	-- <!-- code on the content page -->
	-- |title=Crossbow
	-- |tabs=Lesser,Greater,Legendary
	-- |Lesser_Damage=10 |Greater_Damage=15 |Legendary_Damage=30
	-- |Lesser_Accuracy=30 |Greater_Accuracy=50 |Legendary_Accuracy=90
	-- |Lesser_Attack Speed=5 |Greater_Attack Speed=3 |Legendary_Attack Speed=2
	-- }}
	
	if kind == "exampleWeapon" then
		for _, v in ipairs(args.tabs) do
			local arg = v .. '_Accuracy'
			if args[arg] then
				args[arg] = args[arg] .. '%'
			end
		end
	end
	-- end example code
end

return p