Module:Gallery

From Dune: Awakening Community Wiki
Jump to navigation Jump to search

Module:Gallery

Renders a responsive flexbox media gallery.

This module should not be invoked directly unless omitting the Gallery heading is intentional. To add a normal gallery to a page, use Template:Gallery.

This module provides the following function(s):

  • main - parses a tilde-delimited list of media items and renders them as a flexbox gallery.

Usage

Render a gallery

{{#invoke:Gallery|main|
Sandbike - Atreides.jpg~Atreides Sandbike Variant
Sandbike - Harkonnen.jpg~Harkonnen Sandbike Variant
}}

Parameters

main

1
string
Multi-line list of gallery items, one per line.
Each line is a filename, optionally followed by a caption delimited with a tilde: Filename.jpg~Optional caption.
Supports all media types accepted by [[File:...]], including video.

local p = {}

local common = require("Module:Common")

-- ---------- Data ----------

local ROW_HEIGHT = 258  -- max height for 16:9 ratio landscape images to stack into 2 columns on mobile screens

-- ---------- Rendering ----------

function p.render(input)
    local container = mw.html.create("div"):addClass("gallery-flex")

    for line in input:gmatch("[^\r\n]+") do
        line = common.trim(line)
        if line ~= "" then
            local parts = common.splitTilde(line)
            local file, caption = parts[1], parts[2]

            local item = container:tag("div"):addClass("gallery-item")
            item:tag("div"):addClass("gallery-file")
                :css("height", ROW_HEIGHT .. "px")
                :wikitext(string.format("[[File:%s|frameless|x%dpx]]", file, ROW_HEIGHT * 2))

            if caption then
                if #parts > 2 then
                    caption = common.inputError("File caption cannot contain another tilde <code>~</code>")
                end

                item:tag("div"):addClass("gallery-caption"):wikitext(caption)
            end
        end
    end

    return tostring(container)
end

-- ---------- Interface ----------

function p.main(frame)
    return p.render(frame.args[1] or "")
end

return p