Module:FlexGallery

Revision as of 05:50, 24 August 2025 by Karl (talk | contribs) (increased the margin between the pictures)

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

local p = {}
local gallery


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, image in pairs(images) do
        local caption = thumbnails[i] or ""
        local line = string.format("[[%s|thumb|none|%s|%s]]",
                                    image, width, caption)
        ul:tag("li")
          :css("display", "inline-block")
          :css("vertical-align", "top")
          :css("margin-right", "8px")
          :wikitext(frame:preprocess(line))
     end
     return gallery
end

p.gallery_with_widths = function(frame)
    local mwText = mw.text
    local images = {}
    local thumbnails = {}
    local widths = {}
    for k, v in pairs(frame.args) do
        v = mwText.trim(tostring(v or ""))
        if (k % 3) == 1 then
            local image = mw.ustring.match(v, "^File:[%w%s%-%._()%',]+$") or ""
            table.insert(images, image)
        elseif (k % 3) == 2 then
            local width = mwText.trim(tostring(v or ""))
            if not mw.ustring.match(width, "^%d+px$") then
                width = "120px"
            end
            table.insert(widths, width)       
        else
            table.insert(thumbnails, tostring(v))          
        end
    end

    local gallery = mw.html.create("div")
    local ul = gallery:tag("ul")
    for i, image in pairs(images) do
        local caption = thumbnails[i] or ""
        local width = widths[i] or ""
        local line = string.format("[[%s|thumb|none|%s|%s]]",
                                    image, width, caption)
        ul:tag("li")
          :css("display", "inline-block")
          :css("vertical-align", "top")
          :css("margin-right", "8px")
          :wikitext(frame:preprocess(line))
     end
     return gallery
end

return p