Modul:Gallery

Aus Steel Beasts Wiki
Version vom 5. Februar 2023, 19:44 Uhr von sc>FoXFTW
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)

Die Dokumentation für dieses Modul kann unter Modul:Gallery/Doku erstellt werden

local Gallery = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable

--- Query SMW for images in Category:Galerie and the current page
---
--- @return table|nil
function methodtable.loadSmwData( self, page, depth, limit )
    depth = depth or 5
    limit = limit or 12

    local result = mw.smw.ask{
        '[[Kategorie:' .. page .. '|+depth=' .. depth .. ']][[Kategorie:Galerie]][[Datei:!~*.mp4]][[Datei:!~*.webm]]',
        '?#-=image', '?Beschreibung=caption', '+index=1', limit=limit, mainlabel='-'
    }

    if result ~= nil then
        return result
    end

    return nil
end

--- Generate the gallery and output an optional header
---
--- @return string
function methodtable.makeGallery( self, data, withHeader, addMoreLink, page )
    local div = mw.html.create( 'div' )
                  :attr( 'class', 'gallery' )

    for _, image in pairs( data ) do
        if image[ 'caption' ] == nil then
            div
                :wikitext( '[[' .. image[ 'image' ] .. '|frameless|300px]]' )
            :done()
        else
        	--if mw.ustring.len( image[ 'caption' ] ) > 100 then
        	--	image[ 'caption' ] = mw.ustring.sub( image[ 'caption' ], 0, 100 ) .. '…'
    		--end

            div
                :tag( 'div' )
                    :addClass( 'image' )
                    :wikitext( '[[' .. image[ 'image' ] .. '|frameless|300px|' .. image[ 'caption' ] .. '|alt=' .. image[ 'caption' ] .. ']]' )
                    :tag( 'span' )
                        :addClass( 'thumbcaption magnify' )
                        :wikitext( image[ 'caption' ] )
                    :done()
                :done()
            :done()
        end

        div:done()
    end

    local h2 = mw.html.create( 'h2' ):wikitext( 'Galerie' ):done()

    if withHeader == false then
        h2 = ''
    end

	if addMoreLink == true then
        div
            :tag( 'span' )
                :addClass( 'all-images-link' )
                :wikitext( '[[:Kategorie:' .. page .. '|Weitere Bilder]]' )
            :done()
        :done()
	end

    return tostring( h2 ) .. tostring( div )
end

--- Entry for template calls
--- Load images from SMW by pagename
---
--- @return string
function Gallery.output( frame )
    local instance = Gallery:new()

    local page = mw.title.getCurrentTitle().subpageText

    if frame:getParent().args.page ~= nil and #frame:getParent().args.page > 0 then
        page = frame:getParent().args.page
    end

    if frame:getParent().args.noGallery ~= nil then
        return ''
    end

    local images = instance:loadSmwData(
    	page,
    	frame:getParent().args.depth or 0,
    	frame:getParent().args[ 'Bilderanzahl' ] or 12
    )

    if images == nil then
        return ''
    end

    return instance:makeGallery( images, true, true, page )
end

--- Entry for module calls
---
--- @return string
function Gallery.fromArgs( frame )
    local instance = Gallery:new()

    local args = require( 'Module:Arguments' ).getArgs( frame )

    local images = {}

    for k, arg in pairs( args ) do
    	local ending = mw.ustring.sub( arg, -3 )

		if ending == 'png' or ending == 'jpg' or ending == 'peg' then
			arg = mw.title.new( arg, 6 ).prefixedText
		end

		local sub = mw.ustring.sub( arg, 0, 4 )

        if sub == 'File' or sub == 'Date' then
            table.insert( images, k, {
                [ 'image' ] = arg
            } )
        else
            local prev = k - 1
            if images[ prev ] ~= nil then
                images[ prev ] = {
                    [ 'image' ] = images[ prev ][ 'image' ],
                    [ 'caption' ] = arg
                }
            end
        end
    end

    if #images == 0 then
    	return Gallery.output( frame )
	end

    return instance:makeGallery( images, false, false )
end

--- New Instance
--- @return table Gallery
function Gallery.new( self )
    local instance = {}

    setmetatable( instance, metatable )

    return instance
end

return Gallery