Module:FlexGallery: Difference between revisions
Appearance
No edit summary |
Cleanup + sanitization based on chatgpt |
||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local gallery | local gallery | ||
-- frame.args | -- frame.args | ||
p. | p.gallery = function(frame) | ||
local mwText = mw.text | |||
local width = mwText.trim(tostring(frame.args[1] or "")) | |||
if not mw.ustring.match(width, "^%d+px$") then | |||
width = "120px" | |||
end | |||
local images = {} | |||
local thumbnails = {} | |||
for k, v in pairs(frame.args) do | for k, v in pairs(frame.args) do | ||
v = v | v = mwText.trim(tostring(v or "")) | ||
if k ~= 1 then | |||
if k | if (k % 2) == 0 then | ||
local image = mw.ustring.match(v, "^File:[%w%s%-%._()%',]+$") or "" | |||
table.insert(images, image) | |||
else | |||
table.insert(thumbnails, tostring(v)) | |||
end | |||
end | end | ||
end | end | ||
local gallery = " | local gallery = mw.html.create("div") | ||
for | local ul = gallery.tag("ul") | ||
for i, images in pairs(images) do | |||
local caption = thumbnails[i] or "" | |||
local line = string.format("[[%s|thumb|none|%spx|%s]]", | local line = string.format("[[%s|thumb|none|%spx|%s]]", | ||
image, width, caption) | |||
ul:tag("li") | |||
:css("display", "inline-block") | |||
:css("vertical-align", "top") | |||
:wikitext(frame:preprocess(line)) | |||
end | end | ||
return gallery | |||
return | |||
end | end | ||
return p | return p | ||
Revision as of 04:17, 21 August 2025
Documentation for this module may be created at Module:FlexGallery/doc
local p = {}
local gallery
-- frame.args
p.gallery = function(frame)
local mwText = mw.text
local width = mwText.trim(tostring(frame.args[1] or ""))
if not mw.ustring.match(width, "^%d+px$") then
width = "120px"
end
local images = {}
local thumbnails = {}
for k, v in pairs(frame.args) do
v = mwText.trim(tostring(v or ""))
if k ~= 1 then
if (k % 2) == 0 then
local image = mw.ustring.match(v, "^File:[%w%s%-%._()%',]+$") or ""
table.insert(images, image)
else
table.insert(thumbnails, tostring(v))
end
end
end
local gallery = mw.html.create("div")
local ul = gallery.tag("ul")
for i, images in pairs(images) do
local caption = thumbnails[i] or ""
local line = string.format("[[%s|thumb|none|%spx|%s]]",
image, width, caption)
ul:tag("li")
:css("display", "inline-block")
:css("vertical-align", "top")
:wikitext(frame:preprocess(line))
end
return gallery
end
return p