Documentation for this module may be created at Module:Image/doc
-- <pre> -- Module - Image
--[=============================================================================[
This module contains functions for parsing and format templates to return images, for use such as galleries.
]=============================================================================]
--=======================================
--[==[Non-Invokable Functions]==]
-- Create a table of another table's keys
function keysToTable(t)
local keyset={}
local n=0
for k,v in pairs(t) do
n=n+1
keyset[n]=k
end
return keyset
end
-- Combine two tables together. Merge any values with non-number keys, append
-- the rest to the end of the 1st table
function mergeTables(t1, t2)
-- error correct for nil arguments
if t1 and not t2 then return t1 end
if t2 and not t1 then return t2 end
-- throw error if an argument isn't a table
if type(t1) ~= 'table' or type(t2) ~= 'table' then
error("Module:HoverTooltip - 'mergeTables' requires two tables. It was given a "
.. type(t1) .. " and a " .. type(t2) .. ".")
end
-- error correct for empty table arguments
local keys1 = keysToTable(t1)
if not keys1[1] then return t2 end
local keys2 = keysToTable(t2)
if not keys2[1] then return t1 end
-- find max numerical key for the first table
local keyMax = 0
for k,_ in ipairs(t1) do
if type(k) == 'number' and k >= keyMax then
keyMax = k
end
end
-- combine tables
for k,v in ipairs(t2) do
if type(k) == 'number' then -- append # keyed values to end
keyMax = keyMax + 1
t1[keyMax] = v
else -- merge/add anything else
t1[k] = v
end
end
return t1
end
--=======================================
--[==[Invokable Functions]==]
-- These Frame Object functions can be called from a wiki article or template
-- with the syntax: {{#invoke:ModuleName|functionName|arg1|arg2|...}}
local p = {}
function p.cardGallery(frame)
-- Create a gallery of card images, linked to their pages (by default)
local args = frame.args
local parent = frame:getParent().args
-- merge frame and parent args
args = mergeTables(args, parent)
-- find unnamed parameters (i.e. list of cards)
-- check both frame and parent args (& combine if needed)
-- format card wikitext
local cards = ''
if args.link == 'none' then -- format without card page links if requested
for i,v in pairs(args) do
if type(i) == 'number' then -- extract only numerically indexed arguments
cards = cards .. '\n' .. v .. '.png'
end
end
else
for i,v in pairs(args) do
if type(i) == 'number' then
cards = cards .. '\n' .. v .. '.png|link=' .. v
end
end
end
-- create <div> wrapper so gallery can be targeted for custom CSS
local result = mw.html.create('div')
:addClass('cardGallery')
-- create gallery structure
local gallery = result:tag('gallery')
:attr({spacing="small", bordersize="none", hideaddbutton="true"})
-- add cards images to the gallery
:wikitext(cards)
result:done()
-- format to display code instead of rendering if requested
if args.pre then
result = tostring(result)
result = '<pre>' .. mw.text.nowiki(result) .. '</pre>'
end
return result
end
--]==]
-- -----------------------------------------------------
return p
-- </pre> [[Category:Eternal Wiki Modules]]