mNo edit summary |
mNo edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 7: | Line 7: | ||
--[==[Non-Invokable Functions]==] |
--[==[Non-Invokable Functions]==] |
||
⚫ | |||
function keysToTable(t) |
function keysToTable(t) |
||
⚫ | |||
local keyset={} |
local keyset={} |
||
local n=0 |
local n=0 |
||
Line 20: | Line 20: | ||
end |
end |
||
⚫ | |||
-- Combine two tables together. Merge any values with non-number keys, append |
-- Combine two tables together. Merge any values with non-number keys, append |
||
-- the rest to the end of the 1st table |
-- the rest to the end of the 1st table |
||
⚫ | |||
-- error correct for nil arguments |
-- error correct for nil arguments |
||
if t1 and not t2 then return t1 end |
if t1 and not t2 then return t1 end |
||
Line 103: | Line 103: | ||
bordersize="none", |
bordersize="none", |
||
hideaddbutton="true", |
hideaddbutton="true", |
||
− | orientation="portrait" |
+ | orientation="portrait", |
+ | widths = args.size or '150'} -- default width of 150px, without 'size' parameter |
||
− | if args.size then |
||
− | galleryAttr["widths"] = args.size |
||
− | end |
||
local gallery = result:tag('gallery') |
local gallery = result:tag('gallery') |
Latest revision as of 14:53, 8 July 2020
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]==]
function keysToTable(t)
-- Create a table of another table's keys
local keyset={}
local n=0
for k,v in pairs(t) do
n=n+1
keyset[n]=k
end
return keyset
end
function mergeTables(t1, t2)
-- Combine two tables together. Merge any values with non-number keys, append
-- the rest to the end of the 1st table
-- 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 galleryAttr = {
spacing="small",
bordersize="none",
hideaddbutton="true",
orientation="portrait",
widths = args.size or '150'} -- default width of 150px, without 'size' parameter
local gallery = result:tag('gallery')
:attr(galleryAttr)
-- add cards images to the gallery
:wikitext(cards)
:allDone()
result = tostring(result)
-- return result
if args.pre then -- format to display code instead of rendering if requested
return '<pre>' .. mw.text.nowiki(result) .. '</pre>'
else
return frame:preprocess(result)
end
end
--]==]
-- -----------------------------------------------------
return p
-- </pre> [[Category:Eternal Wiki Modules]]