Jump to content

Module:FlexGallery: Difference between revisions

From Climbopedia
No edit summary
Added versions with captions
 
(26 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local width
local images = {}
local thumbnails = {}
local gallery
local gallery


-- frame.args


p.hi = function(frame)
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", "12px")
          :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", "12px")
          :wikitext(frame:preprocess(line))
    end
    return gallery
end
 
p.gallery_with_alt = 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 = {}
    local alts = {}
    for k, v in pairs(frame.args) do
        v = mwText.trim(tostring(v or ""))
        if k ~= 1 then
            if (k % 3) == 2 then
                local image = mw.ustring.match(v, "^File:[%w%s%-%._()%',]+$") or ""
                table.insert(images, image)
            elseif (k % 3) == 0 then
                table.insert(thumbnails, tostring(v))
            else
                table.insert(alts, 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 alt = alts[i] or ""
        local line = string.format("[[%s|thumb|none|%s|%s|alt=%s]]",
                                    image, width, caption, alt)
        ul:tag("li")
          :css("display", "inline-block")
          :css("vertical-align", "top")
          :css("margin-right", "12px")
          :wikitext(frame:preprocess(line))
    end
    return gallery
end
 
p.gallery_with_widths_alt = function(frame)
    local mwText = mw.text
    local images = {}
    local thumbnails = {}
    local widths = {}
    local alts = {}
     for k, v in pairs(frame.args) do
     for k, v in pairs(frame.args) do
         -- v = v:gsub("[\n\r]", "") seems unnecessary..
         v = mwText.trim(tostring(v or ""))
         v = tostring(v)
        if (k % 4) == 1 then
        if k == 1 then
            local image = mw.ustring.match(v, "^File:[%w%s%-%._()%',]+$") or ""
            width = tonumber(v)
            table.insert(images, image)
         elseif (k % 2) == 0 then
         elseif (k % 4) == 2 then
             table.insert(images, tostring(v))
            local width = mwText.trim(tostring(v or ""))
            if not mw.ustring.match(width, "^%d+px$") then
                width = "120px"
            end
            table.insert(widths, width)
         elseif (k % 4) == 3 then
             table.insert(thumbnails, tostring(v))  
         else
         else
             table.insert(thumbnails, tostring(v))
             table.insert(alts, tostring(v))
         end
         end
     end
     end
    local gallery = "<div><ul>"
    for k, v in pairs(images) do
        local line = "[[" .. v .. "|thumb|none|" .. width .. "px|" .. thumbnails[k] .. "]]"
        local li = mw.html.create('li')
        li
            :css("display", "inline-block")
            :css("vertical-align", "top")
            :wikitext(frame:preprocess(line))
        line = "[[File:carabine_edge_1.png |thumb|none|120px|Positioning carabiner over an edge atypically loads the carabiner]]"


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


return p
return p

Latest revision as of 15:51, 5 November 2025

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", "12px")
          :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", "12px")
          :wikitext(frame:preprocess(line))
     end
     return gallery
end

p.gallery_with_alt = 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 = {}
    local alts = {}
    for k, v in pairs(frame.args) do
        v = mwText.trim(tostring(v or ""))
        if k ~= 1 then
            if (k % 3) == 2 then
                local image = mw.ustring.match(v, "^File:[%w%s%-%._()%',]+$") or ""
                table.insert(images, image)
            elseif (k % 3) == 0 then
                table.insert(thumbnails, tostring(v))
            else
                table.insert(alts, 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 alt = alts[i] or ""
        local line = string.format("[[%s|thumb|none|%s|%s|alt=%s]]",
                                    image, width, caption, alt)
        ul:tag("li")
          :css("display", "inline-block")
          :css("vertical-align", "top")
          :css("margin-right", "12px")
          :wikitext(frame:preprocess(line))
     end
     return gallery
end

p.gallery_with_widths_alt = function(frame)
    local mwText = mw.text
    local images = {}
    local thumbnails = {}
    local widths = {}
    local alts = {}
    for k, v in pairs(frame.args) do
        v = mwText.trim(tostring(v or ""))
        if (k % 4) == 1 then
            local image = mw.ustring.match(v, "^File:[%w%s%-%._()%',]+$") or ""
            table.insert(images, image)
        elseif (k % 4) == 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)
        elseif (k % 4) == 3 then
            table.insert(thumbnails, tostring(v)) 
        else
            table.insert(alts, 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 alt = alts[i] or ""
        local line = string.format("[[%s|thumb|none|%s|%s|alt=%s]]",
                                    image, width, caption, alt)
        ul:tag("li")
          :css("display", "inline-block")
          :css("vertical-align", "top")
          :css("margin-right", "12px")
          :wikitext(frame:preprocess(line))
     end
     return gallery
end

return p